MySQL Creating User & Grant Access


#!/bin/bash

Ask User to Enter Database Name
read -p “Please Enter Database Name:” dbname

Checking if Database Exist
mysql -Bse “USE $dbname” 2> /dev/null
if [ $? -eq 0 ]; then
read -p “Please enter the username you wish to create : ” username
read -p “Please Enter Host To Allow Access Eg: %,ip or hostname : ” host
read -p “Please Enter the Password for New User ($username) : ” password

MySQL Query Will Create New User & Grant Privileges
query=”GRANT ALL PRIVILEGES ON $dbname.* TO $username@’$host’ IDENTIFIED BY ‘$password'”;

Ask User to Confirm Entered Data
read -p “Executing Query : $query , Please Confirm (y/n) : ” confirm
if [ “$confirm” == ‘y’ ]; then
mysql -e “$query”

Update Privileges
mysql -e “flush privileges”
else
read -p “Aborted, Press any key to continue..”
fi
else
echo “The Database: $dbname does not exist, please specify a database that exists”;
fi

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s