Zuletzt aktiv 3 weeks ago

Änderung faff6de7b1ef91f0e182c99df0c9d1cbed63da4f

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