MySQL logical backup refers to using SQL statements to back up the database's structure and data, rather than directly backing up the database files. Usually usedmysqldump
The tool performs logical backup.
1.mysqldump
mysqldump
It is the most commonly used logical backup tool, which can generate SQL script files that can be used to rebuild database structure and data.
Basic commands:
mysqldump -u [username] -p[password] [Database name] > [Backup files.sql]
Parameter options:
-
-u, --user=[name]
: Specify the MySQL username. -
-p, --password[=password]
: Specify the MySQL password. If no password is provided,mysqldump
It will be prompted to enter. -
-h, --host=[hostname]
: Specify the MySQL server host name. -
-P, --port=[port_num]
: Specify the MySQL server port number. -
--protocol=[{TCP|SOCKET|PIPE|MEMORY}]
: Specify the connection protocol. -
-r, --result-file=[file]
: Write the output directly to the file, not through standard output. -
-t, --no-create-info
: Do not write statements that create tables, only export data. -
-d, --no-data
: Do not write data, only export the database structure. -
--add-drop-table
: Add before each table statementDROP TABLE IF EXISTS
Sentence. -
--add-locks
: Use before and after inserting dataLOCK TABLES
andUNLOCK TABLES
Sentence. -
--disable-keys
: Use before inserting dataALTER TABLE ... DISABLE KEYS
, use after inserting dataALTER TABLE ... ENABLE KEYS
。 -
-B, --databases
: Specify the database to be backed up. If this option is used, all database names that follow will be backed up. -
-A, --all-databases
: Backup all databases. -
--tables
: Specifies the table to be backed up, which must be followed by the database name. -
-n, --no-create-db
: No statements that create database are written. -
-w, --where='condition'
: Only export rows that meet the specified criteria. -
--single-transaction
: Export all data in one transaction, suitable for tables that support transactions (such as InnoDB). -
--lock-tables
: Lock all tables before export, suitable for tables that do not support transactions (such as MyISAM). -
--master-data[=#]
: Add the binary log location and file name of the main server in the export to set up replication. -
--flush-logs
: Refresh the MySQL server logs before exporting. -
--routines
: Export stored procedures and functions. -
--triggers
: Export trigger. -
--events
: Export events. -
--hex-blob
: Export binary columns in hexadecimal format. -
--set-gtid-purged=[OFF|ON|AUTO]
: Controls whether to include GTID information in the backup, suitable for GTID replication.
Example
Backup the entire database
mysqldump -u root -p mydatabase > mydatabase_backup.sql
Backup multiple databases
mysqldump -u root -p --databases db1 db2 > multi_database_backup.sql
Back up all databases
mysqldump -u root -p --all-databases > all_databases_backup.sql
Backup the database structure only
mysqldump -u root -p --no-data mydatabase > mydatabase_structure_backup.sql
Backup only specific tables
mysqldump -u root -p mydatabase table1 table2 > mydatabase_tables_backup.sql
Add options to effectively handle lock table issues
mysqldump -u root -p --single-transaction --quick --lock-tables=false mydatabase > mydatabase_backup.sql
Recover data
Recover the database
grammar
mysql -u [username] -p[password] [Database name] < [Backup files.sql]
Example
mysql -u root -p mydatabase < mydatabase_backup.sql
Recover tables in the library
mysqldump -u root -p[password] [Library name] [Table name] > staff_backup.sql
Use source recovery
First log in to mysql and restore it using the following syntax in mysql
source File path;
Example:
source /path/to/employees_backup.sql;
Things to note
-
Permission Management: Ensure execution
mysqldump
The user with the recovery command has corresponding database read and write permissions. - Regular backup: Logical backups usually require high data consistency and integrity. It is recommended to regularly backup and verify the validity of the backup files.
- Storage location: Store backup files in a secure location to avoid loss or improper modification.
2. mysqlpump
mysqlpump
It is a backup tool introduced by MySQL 5.7. It supports multi-threading and can perform backups faster. It ismysqldump
Enhanced version.
Mysqlpump Features
-
Parallel processing:
mysqlpump
Supports concurrent backup of multiple tables and multiple databases, greatly improving backup speed. - More functional options: Supports filtering specific databases, tables and data, and compressing while exporting.
- Enhanced flexibility: Provide more parameters to customize backup operations.
Basic commands:
mysqlpump -u [username] -p[password] [Database name] > [Backup files.sql]
Common options:
-
--exclude-databases
: Exclude specific databases -
--include-databases
: Contains specific databases -
--default-parallelism
: Set the parallelism -
--skip-definer
: Ignore the DEFINER clause -
--set-gtid-purged
: Backup for GTID
Example
Backup a single database
mysqlpump -u root -p mydatabase > mydatabase_backup.sql
Backup multiple databases
mysqlpump -u root -p --databases db1 db2 > multi_database_backup.sql
Back up all databases
mysqlpump -u root -p --all-databases > all_databases_backup.sql
Concurrent processing
mysqlpump -u root -p --default-parallelism=4 --databases db1 db2 > multi_database_backup.sql
use--default-parallelism
The parameters can set the number of concurrent threads to speed up the backup speed.
Filter table
mysqlpump -u root -p mydatabase --exclude-tables=table1,table2 > mydatabase_backup.sql
use--exclude-tables
Specific tables can be excluded
Backup table structure only
mysqlpump -u root -p mydatabase --skip-dump-data > mydatabase_structure_backup.sql
Compress backup files
mysqlpump -u root -p mydatabase | gzip > mydatabase_backup.
Backup data directory
mysqlpump --users --routines --databases db1 db2 > backup_with_users_routines.sql
use--users
Back up user accounts and use--routines
Backup storage functions and stored procedures.
Recover the database
Unzip the backup file
gunzip < mydatabase_backup. | mysql -u root -p mydatabase
Restore backup
mysql -u [username] -p [Database name] < [Backup files.sql]
Example
mysql -u root -p mydatabase < mydatabase_backup.sql
Things to note
-
Permission Management: Ensure execution
mysqlpump
The user of the recovery command has the necessary database read and write permissions. - Concurrency: Set the number of concurrent threads reasonably to avoid excessive server load.
- Regular backup: It is recommended to regularly backup and verify the integrity of the backup files.
- Storage location: Store backup files in a safe and reliable location to avoid loss or improper modification.
Pros and cons of logical backup
advantage:
- Strong readability: Backup files are SQL scripts in text format, which are easy to view and edit.
- Cross-platform: Backup files can be ported between different operating systems and different versions of MySQL.
- Flexible backup: You can choose to back up the entire database, specific tables, or structures.
shortcoming:
- Large performance overhead: There will be large CPU and I/O overhead during backup and recovery, especially for databases with large data volumes.
- Slow recovery speed: Compared with physical backups, logical backups are slower to restore.
- Consistency Issue: If the transaction consistency option is not used correctly, data inconsistency may be caused.
Backup table data extensions only
/var/lib/mysql-files
is the directory MySQL uses to store exported files by default. This directory is usually usedSELECT INTO OUTFILE
The file generated by other operations.
mysqldump -n
introduce
mysqldump
is a tool for backing up MySQL databases.-n
Parameters allow you to only backup data in the table, excluding the table structure. The backup file is a pure SQL statement that can be used to restore data.
Give an example
Scene: There is a name calledemployees
database containingstaff
surface. Backupstaff
Data in tables, but not table structure.
step:
Go to the command line.
Execute the following command:
mysqldump -u root -p[password] --no-create-info employees staff > staff_data_only.sql
3. Results: Generatedstaff_data_only.sql
File content:
INSERT INTO `staff` (`id`, `name`, `position`, `salary`) VALUES (1, 'Alice', 'Manager', 50000); INSERT INTO `staff` (`id`, `name`, `position`, `salary`) VALUES (2, 'Bob', 'Engineer', 40000); INSERT INTO `staff` (`id`, `name`, `position`, `salary`) VALUES (3, 'Charlie', 'Technician', 30000);
recover:
- In the new environment, make sure that the same structure already exists
staff
surface. - Enter the MySQL client and execute:
source /path/to/staff_data_only.sql;
SELECT INTO OUTFILE
introduce
SELECT INTO OUTFILE
is an SQL statement used to export query results to a file. File formats can be defined as needed (such as CSV format or xlsx-end format), and are usually used for data analysis and transmission.
Give an example
1. Scene: There is a name calledemployees
database containingstaff
surface. Exportstaff
The data in the table is analyzed as a CSV file.
2. Steps:
Enter the MySQL client.
Execute the following SQL statement:
SELECT * FROM staff INTO OUTFILE '/var/lib/mysql-files/staff_data.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'; Command interpretation (you can also use the default, so there are no need for so many specified requirements statements later): -- Select all columns and rows from the staff table SELECT * FROM staff -- Write the query result to the file with the specified path INTO OUTFILE '/var/lib/mysql-files/staff_data.csv' -- The data for each field (column) is separated by commas FIELDS TERMINATED BY ',' -- The value of each field is surrounded by double quotes ENCLOSED BY '"' -- Each line of records is separated by newline characters LINES TERMINATED BY '\n';
3. Results: Generatedstaff_data.csv
File content:
"1","Alice","Manager","50000" "2","Bob","Engineer","40000" "3","Charlie","Technician","30000"
recover:
- In the new environment, make sure that the same structure already exists
staff
surface. - copy
staff_data.csv
File to the server. - Use the following SQL statements to import data:
LOAD DATA INFILE '/var/lib/mysql-files/staff_data.csv' INTO TABLE staff FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
Summarize
Different uses:
-
mysqldump -n
Mainly used to back up and restore MySQL database data. Generate SQL files to facilitate recovery of data when needed. -
SELECT INTO OUTFILE
Mainly used for data export for analysis or transmission. The generated file format such as CSV can be opened by tools such as Excel.
Recover the specified table:
-
mysqldump -n
Recovering data is easier, just load the source file directly into MySQL. -
SELECT INTO OUTFILE
The exported file format is flexible, but additional steps are required to import the data (such as usingLOAD DATA INFILE
)。
Convenience and flexibility:
-
mysqldump -n
Suitable for cross-platform environments, it is relatively convenient to restore databases. -
SELECT INTO OUTFILE
Applicable to export data for external analysis, with flexible format but slightly complex operation.
This is the end of this article about the implementation steps of MySQL logical backup. For more related contents of MySQL logical backup, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!