/
mysqladmin_shell.sh
executable file
·362 lines (296 loc) · 10.8 KB
/
mysqladmin_shell.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/bin/bash
VER=0.0.2
###############################################################
# mysqladmin shell Centmin Mod Addon for centminmod.com users
# create new mysql username and assign standard
# permissions to specified database name
# written by George Liu (eva2000) centminmod.com
#
# How to Use
# https://community.centminmod.com/threads/543
###############################################################
# http://dev.mysql.com/doc/refman/5.1/en/account-management-sql.html
# http://dev.mysql.com/doc/refman/5.1/en/create-user.html
# http://dev.mysql.com/doc/refman/5.1/en/drop-user.html
# http://dev.mysql.com/doc/refman/5.1/en/grant.html
# http://dev.mysql.com/doc/refman/5.1/en/revoke.html
# http://dev.mysql.com/doc/refman/5.1/en/set-password.html
###############################################################
# variables
#############
MYSQLHOSTNAME='localhost'
MYSQLEXTRA_FILE='/root/.my.cnf'
###############################################################
# Setup Colours
black='\E[30;40m'
red='\E[31;40m'
green='\E[32;40m'
yellow='\E[33;40m'
blue='\E[34;40m'
magenta='\E[35;40m'
cyan='\E[36;40m'
white='\E[37;40m'
boldblack='\E[1;30;40m'
boldred='\E[1;31;40m'
boldgreen='\E[1;32;40m'
boldyellow='\E[1;33;40m'
boldblue='\E[1;34;40m'
boldmagenta='\E[1;35;40m'
boldcyan='\E[1;36;40m'
boldwhite='\E[1;37;40m'
Reset="tput sgr0" # Reset text attributes to normal
#+ without clearing screen.
cecho () # Coloured-echo.
# Argument $1 = message
# Argument $2 = color
{
message=$1
color=$2
echo -e "$color$message" ; $Reset
return
}
###############################################################
# functions
#############
mysqlperm() {
cecho "--------------------------------------------------------------" $boldyellow
cecho "Basic MySQL Admin - create mysql user & databases " $boldgreen
cecho "--------------------------------------------------------------" $boldyellow
if [ ! -f "$MYSQLEXTRA_FILE" ]; then
read -ep " Do you have mysql root user password set ? [y/n]: " rootset
if [[ "$rootset" = [yY] ]]; then
read -ep " Enter your mysql root username i.e. root: " myrootuser
read -ep " Enter your mysql root password: " myrootpass
MYSQLOPTS="-u$myrootuser -p$myrootpass"
else
rootset='n'
fi
else
MYSQLOPTS="--defaults-extra-file=${MYSQLEXTRA_FILE}"
rootset=y
fi
cecho "--------------------------------------------------------------" $boldyellow
echo ""
}
multicreatedb() {
cecho "----------------------------------------------------------------------------" $boldyellow
cecho "Create Multiple MySQL Databases, User & Pass From specified filepath/name" $boldgreen
cecho "i.e. /home/nginx/domains/domain.com/dbfile.txt" $boldgreen
cecho "One entry per line in dbfile.txt in format of:" $boldgreen
cecho "databasename databaseuser databasepass" $boldgreen
cecho "----------------------------------------------------------------------------" $boldyellow
echo
read -ep " Enter full path to db list file i.e. /home/nginx/domains/domain.com/dbfile.txt (to exit type = x): " dbfile
echo
if [[ "$dbfile" = [xX] || -z "$dbfile" ]]; then
exit
fi
if [[ "$rootset" = [yY] && -f "$dbfile" ]]; then
while read -r db u p; do
mysql ${MYSQLOPTS} -e "CREATE DATABASE $db; CREATE USER '$u'@'$MYSQLHOSTNAME' IDENTIFIED BY '$p'; GRANT index, select, insert, delete, update, create, drop, alter, create temporary tables, execute, lock tables ON $db.* TO '$u'@'$MYSQLHOSTNAME'; flush privileges; show grants for '$u'@'$MYSQLHOSTNAME';"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: command was unsuccessful" $boldgreen
echo
else
echo ""
cecho "Ok: MySQL user: $u MySQL database: $db created successfully" $boldyellow
echo
fi
done < $dbfile
fi
}
createuserdb() {
read -ep " Do you want to create a new MySQL username (type = y) or `echo $'\n '`Add a new database name to existing MySQL username (type = n) ? `echo $'\n '`Enter y or n: " createnewuser
if [[ "$createnewuser" = [yY] ]]; then
cecho "---------------------------------" $boldyellow
cecho "Create MySQL username:" $boldgreen
cecho "---------------------------------" $boldyellow
read -ep " Enter new MySQL username you want to create: " newmysqluser
read -ep " Enter new MySQL username's password: " newmysqluserpass
else
createnewuser='n'
cecho "-------------------------------------------------------------------------" $boldyellow
cecho "Add new database name to existing MySQL username:" $boldgreen
cecho "-------------------------------------------------------------------------" $boldyellow
read -ep " Enter existing MySQL username you want to add new database name to: " existingmysqluser
fi
cecho "---------------------------------" $boldyellow
cecho "Create MySQL database:" $boldgreen
cecho "---------------------------------" $boldyellow
read -ep " Enter new MySQL database name: " newdbname
echo
if [[ "$rootset" = [yY] && "$createnewuser" = [yY] ]]; then
mysql ${MYSQLOPTS} -e "CREATE DATABASE $newdbname; CREATE USER '$newmysqluser'@'$MYSQLHOSTNAME' IDENTIFIED BY '$newmysqluserpass'; GRANT index, select, insert, delete, update, create, drop, alter, create temporary tables, execute, lock tables ON $newdbname.* TO '$newmysqluser'@'$MYSQLHOSTNAME'; flush privileges; show grants for '$newmysqluser'@'$MYSQLHOSTNAME';"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: command was unsuccessful" $boldgreen
echo
else
echo ""
cecho "Ok: MySQL user: $newmysqluser MySQL database: $newdbname created successfully" $boldyellow
echo
fi
elif [[ "$rootset" = [nN] && "$createnewuser" = [yY] ]]; then
mysql -e "CREATE DATABASE $newdbname; CREATE USER '$newmysqluser'@'$MYSQLHOSTNAME' IDENTIFIED BY '$newmysqluserpass'; GRANT index, select, insert, delete, update, create, drop, alter, create temporary tables, execute, lock tables ON $newdbname.* TO '$newmysqluser'@'$MYSQLHOSTNAME'; flush privileges; show grants for '$newmysqluser'@'$MYSQLHOSTNAME';"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: command was unsuccessful" $boldgreen
echo
else
echo ""
cecho "Ok: MySQL user: $newmysqluser MySQL database: $newdbname created successfully" $boldyellow
echo
fi
elif [[ "$rootset" = [nN] && "$createnewuser" = [nN] ]]; then
mysql -e "CREATE DATABASE $newdbname; GRANT index, select, insert, delete, update, create, drop, alter, create temporary tables, execute, lock tables ON $newdbname.* TO '$existingmysqluser'@'$MYSQLHOSTNAME'; flush privileges; show grants for '$existingmysqluser'@'$MYSQLHOSTNAME';"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: command was unsuccessful" $boldgreen
echo
else
echo ""
cecho "Ok: New MySQL database: $newdbname assigned to existing MySQL user: $existingmysqluser" $boldyellow
echo
fi
elif [[ "$rootset" = [yY] && "$createnewuser" = [nN] ]]; then
mysql ${MYSQLOPTS} -e "CREATE DATABASE $newdbname; GRANT index, select, insert, delete, update, create, drop, alter, create temporary tables, execute, lock tables ON $newdbname.* TO '$existingmysqluser'@'$MYSQLHOSTNAME'; flush privileges; show grants for '$existingmysqluser'@'$MYSQLHOSTNAME';"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: command was unsuccessful" $boldgreen
echo
else
echo ""
cecho "Ok: New MySQL database: $newdbname assigned to existing MySQL user: $existingmysqluser" $boldyellow
echo
fi
fi
}
changeuserpass() {
cecho "--------------------------------------------------------------" $boldyellow
cecho "Change Existing MySQL username's password:" $boldgreen
cecho "--------------------------------------------------------------" $boldyellow
read -ep " Enter MySQL username you want to change password for: " changemysqluserpass
read -ep " Enter MySQL username's new password to change to: " changenewmysqlpass
if [[ "$rootset" = [yY] ]]; then
mysql ${MYSQLOPTS} -e "set password for '$changemysqluserpass'@'$MYSQLHOSTNAME' = password('$changenewmysqlpass');"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: Changing MySQL password for $changemysqluserpass was unsuccessful" $boldgreen
echo
else
echo ""
cecho "Ok: Changing MySQL password for $changemysqluserpass was successful" $boldyellow
echo
fi
else
mysql -e "set password for '$changemysqluserpass'@'$MYSQLHOSTNAME' = password('$changenewmysqlpass');"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: Changing MySQL password for $changemysqluserpass was unsuccessful" $boldgreen
echo
else
echo ""
cecho "Ok: Changing MySQL password for $changemysqluserpass was successful" $boldyellow
echo
fi
fi
}
delusername() {
cecho "---------------------------------" $boldyellow
cecho "Delete MySQL username:" $boldgreen
cecho "---------------------------------" $boldyellow
read -ep " Enter MySQL username you want to delete: " delmysqluser
if [[ "$rootset" = [yY] ]]; then
mysql ${MYSQLOPTS} -e "drop user '$delmysqluser'@'$MYSQLHOSTNAME'; flush privileges;"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: Deleting '$delmysqluser'@'$MYSQLHOSTNAME' was unsuccessful" $boldgreen
echo
else
echo ""
cecho "Ok: Deleting '$delmysqluser'@'$MYSQLHOSTNAME' was successful" $boldyellow
echo
fi
else
mysql -e "drop user '$delmysqluser'@'$MYSQLHOSTNAME'; flush privileges;"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: Deleting '$delmysqluser'@'$MYSQLHOSTNAME' was unsuccessful" $boldgreen
echo
else
echo ""
cecho "Ok: Deleting '$delmysqluser'@'$MYSQLHOSTNAME' was successful" $boldyellow
echo
fi
fi
}
listgrants() {
cecho "---------------------------------" $boldyellow
cecho "Show Grants for MySQL username:" $boldgreen
cecho "---------------------------------" $boldyellow
read -ep " Enter MySQL username to Show Grant permissions: " showmysqluser
if [[ "$rootset" = [yY] ]]; then
mysql ${MYSQLOPTS} -e "SHOW GRANTS for '$showmysqluser'@'$MYSQLHOSTNAME';"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: SHOW GRANTS for '$showmysqluser'@'$MYSQLHOSTNAME' was unsuccessful" $boldgreen
echo
else
echo ""
cecho "Ok: SHOW GRANTS for '$showmysqluser'@'$MYSQLHOSTNAME' was successful" $boldyellow
echo
fi
else
mysql -e "SHOW GRANTS for '$showmysqluser'@'$MYSQLHOSTNAME';"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: SHOW GRANTS for '$showmysqluser'@'$MYSQLHOSTNAME' was unsuccessful" $boldgreen
echo
else
echo ""
cecho "Ok: SHOW GRANTS for '$showmysqluser'@'$MYSQLHOSTNAME' was successful" $boldyellow
echo
fi
fi
}
###############################################################
case "$1" in
multidb)
mysqlperm
multicreatedb
;;
setuserdb)
mysqlperm
createuserdb
;;
setpass)
mysqlperm
changeuserpass
;;
deluser)
mysqlperm
delusername
;;
showgrants)
mysqlperm
listgrants
;;
*)
echo ""
cecho "$0 {multidb|setuserdb|setpass|deluser|showgrants}" $boldyellow
echo ""
;;
esac
exit