Question

I am trying to export a database using mysqldump from command line. I am using the following syntax:

mysqldump -u root -ppassword databasename > outputfile.sql

I've tried several variations on this, but I always end up with the following as the contents of the output file:

Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]

I can get mysqldump to export all of my databases if I exclude the database name, but it will not export just a single database.

Am I overlooking something here?

Was it helpful?

Solution

Troubleshooting from the comments above:

That is correct syntax. I'd guess that mysqldump is picking up some other options somewhere. Maybe it's a shell alias with an option like -A included in the alias definition? Try running \mysqldump ... to run it un-aliased.

Your reply:

@BillKarwin you were on the right track with -A. I tried mysqldump --print-defaults and apparently --all-databases is in the default arguments. I ran it with --no-defaults and it worked like a charm.

The problem is that --all-databases was configured as a default option. When you try using that option together with an argument specifying one database, it outputs the usage error you described.

http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html says that all-databases can be either a command-line flag, or an option in the config file.

I'd suggest looking in your /etc/my.cnf or $HOME/.my.cnf for the all-databases option. It can appear either in the [mysqldump] group or the [client] group.

OTHER TIPS

How about (without the space between u and root)

mysqldump -uroot -ppassword databasename > outputfile.sql

Putting your root password in a command line is a really bad idea. At the very least, create a .my.cnf in your home directory, setting permissions to 600 (rw for you only) containing:

[mysqldump]
user=root
password=yourpassword

This will allow you to perform that particular command without a password. Since there's no particular reason your root user needs to be doing the dump, why not just create a user that can do this?

Presumably, you're doing the mysqldump to back things up. To make life even easier on you, set it in cron as in the example below which executes at midnight. Because of the presence of the .my.cnf file containing the password, it doesn't need a password in the command

0 0 * * * /usr/bin/mysqldump -u root -h localhost databasename > /home/someuser/outputfile.sql 2>&1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top