Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose that I want to access mysql server and print all tables and databases and their data in localhost using bash script.

The script I have looks like:

mysql -u root -p password

After the script is executed, the mysql shell opens instead of bash. So how can I run a command with mysql?

Also, what command should I execute to print out all databases, tables and their data?

Edit: I now get which command I should use, but still the question remains:

I need to make it into the script, but when entering mysqldump command, it asks for password interactively.

So how can I handle password in a bash script?

share|improve this question
up vote 1 down vote accepted

See mysqldump which does what you want.

Usage example:

mysqldump -u root -p --all-databases

See the manual for more switches to suit your particular situation.

share|improve this answer

try this..

mysqldump -u root -p databasename > filename.sql

all tables store in filename.sql. its like as backup.

and if you want to store tables back in database then use this command.

mysqldump -u root -p databasename < filename.sql
share|improve this answer

If you execute man mysqldump, you will get the following output:

--password[=password], -p[password]

The password to use when connecting to the server. If you use the short option form (-p), you cannot have a space between the option and the password. If you omit the password value following the --password or -p option on the command line, mysqldump prompts for one.

Specifying a password on the command line should be considered insecure. See Section, "End-User Guidelines for Password Security". You can use an option file to avoid giving the password on the command line.

Assuming your root password is PASSWORD, just try the following command:

mysqldump -u root -pPASSWORD --all-databases > database.sql
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .