Backing Up MySQL Databases Using mysqldump
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
MySQL (and MariaDB) include the mysqldump utility to simplify the process to create a backup of a database or system of databases. Using
mysqldump creates a logical backup and generates the SQL statements needed to reproduce the original database structure and data.
Before You Begin
Obtain the connection details for the MySQL instance you wish to use. If you do not have a MySQL instance yet, you can create a Managed Database, deploy the MySQL Marketplace App, or install MySQL server (or MariaDB) on a Compute Instance.
Log in to the system where you intend to capture or store your backups. This system needs a MySQL command-line client installed (which should come with the mysqldump utility). Run the following command to verify that mysqldump is installed:
This should inform you which version you are using as well, needed when referencing the documentation. If mysqldump and mysql are not installed, see the Installing MySQL guide.
Ensure your MySQL user has proper grants: The MySQL user you intend to use to export your existing database must have
SHOW VIEW, and
General mysqldump Syntax
The following list represents mysqldump commands for various scenarios. Within the commands,
[options] represents all of the command options required to perform the backup according to your own needs. See Common Command Options for a list of available options.
mysqldump [options] [database_name] > backup.sql
Specific tables in single database:
mysqldump [options] [database_name] [table_name] > backup.sql
Multiple specific databases:
mysqldump [options] --databases [database1_name] [database2_name] > backup.sql
mysqldump [options] --all-databases > backup.sql
ImportantDo not use the
--all-databasesoption if you intend on restoring this database to a Linode MySQL Managed Database. It may delete existing users and restrict access to your database.
--quickoption to receive rows one at a time instead of all at once.
Common Command Options
The following list is a collection of common options used with the mysqldump command. At minimum, the username and password is required. When connecting to a remote database server, the host (and perhaps the port) should be provided. For a full list of available options, reference the Option Syntax documentation.
-u ): The username of your MySQL user. This user must have proper grants to access the database.
-p): Specifies that the user’s password is required for the connection. The password can be entered directly in the command itself (though that is not recommended due to security concerns) or the password can be omitted (by just using the
--passwordoption with no value). In the password is omitted, mysqldump prompts you for the password before connecting to the database. For more details about password security, see MySQL’s End-User Guidelines for Password Security.
-h ): The IP address or hostname of the remote database server. You can omit this option from the command if you are connecting to a local MySQL instance on your same system.
-P ): The port number of that the MySQL database instance uses. This can be omitted if your MySQL instance uses the default port of
Output file (
> backup.sql): The name of the output file. To keep your backups organized with unique filenames, it may be helpful to add an automatically generated timestamp (ex:
backup-$(date +%F).sqlfor just the date or
backup-$(date +%Y%m%d-%H%M%S).sqlfor the date and time). You can reference the formatting options on the date manual page to further customize it.
If you are frequently backing up a database with mysqldump or running a backup through a cron job, you can securely store many of these options (including the password). See the Securely Storing Credentials guide. Other options can be stored in an option file.
--single-transaction: Issue a BEGIN SQL statement before dumping data from the server. See –single-transaction.
-q: Enforce dumping tables row by row. This provides added safety for systems with low memory and for large databases where storing tables in memory could become problematic. See –quick.
--lock-tables=false: Do not lock tables for the backup session. See –lock-tables.
--ssl-mode=REQUIRED: Force SSL when your existing database has SSL enabled. See Command Options for Encrypted Connections.
--set-gtid-purged=OFF: Use this option if you have GTID-based replication enabled. See –set-gtid-purged.
Single database on a local system: The following command backs up a single database called SampleDatabase from a MySQL instance on a local system. The user is exampleuser and the
-poption without a given value indicates mysqldump should prompt for the password. The backup filename includes a timestamp.
mysqldump -u exampleuser -p --single-transaction SampleDatabase > backup-$(date +%F).sql
Single database on a remote system: This command again backs up a single database, only this time the MySQL instance is on a remote system. The host in this example is an IP address of 192.0.2.1, though a domain name could also be provided. Provided the remote database uses port 3306, no port needs to be specified (as is the case in this example).
mysqldump -h 192.0.2.1 -u exampleuser -p --single-transaction SampleDatabase > backup-$(date +%F).sql
Single database on a Linode MySQL Managed Database: In this example, the mysqldump command is used to backup a database called Test in a Linode MySQL Managed Database cluster. See Connect to a MySQL Managed Database guide for instructions on viewing the connection details (including the username, password, host, and port).
mysqldump -h lin-1111-1111-mysql-primary.servers.linodedb.net -u linroot -p --single-transaction --set-gtid-purged=OFF Test > backup-$(date +%F-%H.%M.%S).sql
Automate Backups with cron
To schedule regular backups of your database, you can use the mysqldump command inside of a cron job.
Log in to the system where you wish to capture and store your backups. This system should likely be a remote / cloud-based Linux server that is always running and should have a MySQL client installed.
Store your database credentials and connection details using the
mysql_config_editor setcommand. An example command is provided below, though be sure to replace the values with your own. See Securely Storing Credentials guide for additional details and options.
mysql_config_editor set --login-path=[name] --user=[username] --host=[host] --password --warn
Create the folder where you wish to store your backup files. This can be anywhere that is accessible by your user.
Edit your user’s crontab file.
Add the cron job to your crontab file. In the example below, replace [name] with the login path name you wish to use and [database-name] with the database you wish to back up. Edit the location where you are storing the backups, as well as any other options, as needed.
0 1 * * * /usr/bin/mysqldump --login-path=[name] --single-transaction [database-name] > ~/database-backups/backup-$(date +%F-%H.%M.%S).sql
For more information on cron, see our Using Cron guide or review the cron(8) and cron(5) manual pages.
Restore a Backup
The restoration command’s general syntax is:
mysql -u [username] -p [databaseName] < [filename].sql
Restore an entire DBMS backup. You will be prompted for the MySQL root user’s password:
This will overwrite all current data in the MySQL database system
mysql -u root -p < full-backup.sql
Restore a single database dump. An empty or old destination database must already exist to import the data into, and the MySQL user you’re running the command as must have write access to that database:
mysql -u [username] -p db1 < db1-backup.sql
Restore a single table, you must have a destination database ready to receive the data:
mysql -u dbadmin -p db1 < db1-table1.sql
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on