Skip to content

Instantly share code, notes, and snippets.

@benjamin-dk
Last active December 18, 2015 01:29
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 benjamin-dk/5704415 to your computer and use it in GitHub Desktop.
Save benjamin-dk/5704415 to your computer and use it in GitHub Desktop.
Linux bash script for setting up a new database and database user in MySQL
#!/bin/bash
# Command should follow this pattern: ./createdb testdb testuser secretpass
BTICK='`'
EXPECTED_ARGS=3
E_BADARGS=65
MYSQL=`which mysql`
echo "You will be prompted your mysql root password."
Q1="CREATE DATABASE IF NOT EXISTS $1 CHARACTER SET utf8 COLLATE utf8_general_ci;"
Q2="GRANT ALL ON ${BTICK}$1${BTICK}.* 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"
# We see if the mysql command executed correctly
status=$?
if test $status -eq 0
then
echo -e "Drupal-ready database and database user created >8-D\nOhh bash scripting, it's better than your moms homemade jellybeans"
else
echo "Dangdiddeli-doh - something went wrong!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment