(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



# require one argument on the command line

if [ $# -ne 3 ]; then

echo "Usage: createdb.sh dbname dbuser dbpassword";
exit 1; 





#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


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!

  • Why in your script are you assigning $1 to dbname, etc then reusing $1? In your create line, use create database ${dbname}; grant all on ${dbname}.* to '${dbuser}' identified by '${dbpassword}'. Also, you don't need to use flush privileges after the grant statement. You only need to do that if you modify the mysql.* tables directly. – slillibri Jun 17 '11 at 13:20
  • I will do that. Still I don't know why this is happening though? – Botha L Jun 17 '11 at 13:55

You should use following

mysql --user root --password=dumbpassword


mysql -u root -pdumbpassword

Note that there is no space between -p/--password and password itself.


The correct format for logging in to mysql is:

mysql -u username -p mydbname

This allows you to log in to the database "mydbname". Instead you have passed the password as the database name, so mySQL is telling you that it cannot find the database "dumbpassword"

Try this:

mysql -u username -p

This will allow you to login in to mysql after prompting for the password, and then you can choose the database with :

use mydbname

If you run mysql without the u/p switches, it will pick up the username and password from the current context.

  • My answer is not incorrect. – cone Jun 18 '11 at 9:03
  • How does it pick up the password from the currently logged in user? – Kyle Smith Jul 2 '11 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.