Skip to content

Instantly share code, notes, and snippets.

@hoahovan
Last active November 26, 2019 02:37
Show Gist options
  • Select an option

  • Save hoahovan/97ec284c573a87b81169392fa26ebef0 to your computer and use it in GitHub Desktop.

Select an option

Save hoahovan/97ec284c573a87b81169392fa26ebef0 to your computer and use it in GitHub Desktop.
# Setup database
printf "Setup databases and users\n"
while true; do
read -p "Enter password for MySQL root: " mysqlrootpsw
case $mysqlrootpsw in
"" ) printf "Password may not be left blank\n";;
* ) break;;
esac
done
printf "\nPlease set name for databases, users and passwords\n"
while true; do
read -p "Production database name (recommended: use domain without TLD, for mydomain.com use mydomain): " dbname
case $dbname in
"" ) printf "Database name may not be left blank\n";;
* ) break;;
esac
done
while true; do
read -p "Production database user (recommended: use same as database name, max 16 characters): " dbuser
case $dbuser in
"" ) printf "User name may not be left blank\n";;
* ) break;;
esac
done
while true; do
read -p "Production database password: " dbpass
case $dbpass in
"" ) printf "\nPassword may not be left blank\n";;
* ) break;;
esac
done
printf "Create database $dbname...\n"
mysql -u root -p$mysqlrootpsw -e "CREATE DATABASE $dbname;"
printf "Create user $dbuser...\n"
mysql -u root -p$mysqlrootpsw -e "CREATE USER '$dbuser'@localhost IDENTIFIED BY '$dbpass';"
printf "Grant $dbuser all privileges on $dbname...\n"
mysql -u root -p$mysqlrootpsw -e "GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@localhost;"
printf "Restart MySQL...\n"
service mysql restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment