Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

3 Answers 3

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 6.1.2.1, "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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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