Home / Programming / Bash / Bash script to create MySQL database and user

Bash script to create MySQL database and user

I don’t know about you, but often times I find myself needing to create a MySQL database and I simply forget all of the commands. This is really something that I should remember, but for some strange reason I just never am able to remember it. So like any other admin would do, you script it out to make it easier on yourself. This bash script will allow you to create new databases on the fly quickly and easily. This has been nothing but a time-saver.


#!/bin/bash

echo -n "Enter the MySQL root password: "
read -s rootpw
echo -n "Enter database name: "
read dbname
echo -n "Enter database username: "
read dbuser
echo -n "Enter database user password: "
read dbpw

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"
 exit 1
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

About geoff

Intelligence is the ability to avoid doing work, yet getting the work done. ~Linus Torvalds

One comment

  1. thanks man.. one of best script ever seen

Leave a Reply

Your email address will not be published. Required fields are marked *

*