Skip to content

Instantly share code, notes, and snippets.

@matteocaberlotto
Created February 26, 2016 14:13
Show Gist options
  • Save matteocaberlotto/702a9c7fc3043d6c636e to your computer and use it in GitHub Desktop.
Save matteocaberlotto/702a9c7fc3043d6c636e to your computer and use it in GitHub Desktop.
bash script to create user and database
#!/bin/bash
if [ "$1" == "" ]; then
echo "Error: database required: use create-database <db_name> <username> <password>"
exit 1
fi
if [ "$2" == "" ]; then
echo "Error: username required: use create-database <db_name> <username> <password>"
exit 1
fi
if [ "$3" == "" ]; then
echo "Error: password required: use create-database <db_name> <username> <password>"
exit 1
fi
mysql -u root -p -e "CREATE DATABASE $1 CHARACTER SET utf8 COLLATE utf8_unicode_ci"
mysql -u root -p -e "CREATE USER '$2'@'localhost' IDENTIFIED BY '$3'"
mysql -u root -p -e "GRANT ALL PRIVILEGES ON $1.* TO '$2'@'localhost'"
mysql -u root -p -e "FLUSH PRIVILEGES"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment