Skip to content

Instantly share code, notes, and snippets.

@thejsj
Last active January 1, 2016 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thejsj/8099370 to your computer and use it in GitHub Desktop.
Save thejsj/8099370 to your computer and use it in GitHub Desktop.
Another iteration into a script that creates a database and user
#!/bin/bash
params=" -uroot -p"
echo ' - - - - - - - '
echo 'DB Name : '$1
echo 'Password: '$2
# Create Database
echo ' - - - - - - - '
echo 'CREATE DATABASE IF NOT EXISTS $1;'
# Grant Options (Creates User Automatically)
echo ' - - - - - - - '
echo "GRANT ALL PRIVILEGES ON $1.*
TO '$1'@'%' IDENTIFIED BY '$2'
WITH GRANT OPTION;"
read -p "Do you wish to install this program? [Y/N] : " yn
if [ $yn = "y" -o $yn = "Y" ]; then
# Create Database
mysql $params <<DELIMITER
CREATE DATABASE IF NOT EXISTS $1;
DELIMITER
# Grant Options (Creates User Automatically)
# http://stackoverflow.com/questions/4528393/mysql-create-user-only-when-the-user-doesnt-exist
mysql $params <<DELIMITER
GRANT ALL PRIVILEGES ON $1.*
TO '$1'@'%' IDENTIFIED BY '$2'
WITH GRANT OPTION;
DELIMITER
mysql $params <<DELIMITER
GRANT ALL PRIVILEGES ON $1.*
TO '$1'@'localhost' IDENTIFIED BY '$2'
WITH GRANT OPTION;
DELIMITER
mysql $params <<DELIMITER
GRANT ALL PRIVILEGES ON $1.*
TO '$1'@'127.0.0.1' IDENTIFIED BY '$2'
WITH GRANT OPTION;
DELIMITER
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment