When Mysql is synchronizing databases and tables, you can first use the mysqldump command to back up the databases and tables, and then import the backed-up sql into the new database to complete the data synchronization.
1. Backup
1. Backup command
Format: mysqldump -h host name-P port-u user name-p password-database database name> file name.sql
For example: mysqldump -h 192.168.1.100 -P 3306 -u root -p password --database cmdb> /data/backup/cmdb.sql
2. Backup compression
If the exported data is likely to be large and it is not easy to back up to the remote, then it needs to be compressed
Format: mysqldump -h hostname-P port-u username-p password--database database name | gzip> file name.sql.gz
For example: mysqldump -h 192.168.1.100 -P 3306 -u root -p password --database cmdb | gzip> /data/backup/cmdb.sql.gz
3. Back up multiple tables in the same database
Format: mysqldump -h hostname-P port-u username-p password--database database name table 1 table 2…> file name.sql
For example, mysqldump -h 192.168.1.100 -P 3306 -u root -p password cmdb t1 t2> /data/backup/cmdb_t1_t2.sql
4. Back up multiple libraries at the same time
Format: mysqldump -h host name -P port -u user name -p password --databases database name 1 database name 2 database name 3> file name.sql
例如:mysqldump -h 192.168.1.100 -P 3306 -u root -p password --databases cmdb bbs blog > /data/backup/mutil_db.sql
5. Back up all databases on the instance
Format: mysqldump -h hostname-P port-u username-p password--all-databases> file name.sql
例如:mysqldump -h 192.168.1.100 -P 3306 -u root -p password --all-databases > /data/backup/all_db.sql
6. Back up the database structure without backing up data
Format: mysqldump -h host name -P port -u user name -p password --no-data database name 1 database name 2 database name 3> file name.sql
or
format: mysqldump -h host name -P port -u user Name-p password-d database name 1 database name 2 database name 3> file name.sql
例如:mysqldump -h 192.168.1.100 -P 3306 -u root -p password --no-data –databases db1 db2 cmdb > /data/backup/structure.sql
mysqldump -h 192.168.1.100 -P 3306 -u root -p password -d –databases db1 db2 cmdb > /data/backup/structure.sql
Remark
1: --no-data or -d, with this parameter means that only the table structure is backed up, without this parameter means that the table structure and data are backed up
2: If the mysqldump command does not add environment variables, the command needs to bring the mysqldump command path
Two, synchronization
mysql -h hostname-P port-u username-p password<file name.sql
Note: If the mysql command does not add environment variables, the command needs to bring the mysql command path