Последняя активность 1764358143

Версия cf39fd1a6bc136e7e2f20db1d024fe8402ba3590

mysql_dbu_create.sh Исходник
1#!/usr/bin/env bash
2
3#
4# Script to create MySQL/MariaDB db + user
5#
6# @author Karel Wintersky <karel.wintersky@gmail.com>
7# @version 0.4
8# mysql_config_editor set --login-path=proftpd --host=localhost --user=proftpd --password
9# version this: https://gist.github.com/KarelWintersky/72021fe214f9d2f6ddc6a28c732baac1
10# version 0.2: https://gist.github.com/KarelWintersky/9cb12557873ebc59b5cb94cb37bb6913
11
12_bold=$(tput bold)
13_underline=$(tput sgr 0 1)
14_reset=$(tput sgr0)
15
16_purple=$(tput setaf 171)
17_red=$(tput setaf 1)
18_green=$(tput setaf 76)
19_tan=$(tput setaf 3)
20_blue=$(tput setaf 38)
21_cyan=$(tput setaf 6)
22_yellow=$(tput setaf 3)
23
24function _success()
25{
26 printf "${_green}✔ %s${_reset}\n" "$@"
27}
28
29function _warning()
30{
31 printf "${_yellow}⚠ %s${_reset}\n" "$@"
32}
33
34function _error()
35{
36 printf "${_red}✖ %s${_reset}\n" "$@"
37}
38
39function _info()
40{
41 printf "${_blue}ℹ %s${_reset}\n" "$@"
42}
43
44function _debug()
45{
46 printf "${_cyan}🔧 %s${_reset}\n" "$@"
47}
48
49function _printPoweredBy()
50{
51 local db_type=$(detectDBType)
52 local db_title="${_bold}${_blue}MySQL${_reset}"
53
54 if [ "$db_type" = "mariadb" ]; then
55 db_title="${_bold}${_purple}MariaDB${_reset}"
56 fi
57
58 echo ""
59 echo "${_bold}################################################################${_reset}"
60 echo "${_bold}${db_title} :: Create database, user and password${_reset}"
61 echo "${_bold}(c) Karel Wintersky <karel.wintersky@gmail.com>, 2018${_reset}"
62 echo "${_bold}################################################################${_reset}"
63 echo ""
64}
65
66function detectDBType()
67{
68 local mysql_bin=$(which mysql 2>/dev/null)
69
70 if [ -z "$mysql_bin" ]; then
71 _error "MySQL client not found!"
72 exit 1
73 fi
74
75 # Проверяем, является ли /usr/bin/mysql символической ссылкой
76 if [ -L "/usr/bin/mysql" ]; then
77 local link_target=$(readlink -f "/usr/bin/mysql")
78 if [[ "$link_target" == *"mariadb"* ]]; then
79 echo "mariadb"
80 return
81 fi
82 fi
83
84 # Дополнительная проверка через which mariadb
85 local mariadb_bin=$(which mariadb 2>/dev/null)
86 if [ -n "$mariadb_bin" ] && [ "$mysql_bin" = "$mariadb_bin" ]; then
87 echo "mariadb"
88 return
89 fi
90
91 echo "mysql"
92}
93
94function getDBBinary()
95{
96 local db_type=$(detectDBType)
97
98 if [ "$db_type" = "mariadb" ]; then
99 which mariadb
100 else
101 which mysql
102 fi
103}
104
105function generatePassword()
106{
107 echo "$(openssl rand -base64 12)"
108}
109
110function getCredentials()
111{
112 _info "Please enter database credentials:"
113 echo ""
114
115 read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter the NAME of the new database:${_reset} " DBNAME
116 read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter HOST for user access (% for remote access): ${_reset} " -i "localhost" USERHOST
117 read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter the database CHARACTER SET (latin1, utf8): ${_reset} " -i "utf8mb4" CHARSET
118 read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter the NAME of the user:${_reset} " -i "$DBNAME" USERNAME
119 read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter the PASSWORD for the user:${_reset} " -i "$PASSWORD" PASSWORD
120}
121
122function getRootPassword()
123{
124 echo ""
125 _warning "Root password required for database operations"
126 read -e -p "${_bold}${_yellow}?${_reset} ${_bold}Enter MySQL/MariaDB root user password:${_reset} " -s ROOTPASSWORD
127 echo ""
128
129 ROOTACCESS="--user=root --password=${ROOTPASSWORD}"
130}
131
132function checkDBExist()
133{
134 local FOUND
135 FOUND=$(${DB_BINARY} ${ROOTACCESS} -e "SHOW DATABASES LIKE '${DBNAME}';" | grep ${DBNAME})
136 echo ${FOUND}
137}
138
139function checkUserExist()
140{
141 local FOUND
142 FOUND=$(${DB_BINARY} ${ROOTACCESS} -e "SELECT COUNT(*) FROM mysql.user WHERE user = '${USERNAME}';" | grep 1)
143
144 if [ "${FOUND}" = "1" ]; then
145 echo "1"
146 else
147 echo "0"
148 fi
149}
150
151function create()
152{
153 if [ ! -f ~/.my.cnf ]; then
154 getRootPassword
155 fi
156
157 echo ""
158 _info "Starting database creation process..."
159 echo ""
160
161 if [[ -n $(checkDBExist) ]]; then
162 _warning "Database ${_bold}${DBNAME}${_reset} already exists!";
163 else
164 _info "Creating database ${_bold}${DBNAME}${_reset}..."
165 ${DB_BINARY} ${ROOTACCESS} -e "CREATE DATABASE ${DBNAME} /*\!40100 DEFAULT CHARACTER SET ${CHARSET} */;"
166 _success "Database ${_bold}${DBNAME}${_reset} successfully created!"
167 fi
168
169 echo ""
170
171 if [ $(checkUserExist) = "0" ]; then
172 _info "Creating new user ${_bold}${USERNAME}${_reset}..."
173 ${DB_BINARY} ${ROOTACCESS} -e "CREATE USER ${USERNAME}@'${USERHOST}' IDENTIFIED BY '${PASSWORD}';"
174 _success "User ${_bold}${USERNAME}${_reset} successfully created!"
175 else
176 _warning "User ${_bold}${USERNAME}${_reset} already exists!";
177 fi
178
179 echo ""
180
181 _info "Granting ALL privileges on ${_bold}${DBNAME}${_reset} to ${_bold}${USERNAME}${_reset}..."
182 ${DB_BINARY} ${ROOTACCESS} -e "GRANT ALL PRIVILEGES ON ${DBNAME}.* TO '${USERNAME}'@'${USERHOST}';"
183 ${DB_BINARY} ${ROOTACCESS} -e "FLUSH PRIVILEGES;"
184 _success "Privileges successfully granted!"
185 echo ""
186}
187
188function printSuccessMessage()
189{
190 local db_type=$(detectDBType)
191 local db_name="${_bold}${_blue}MySQL${_reset}"
192
193 if [ "$db_type" = "mariadb" ]; then
194 db_name="${_bold}${_purple}MariaDB${_reset}"
195 fi
196
197 _success "${db_name} database creation completed!"
198 echo ""
199
200 echo "${_bold}${_green}################################################################${_reset}"
201 echo ""
202 echo " ${_green}${_reset} ${_bold}Database Type:${_reset} ${db_name}"
203 echo " ${_green}${_reset} ${_bold}Database:${_reset} ${_green}${DBNAME}${_reset}"
204 echo " ${_green}${_reset} ${_bold}User:${_reset} ${_green}${USERNAME}${_reset}"
205 echo " ${_green}${_reset} ${_bold}Password:${_reset} ${_green}${PASSWORD}${_reset}"
206 echo " ${_green}${_reset} ${_bold}Host:${_reset} ${_green}${USERHOST}${_reset}"
207 echo " ${_green}${_reset} ${_bold}Charset:${_reset} ${_green}${CHARSET}${_reset}"
208 echo ""
209 echo "${_bold}${_green}################################################################${_reset}"
210}
211
212################################################################################
213# Main
214################################################################################
215export LC_CTYPE=C
216export LANG=C
217VERSION="0.3"
218
219# Определяем бинарник для работы с БД
220DB_BINARY=$(getDBBinary)
221DB_TYPE=$(detectDBType)
222
223if [ -z "$DB_BINARY" ]; then
224 _error "No database client found (mysql or mariadb)"
225 exit 1
226fi
227
228ROOTACCESS=
229ROOTPASSWORD=
230CHARSET='utf8mb4';
231DBNAME=
232USERNAME=
233USERHOST='localhost'
234PASSWORD=$(generatePassword);
235
236function main()
237{
238 _printPoweredBy
239
240 getCredentials
241
242 echo ""
243 echo "${_bold}${_blue}################################################################${_reset}"
244 echo ""
245
246 create
247 printSuccessMessage
248}
249
250main
251
252exit 0