Son aktivite 3 weeks ago

Revizyon 21e80f3ef1a009d7ab56c11674551361cb4de10c

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