When the main stock in how to complete the full amount of historical data from Mysql master copy
Previous article describes [docker + mysql from the master copy of the building] [1], the use of docker has many advantages: You can easily portable across platforms, do not affect each other docker container, inside the container does not pollute places host environment, and so on. If you have not had contact with docker, we recommended to go look at the tutorials docker, of course, will not affect the learning docker not solve the problem of general steps.
premise:
When deciding to use Mysql master-slave replication database schema, you may project has been running for some time, this is a very common scenario, such as the current project I am responsible, I would like to change the status quo single-node database production environment by from the master copy to pave the way for the separate read and write. Then you need two or more nodes as the database node, here not only refers to the so-called server node, or it may be the same server, different ports, use docker isolation.
But I found that you configure the master-slave replication, the master node existing data is not synchronized to the node when the primary node to write data to the database, repeat these operations from the node, you can simply do not create those master node from its own library , resulting in operation error, IO thread necrosis, ultimately hang from the node
Course start
Check the main library of existing database
show databases;
Hypothesis test in which the database is a lot of data that we want to master-slave synchronization database, simulate the production environment, the database has been
use test;
select * from user;
Lock the primary database
Lock the primary database, only allowed to write to read, the aim is to prevent the insertion of new data after a backup process or backup is completed, resulting in inconsistent backup data and the main data, again, this also has drawbacks, the lock may during the library will affect the normal business processes, so we should lock the database size as small as possible.
flush tables with read lock;
Query the main database state, and write down the value of FILE and Position
show master status;
Began to back up the master database
Exit mysql terminal, execute docker mysql backup command
docker exec [CONTAINER] /usr/bin/mysqldump -u username --password=xxx [DATABASE] > back.sql
[CONTAINER] is the name of your own vessel, [DATABASE] is the name of your database, back.sql temporary backup files generated, which is sql statement, username is your user name, usually root, xxx is your own user password. Means execution of the command is a command related to the docker container mysql: mysqldump, export the database to a designated host which
We only need to back up the test database, to back up the entire database, [DATABASE] at the use of--all-databases
Execute the following statement on the host
Figure warnming because we entered the password on the command line, so there will be a security warning message. It can be seen back.sql has been generated on the host,
Start importing from database
Prior to import a backup file, you need to establish the appropriate namesake library manually from the library CREATE DATABASE test;
, otherwise it will appear the following error
Can not find the relevant database
Perform the following statement after you have created a database manually
cat back.sql | docker exec -i [CONTAINER] /usr/bin/mysql -u username --password=xxx [DATABASE]
Backup success
Then you can start from the main configuration mode, and specifically refer to [main docker + mysql replication built from] my article [1]
Remember that you have configured to unlock the main library
unlock tables;