1. Copy the data from the master database to the slave database
This step is mainly for the master database that has been running for a period of time. Historical data needs to be imported into the slave database to ensure strong master-slave consistency.
Main database lock table stops writing operations
Execute in the main database MySQL command line
flush tables with read lock;
Export main database data
Use the mysqldump command on the command line of the host where the main library is located to export all data.
mysqldump -uroot -p --all-databases|gzip > all_db.sql.gz
Import data into slave library
Transfer the exported file to the host where the slave library is located, and execute the import. Be careful not to make a mistake in the file path
mysqldump -uroot -p --all-databases < all_db.sql
2. Configure master-slave synchronization
See the article for details:
MySQL master-slave synchronization (without GTID)_DK_521's blog-CSDN blog
Problems you may encounter:
1. Mysqldump gets stuck there and doesn’t move. What should I do?
solve:
When using mysqldump
backup, add a parameter: --single-transaction
, so that the data table can not be locked when backing up the database.
For example:
mysqldump -uroot -p --all-databases
--single-transaction
|gzip > all_db.sql.gz
Shoulders of Giants:
The main database already has data configured for MySQL8 master-slave synchronization.