This article describes how to back up a MySQL database. We all know that mysqldump uses an SQL-level backup mechanism. Its main function is to export data tables into SQL script files, it is suitable for upgrading between different MySQL versions, which is also the most common MySQL database backup method.
Here are some main parameters of mysqldump:
Compatible = name
It tells mysqldump that the exported data will be compatible with the database or the old MySQL server. The values can be ansi, mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb, no_key_options, no_tables_options, and no_field_options. Separate them with commas. Of course, it does not guarantee full compatibility, but is as compatible as possible.
Complete-insert,-c
The exported data adopts the complete INSERT method containing the field name, that is, all values are written in one row. This can improve the insertion efficiency, but may be affected by the max_allowed_packet parameter, resulting in insertion failure. Therefore, you need to use this parameter with caution. At least I do not recommend this parameter.
Default-character-set = charset
Specifies the character set used for data export. If the data table does not use the default latin1 character set, this option must be specified during data export. Otherwise, garbled characters will occur after data is imported again.
Disable-keys
Tell mysqldump to add/* at the beginning and end of the INSERT statement /*! 40000 alter table table disable keys */; And /*! 40000 alter table table enable keys */; Statement, which greatly improves the speed of the insert statement because it re-creates the index after all data is inserted. This option is only applicable to MyISAM tables.
Extended-insert = true | false
By default, mysqldump enables the complete-insert mode, so if you do not want to use it, use this option to set its value to false.
Hex-blob
Export binary string fields in hexadecimal format. This option is required if binary data exists. The affected field types include BINARY, VARBINARY, and BLOB.
Lock-all-tables,-x
Before starting the export, submit a request to lock all tables in all databases to ensure data consistency. This is a global read lock and the single-transaction and lock-tables options are automatically disabled.
Lock-tables
It is similar to lock-all-tables, but instead of locking all tables in the database. This option is only applicable to MyISAM tables. For Innodb tables, you can use the single-transaction option.
No-create-info,-t
Only export data without adding the create table statement.
No-data,-d
Only the database table structure is exported without exporting any data.
Opt
This is just a shortcut option. It is equivalent to adding the add-drop-tables add-locking create-option disable-keys extended-insert lock-tables quick set-charset option at the same time. This option allows the mysql database to quickly back up and export data, and the exported data can be quickly imported back. This option is enabled by default, but can be disabled with skip-opt. Note: If you run mysqldump without specifying the quick or opt option, the entire result set is stored in the memory. If you export a large database, problems may occur.
Quick,-q
This option is useful when exporting large tables. It forces mysqldump to retrieve records from the Server query and output them directly instead of caching all records into the memory.
Routines,-R
Export stored procedures and user-defined functions.
Single-transaction
This option submits a begin SQL statement before exporting data. BEGIN does not block any applications and ensures Database Consistency during export. It is only applicable to transaction tables, such as InnoDB and BDB.
This option and the lock-tables option are mutually exclusive, Because lock tables will implicitly commit any pending transactions.
To export a large table, use the quick option.
Triggers
Export the trigger at the same time. This option is enabled by default. Use skip-triggers to disable it.
For details about other parameters, see the manual. I usually use the following SQL to back up the MyISAM table:
- /usr/local/mysql/bin/mysqldump -uyejr -pyejr "default-character-set=utf8 opt extended-insert=false "triggers -R hex-blob -x db_name > db_name.sql
Use the following SQL to back up Innodb tables in a MySQL database:
- /usr/local/mysql/bin/mysqldump -uyejr -pyejr "default-character-set=utf8
opt extended-insert=false "triggers -R hex-blob single-transaction db_name > db_name.sql
In addition, if you want to implement online backup, you can also use the master-data parameter as follows:
- /usr/local/mysql/bin/mysqldump -uyejr -pyejr "default-character-set=utf8 opt master-data=1 "single-transaction flush-logs db_name > db_name.sql
It only requests the lock table at the beginning, then refreshes the binlog, and then adds the change master statement to the exported file to specify the binlog location of the current MySQL database backup, if you want to restore the file to slave, you can use this method.