Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading MySQL dump from command line

I am moving away from Linode because I don't have the Linux sysadmin skills necessary; before I complete the transition to a more noob-friendly service, I need to download the contents of a MySQL database. Is there a way I can do this from the command line?

like image 304
Phillip Copley Avatar asked Nov 21 '12 00:11

Phillip Copley


People also ask

How do I export a MySQL database dump?

Step 1 — Exporting a MySQL or MariaDB Database You will need your database's name and credentials for an account whose privileges allow at least full read-only access to the database. Use mysqldump to export your database: mysqldump -u username -p database_name > data-dump.


1 Answers

You can accomplish this using the mysqldump command-line function.

For example:

If it's an entire DB, then:

   $ mysqldump -u [uname] -p db_name > db_backup.sql 

If it's all DBs, then:

   $ mysqldump -u [uname] -p --all-databases > all_db_backup.sql 

If it's specific tables within a DB, then:

   $ mysqldump -u [uname] -p db_name table1 table2 > table_backup.sql 

You can even go as far as auto-compressing the output using gzip (if your DB is very big):

   $ mysqldump -u [uname] -p db_name | gzip > db_backup.sql.gz 

If you want to do this remotely and you have the access to the server in question, then the following would work (presuming the MySQL server is on port 3306):

   $ mysqldump -P 3306 -h [ip_address] -u [uname] -p db_name > db_backup.sql 

It should drop the .sql file in the folder you run the command-line from.

EDIT: Updated to avoid inclusion of passwords in CLI commands, use the -p option without the password. It will prompt you for it and not record it.

like image 109
nickhar Avatar answered Oct 19 '22 03:10

nickhar