Skip to content

Instantly share code, notes, and snippets.

@dharma017
Forked from hilja/create-mysql-db.sh
Last active February 4, 2016 09:09
Show Gist options
  • Select an option

  • Save dharma017/37b5eba44c64956521c9 to your computer and use it in GitHub Desktop.

Select an option

Save dharma017/37b5eba44c64956521c9 to your computer and use it in GitHub Desktop.
Shell script to create MySQL database and user
#!/bin/bash
#Usage:
#Name it something like mysql-db-create.sh
#Make it executable $ sudo chmod 755 mysql-db-create.sh
#Run it sudo ./mysql-db-create.sh dbname dbuser password
# Functions
ok() { echo -e '\e[32m'$1'\e[m'; } # Green
EXPECTED_ARGS=3
E_BADARGS=65
MYSQL=`which mysql`
Q1="CREATE DATABASE IF NOT EXISTS $1;"
Q2="GRANT ALL ON *.* TO '$2'@'localhost' IDENTIFIED BY '$3';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}"
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: $0 dbname dbuser dbpass"
exit $E_BADARGS
fi
$MYSQL -uroot -p -e "$SQL"
ok "Database $1 and user $2 created with a password $3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment