Skip to content

Instantly share code, notes, and snippets.

@agustibr
Created April 19, 2016 09:04
Show Gist options
  • Save agustibr/3c3a779f5a13d9ad2a6ffac3725fa417 to your computer and use it in GitHub Desktop.
Save agustibr/3c3a779f5a13d9ad2a6ffac3725fa417 to your computer and use it in GitHub Desktop.
Bash script that quickly generates a MySQL database, user, and password
#!/bin/bash
# Usage:
# $ brew install mysql
# $ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
# $ bash -c "$(curl -fsSL https://gist.githubusercontent.com/richardcornish/e9093757a6977b1b6b5b/raw)"
echo "Welcome to the MySQL generator!"
read -p "Database name: " dbname
read -p "Database user: " dbuser
read -p "Database pass: " -s dbpass
Q1="CREATE DATABASE IF NOT EXISTS $dbname;"
Q2="GRANT USAGE ON $dbname.* TO '$dbuser'@'localhost' IDENTIFIED BY '$dbpass';"
Q3="GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@'localhost';"
Q4="FLUSH PRIVILEGES;"
echo -e "\nMySQL root password (or press return if none)"
mysql -u root -p -e "${Q1}${Q2}${Q3}${Q4}"
echo "Generated database \"$dbname\" with user \"$dbuser\" and password."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment