Skip to content

Instantly share code, notes, and snippets.

@yevrah
Created March 19, 2015 00:34
Show Gist options
  • Save yevrah/46fadb98c88363692236 to your computer and use it in GitHub Desktop.
Save yevrah/46fadb98c88363692236 to your computer and use it in GitHub Desktop.
MySQL: User Setup Script
#!/bin/bash
# Create db - if it doesnt exist, and setup user ready to
# read and write
#
# Usage: db-user-setup.sh <database> <user> <password>
EXPECTED_ARGS=3
E_BADARGS=65
MYSQL=`which mysql`
Q1="CREATE DATABASE IF NOT EXISTS $1;"
Q2="GRANT USAGE ON *.* TO $2@localhost IDENTIFIED BY '$3';"
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 database user pass"
exit $E_BADARGS
fi
$MYSQL -uroot -p -e "$SQL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment