Skip to content

Instantly share code, notes, and snippets.

@9ete
Last active August 29, 2015 14:06
Show Gist options
  • Save 9ete/8da969f373de6c83c3d2 to your computer and use it in GitHub Desktop.
Save 9ete/8da969f373de6c83c3d2 to your computer and use it in GitHub Desktop.
Create MySQL database via the command line. Pass it a short db and user name, then enter db pass.
#!/bin/bash
echo "Enter Short Name for Database:"
read DATABASENAME
echo "Enter Short Name for User:"
read USERNAME
DATE=$(date +%s)
PASSWORD1=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 4 | head -n 1)
PASSWORD2=`echo "((($RANDOM * $DATE)/$RANDOM)/$RANDOM)/100" | bc`
PASSWORD3=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 5 | head -n 1)
PASSWORD4=`echo "(($RANDOM * $RANDOM)/$RANDOM)/100" | bc`
PASSWORD5=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 4 | head -n 1)
Q1="create database petelower_$DATABASENAME;"
Q2="grant all on petelower_"$DATABASENAME".* to 'wpdb_"$USERNAME"' identified by '"$PASSWORD1""$PASSWORD2""$PASSWORD3""$PASSWORD4""$PASSWORD5"';"
Q3="SHOW DATABASES;"
SQL="${Q1}${Q2}${Q3}"
echo $RANDOM
echo $SQL
echo " "
echo $DATABASENAME
echo " "
echo $USERNAME
echo " "
echo $PASSWORD1$PASSWORD2$PASSWORD3$PASSWORD4$PASSWORD5
echo " "
echo " "
echo "$SQL" | mysql -u root -p
echo " "
echo " "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment