Created
November 8, 2013 07:04
-
-
Save ssv445/7367304 to your computer and use it in GitHub Desktop.
This script creates a database 'db' and a user as 'username' with given password 'pass'. It also grant 'username' ALL access to 'db'. Works on any linux.
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 | |
# A shell script to add mysql database, username and password. | |
# ------------------------------------------------------------------------- | |
_db="$1" | |
_user="$2" | |
_pass="$3" | |
## Mysql root settings ## | |
_madminuser='root' | |
_madminpwd='mysql-root-password' | |
# make sure we get 3 args, else die | |
[[ $# -le 2 ]] && { echo "Usage: $0 'DB_Name' 'DB_USER' 'DB_PASSORD' "; exit 1; } | |
echo 'FLUSH PRIVILEGES;' > /tmp/sql | |
# create DB | |
query=" CREATE DATABASE ${_db} ;"; | |
echo $query >> /tmp/sql | |
# grant permission | |
query=" GRANT ALL ON ${_db}.* TO ${_user} IDENTIFIED BY '${_pass}';"; | |
echo $query >> /tmp/sql | |
#check db and permission | |
query="SELECT Host, Db, User FROM mysql.db WHERE User='${_user}' and Db='${_db}';"; | |
echo $query >> /tmp/sql | |
echo "Running: [sudo mysql -u${_madminuser} --password=\"${_madminpwd}\" < /tmp/sql]" | |
sudo mysql -u${_madminuser} --password="${_madminpwd}" < /tmp/sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment