Last active 1764358143

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