Created
February 22, 2019 04:37
-
-
Save Nill-R/fd93c57bbb035024586d9cda2009c451 to your computer and use it in GitHub Desktop.
Script for create db and user with all privs to it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
EXPECTED_ARGS=2 | |
E_BADARGS=65 | |
MYSQL=`which mysql` | |
PASS=`tr -cd '[:alnum:]' < /dev/urandom | fold -w24 | head -n1` | |
echo $PASS | |
Q1="CREATE DATABASE IF NOT EXISTS $1;" | |
Q2="GRANT USAGE ON *.* TO $2@localhost IDENTIFIED BY '$PASS';" | |
Q3="GRANT ALL PRIVILEGES ON $1.* TO $2@localhost;" | |
Q4="FLUSH PRIVILEGES;" | |
SQL="${Q1}${Q2}${Q3}${Q4}" | |
if [ $# -ne $EXPECTED_ARGS ] | |
then | |
echo "Usage: $0 dbname dbuser" | |
exit $E_BADARGS | |
fi | |
$MYSQL -e "$SQL" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment