Snippets
GEORGE LIU mysqladmin_shell.sh Centmin Mod Addon https://community.centminmod.com/threads/mysqladmin_shell-sh-shell-based-addon.543/. For simple tasks just for creating new MySQL users/passwords, creating new My
Created by
GEORGE LIU
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 | #!/bin/bash
VER=0.0.1
###############################################################
# 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) vbtechsupport.com
###############################################################
# 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'
###############################################################
# 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
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
else
rootset='n'
fi
cecho "--------------------------------------------------------------" $boldyellow
echo ""
}
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 -u$myrootuser -p$myrootpass -e "CREATE DATABASE $newdbname; CREATE USER '$newmysqluser'@'$MYSQLHOSTNAME' IDENTIFIED BY '$newmysqluserpass'; GRANT 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
else
echo ""
cecho "Ok: MySQL user: $newmysqluser MySQL database: $newdbname created successfully" $boldyellow
fi
elif [[ "$rootset" = [nN] && "$createnewuser" = [yY] ]]; then
mysql -e "CREATE DATABASE $newdbname; CREATE USER '$newmysqluser'@'$MYSQLHOSTNAME' IDENTIFIED BY '$newmysqluserpass'; GRANT 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
else
echo ""
cecho "Ok: MySQL user: $newmysqluser MySQL database: $newdbname created successfully" $boldyellow
fi
elif [[ "$rootset" = [nN] && "$createnewuser" = [nN] ]]; then
mysql -e "CREATE DATABASE $newdbname; GRANT 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
else
echo ""
cecho "Ok: New MySQL database: $newdbname assigned to existing MySQL user: $existingmysqluser" $boldyellow
fi
elif [[ "$rootset" = [yY] && "$createnewuser" = [nN] ]]; then
mysql -u$myrootuser -p$myrootpass -e "CREATE DATABASE $newdbname; GRANT 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
else
echo ""
cecho "Ok: New MySQL database: $newdbname assigned to existing MySQL user: $existingmysqluser" $boldyellow
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 -u$myrootuser -p$myrootpass -e "set password for '$changemysqluserpass'@'$MYSQLHOSTNAME' = password('$changenewmysqlpass');"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: Changing MySQL password for $changemysqluserpass was unsuccessful" $boldgreen
else
echo ""
cecho "Ok: Changing MySQL password for $changemysqluserpass was successful" $boldyellow
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
else
echo ""
cecho "Ok: Changing MySQL password for $changemysqluserpass was successful" $boldyellow
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 -u$myrootuser -p$myrootpass -e "drop user '$delmysqluser'@'$MYSQLHOSTNAME'; flush privileges;"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: Deleting '$delmysqluser'@'$MYSQLHOSTNAME' was unsuccessful" $boldgreen
else
echo ""
cecho "Ok: Deleting '$delmysqluser'@'$MYSQLHOSTNAME' was successful" $boldyellow
fi
else
mysql -e "drop user '$delmysqluser'@'$MYSQLHOSTNAME'; flush privileges;"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: Deleting '$delmysqluser'@'$MYSQLHOSTNAME' was unsuccessful" $boldgreen
else
echo ""
cecho "Ok: Deleting '$delmysqluser'@'$MYSQLHOSTNAME' was successful" $boldyellow
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 -u$myrootuser -p$myrootpass -e "SHOW GRANTS for '$showmysqluser'@'$MYSQLHOSTNAME';"
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
echo ""
cecho "Error: SHOW GRANTS for '$showmysqluser'@'$MYSQLHOSTNAME' was unsuccessful" $boldgreen
else
echo ""
cecho "Ok: SHOW GRANTS for '$showmysqluser'@'$MYSQLHOSTNAME' was successful" $boldyellow
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
else
echo ""
cecho "Ok: SHOW GRANTS for '$showmysqluser'@'$MYSQLHOSTNAME' was successful" $boldyellow
fi
fi
}
###############################################################
case "$1" in
setuserdb)
mysqlperm
createuserdb
;;
setpass)
mysqlperm
changeuserpass
;;
deluser)
mysqlperm
delusername
;;
showgrants)
mysqlperm
listgrants
;;
*)
echo ""
cecho "$0 {setuserdb|setpass|deluser|showgrants}" $boldyellow
echo ""
;;
esac
exit
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.