Create user with assigned DB script

#!/bin/bash
echo-n "Enter the MySQL root password: "
read-s rootpw
echo-n "Enter database name: "
readdbname
echo-n "Enter database username: "
readdbuser
echo-n "Enter database user password: "
readdbpw
db="create database $dbname;GRANT ALL PRIVILEGES ON $dbname.* TO $dbuser@localhost IDENTIFIED BY '$dbpw';FLUSH PRIVILEGES;"
mysql -u root -p$rootpw -e "$db"
if[ $? != "0"]; then
 echo"[Error]: Database creation failed"
 exit1
else
 echo"------------------------------------------"
 echo" Database has been created successfully "
 echo"------------------------------------------"
 echo" DB Info: "
 echo""
 echo" DB Name: $dbname"
 echo" DB User: $dbuser"
 echo" DB Pass: $dbpw"
 echo""
 echo"------------------------------------------"
fi
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s