Skip to content

Instantly share code, notes, and snippets.

@ssv445
Created November 8, 2013 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssv445/7367304 to your computer and use it in GitHub Desktop.
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.
#!/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