Skip to content

Instantly share code, notes, and snippets.

@kappaj
Forked from matteocaberlotto/create-database.sh
Created April 12, 2016 16:14
Show Gist options
  • Save kappaj/aa61b7e5df7ec02a21d89984b287eb4c to your computer and use it in GitHub Desktop.
Save kappaj/aa61b7e5df7ec02a21d89984b287eb4c 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