mysqldump: Difference between revisions
No edit summary |
|||
| Line 1: | Line 1: | ||
[[mysqldump]] is a [[command line interface]] (CLI) utility provided with [[MySQL]]. The command is also provided by [[MariaDB]], where it is a link to [[mariadb-dump]]. | ==About== | ||
[[mysqldump]] is a [[command line interface]] (CLI) utility provided with [[MySQL]]. The command is also provided by [[MariaDB]], where it is a link to an executable called [[mariadb-dump]]. | |||
Its entire purpose in life is exporting data. (For importing data, see [[MySQL/migration/CLI/import]]. | |||
== | |||
===Local=== | ==Commands== | ||
The exact commands to use depend on whether you're exporting from a local or remote database. | |||
===Local DB=== | |||
'''Official MySQL documentation''': [http://dev.mysql.com/doc/refman/5.5/en/backup-and-recovery.html Chapter 6. Backup and Recovery] | '''Official MySQL documentation''': [http://dev.mysql.com/doc/refman/5.5/en/backup-and-recovery.html Chapter 6. Backup and Recovery] | ||
| Line 31: | Line 34: | ||
;SHOW CREATE DATABASE {{arg|dbname}}; : Returns the SQL code which could be used to recreate the database, ''not'' including the contents. | ;SHOW CREATE DATABASE {{arg|dbname}}; : Returns the SQL code which could be used to recreate the database, ''not'' including the contents. | ||
;SHOW CREATE TABLE {{arg|tablename}}; : Returns the SQL code which could be used to recreate{{arg|tablename}}, ''not'' including the data. | ;SHOW CREATE TABLE {{arg|tablename}}; : Returns the SQL code which could be used to recreate{{arg|tablename}}, ''not'' including the data. | ||
===Remote=== | ===Remote DB=== | ||
To backup data from a remote server onto the local machine, via direct connection: | To backup data from a remote server onto the local machine, via direct connection: | ||
mysqldump -h {{arg|remote_host}} -u {{arg|username}} -p {{arg|database_name}} | gzip > {{arg|local_backup_file}}.sql.gz | mysqldump -h {{arg|remote_host}} -u {{arg|username}} -p {{arg|database_name}} | gzip > {{arg|local_backup_file}}.sql.gz | ||
| Line 44: | Line 47: | ||
To close the tunnel, exit the ssh session in the first terminal window. | To close the tunnel, exit the ssh session in the first terminal window. | ||
==Links== | ==Links== | ||
* '''2009-01-06''' [https://blog.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/ PHP, MySQL and SSH Tunneling (Port Forwarding)] | * '''2009-01-06''' [https://blog.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/ PHP, MySQL and SSH Tunneling (Port Forwarding)] | ||
** Although this article is quite old, it still seems accurate as of December 2024. | ** Although this article is quite old, it still seems accurate as of December 2024. | ||
** ...specifically including the fact that PHP's ssh2 library does not appear to support using a different port locally than remotely (which can cause conflicts). | ** ...specifically including the fact that PHP's ssh2 library does not appear to support using a different port locally than remotely (which can cause conflicts). | ||
*** ...although actually, I think maybe it is doable by setting up the "context" resource properly. To Be Investigated. 2026-02-24 | |||
** That said, I've consistently had trouble trying to open tunnels with this methodology. | ** That said, I've consistently had trouble trying to open tunnels with this methodology. | ||
*** -- {{woozle}} 2024-12-04 | *** -- {{woozle}} 2024-12-04 | ||
Revision as of 01:55, 25 February 2026
About
mysqldump is a command line interface (CLI) utility provided with MySQL. The command is also provided by MariaDB, where it is a link to an executable called mariadb-dump.
Its entire purpose in life is exporting data. (For importing data, see MySQL/migration/CLI/import.
Commands
The exact commands to use depend on whether you're exporting from a local or remote database.
Local DB
Official MySQL documentation: Chapter 6. Backup and Recovery
For all of these examples:
- If you leave off the Template:Arg, it will prompt you for one.
- To connect to a MySQL server on localhost, omit -hTemplate:Arg. not tested
- see /remote for backing up from a remote server without storing the backup file remotely
From a shell command line:
- mysqldump --tab=Template:Arg -hTemplate:Arg -uTemplate:Arg -pTemplate:Arg --opt Template:Arg
- This saves SQL code to recreate the entire database in .sql files compatible with the restoring method given below.
- This will create one .sql file per table, in the folder specified by Template:Arg (which must already exist).
- mysqldump --tab=Template:Arg -hTemplate:Arg -uTemplate:Arg -p --opt Template:Arg Template:Arg Template:Arg Template:Arg
- This only dumps tables.
- mysqldump -hTemplate:Arg -uTemplate:Arg --opt Template:Arg > Template:Arg
- This dumps the entire database to a single file named Template:Arg (which will be created).
- mysqldump --all-databases -hhost -uuser -ppassword > name_of_dump_file.sql
- This backs up all databases to a single file.
Questions:
- Not sure what the
--optflag does. - I don't know if there's a way to dump all dbs to individual files.
To copy the dump file from one server to another, rsync generally works well.
For more specific backups, the following mysql client commands are relevant:
- SHOW CREATE DATABASE Template:Arg;
- Returns the SQL code which could be used to recreate the database, not including the contents.
- SHOW CREATE TABLE Template:Arg;
- Returns the SQL code which could be used to recreateTemplate:Arg, not including the data.
Remote DB
To backup data from a remote server onto the local machine, via direct connection:
mysqldump -h Template:Arg -u Template:Arg -p Template:Arg | gzip > Template:Arg.sql.gz
To backup data from a remote server onto the local machine via an ssh tunnel:
First, set up the tunnel:
ssh -L 3307:localhost:3306 Template:Arg@Template:Arg
That will open a ssh terminal session, as well as creating the tunnel. In a separate terminal session, run this:
mysqldump -P 3307 -h 127.0.0.1 -u Template:Arg -p Template:Arg > Template:Arg.sql
To close the tunnel, exit the ssh session in the first terminal window.
Links
- 2009-01-06 PHP, MySQL and SSH Tunneling (Port Forwarding)
- Although this article is quite old, it still seems accurate as of December 2024.
- ...specifically including the fact that PHP's ssh2 library does not appear to support using a different port locally than remotely (which can cause conflicts).
- ...although actually, I think maybe it is doable by setting up the "context" resource properly. To Be Investigated. 2026-02-24
- That said, I've consistently had trouble trying to open tunnels with this methodology.
- -- Woozle 2024-12-04
Notes
- Error message: "The MySQL server is running with the --secure-file-priv option so it cannot execute this statement"
- Meaning: MySQL has been configured to only allow imports or exports from within a specified folder.
- Solution: Check the secure_file_priv setting (can be found in MySQL Workbench under Server -> Status and System Variables -> System Variables tab; current Ubuntu default is "/var/lib/mysql-files/") to see where you should be putting your import/export files so as not to trigger this error. ...or you can stop the server, change the setting, restart, and try again.
