(My first question here, so if I am flouting any conventions, I apologize in advance!)
This is my first attempt at creating a shell script. I am trying to create new databases and users and passwords by specifying them at the command line, along with the shell script. My root username and password are specified within the shell script. Here is my shell script which does this:
#! /bin/sh
# creates tables
masteruser="root"
masterpassword="dumbpassword"
# require one argument on the command line
if [ $# -ne 3 ]; then
echo "Usage: createdb.sh dbname dbuser dbpassword"; exit 1;
fi
dbname="$1"
dbuser="$2"
dbpassword="$3"
#echo $dbname
#echo $dbuser
#echo $dbpassword
mysql -u "$masteruser" -p"$masterpassword" <<EOF
create database $1; grant all on $1.* to '$2' identified by '$3';
flush privileges
EOF
I am running this from commandline like this:
./shellscriptname.sh newdbname newdbuser newdbpassword
After running this script (which creates databases successfully) multiple times, when I try to connect, this happens:
# mysql --user root --password dumbpassword
Enter password: ERROR 1049 (42000): Unknown database 'dumbpassword'
I don't know why this is happening. (Note: there is no semicolon after flush privileges)
Why is it no longer working? I am curious, since I cannot find any problems with my script. Neither is this working:
mysql -u root -p dumbpassword
I am more interested in finding out why this happened, than in solving the issue. So I thought I will ask the gurus here!