Reference document: https: //dev.mysql.com/doc/refman/5.7/en/replication.html
This article is a key step in the above document finishing, there is still time to see the document a comprehensive understanding better
A, Master configuration
In my.cnf
or my.ini
profile [mysqld]
add the following configuration components:
[mysqld]
# 服务器 ID,在 1和(2^32)-1 之间的正整数,不能和其他 MySQL 服务器ID重复
server-id=1
# 启用二进制日志,值为日志文件的前缀
log-bin=mysql-bin
# 为了在使用InnoDB事务的复制设置中实现最大的持久性和一致性
# 您应该在主 文件中启用下面两项配置
innodb_flush_log_at_trx_commit=1
sync_binlog=1
The last two above configuration can provide the highest data security, but will result in higher disk IO, therefore TPS minimum, need to be tested to seek a more suitable configuration according to their actual business use.
Parameter details: sysvar_innodb_flush_log_at_trx_commit
Second, create a user for replication
Create a user
CREATE USER 'repl'@'%.example.com' IDENTIFIED BY 'password';
Give permission
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.example.com';
Third, get binary log coordinates
In order to master the same, need to disable the host CRUD operations, to lock all of the tables read.
In the master execution:
FLUSH TABLES WITH READ LOCK;
Warning
let emitFLUSH TABLES
client-side statement to keep running, so read lock remains active. If you exit the client, it will release the lock.
Open a new session on the master, execute:
SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 73 | test | manual,mysql |
+------------------+----------+--------------+------------------+
Record File and Position information.
Fourth, the synchronization of existing data
If the master data already, you need to synchronize. Synchronization method is as follows.
4.1 Use mysqldump
In the command line:
$ mysqldump --all-databases --master-data > dbdump.db
Five, Slaves Configuration
5.1 release lock on the master
UNLOCK TABLES;
5.2 Configuring a slave server
Modification my.cnf
or my.ini
configuration:
[mysqld]
server-id=2
You need to ensure that server-id will not be repeated a plurality of slave
slave need to open log-bin
, if enabled, data backup can be carried out by the binary operation or a crash recovery log. Also as another slave's Master ( 主-从(主)-从
).
5.3 Setting the master information
Execute the following SQL:
mysql> CHANGE MASTER TO
MASTER_HOST='master_host_name',
MASTER_USER='replication_user_name',
MASTER_PASSWORD='replication_password',
MASTER_LOG_FILE='recorded_log_file_name',
MASTER_LOG_POS=recorded_log_position;
The above parameters 'xxx'
partially modified to the corresponding host name (the IP), with a copy of the user name and password, SHOW MASTER STATUS;
you see the file name of the binary and Position.
Data recovery master 5.4
If prior data derived in the foregoing, they should be introduced into the first data, the following command:
$ mysql -h master < dbdump.db
If in mysqldump
--single-transaction
automatically executes the above-described configuration, the import data.
5.5 Start Slave thread
Execute SQL:START SLAVE;
Sixth, practice
You can quickly deploy two vessels for learning by docker when a simple try.
Reference: https: //hub.docker.com/_/mysql
Docker host IP: 192.168.200.202
Ready to mount the directory structure is as follows:
/docker
└── mysql
├── master
│ ├── conf
│ │ └── my.cnf
│ └── data
└── slave
├── conf
│ └── my.cnf
└── data
6.1 Master
my.cnf
Profiles:
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
collation-server=utf8mb4_unicode_ci
character-set-server=utf8mb4
init-connect='SET NAMES utf8mb4'
max_connections=2000
lower_case_table_names=1
server-id=1
log-bin=mysql-bin
innodb_flush_log_at_trx_commit=1
sync_binlog=1
Here we set the character set for utf8mb4
By docker start the service:
docker run --name mysql-master \
-e MYSQL_ROOT_PASSWORD=root \
-p 3306:3306 \
-v /docker/mysql/master/conf:/etc/mysql \
-v /docker/mysql/master/data:/var/lib/mysql \
-d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
6.2 Slave
my.cnf
Profiles:
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
collation-server=utf8mb4_unicode_ci
character-set-server=utf8mb4
init-connect='SET NAMES utf8mb4'
max_connections=2000
lower_case_table_names=1
server-id=10
log-bin=mysql-bin
Here we set the character set for utf8mb4
By docker start the service:
docker run --name mysql-slave \
-e MYSQL_ROOT_PASSWORD=root \
-p 3307:3306 \
-v /docker/mysql/slave/conf:/etc/mysql \
-v /docker/mysql/slave/data:/var/lib/mysql \
-d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
6.3 Test
6.3.1 Prepare data
In the first implementation of SQL master: database structure .sql , which is to have existing data situation.
6.3.2. Backup master
Docker execute the following command to back up data:
$ docker exec mysql-master sh -c 'exec mysqldump --all-databases --master-data --single-transaction --include-master-host-port -uroot -proot' > /docker/mysql/dbdump.db
6.3.3. Restore slave
Docker execute the following command to restore the data:
$ docker exec -i mysql-slave sh -c 'exec mysql -uroot -proot' < /docker/mysql/dbdump.db
6.3.4 Creating a user copy
Create a user
CREATE USER 'slave'@'%' IDENTIFIED BY 'slave';
Give permission
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
Remind
the first time@'192.168.200.202'
appeared in the following slave stateSlave_IO_Running: Connecting
Slave_SQL_Running: YesThen the master log found:
[Note] Access denied for user ‘slave’@‘172.17.0.1’ (using password: YES)
IP is shown above the container does not have access to modify
@'%'
the state to normal.
6.3.5 slave configuration master
CHANGE MASTER TO
MASTER_HOST='192.168.200.202',
MASTER_USER='slave',
MASTER_PASSWORD='slave',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=15114;
Where MASTER_LOG_FILE
and MASTER_LOG_POS
values can be backed up on top of /docker/mysql/dbdump.db
SQL statements to see, can follow the previously locked to ensure that these two parameters do not change during the backup.
6.3.6 modify the master slave change view
Just CRUD data or add databases, tables, etc. to see the effect.
VII Summary
Long long ago did the master-slave configuration database, it was the article to learn to see other people, and now find different document configuration are different, so look at the official documentation from scratch operation again, MySQL official documentation is comprehensive, currently only saw a small part of the contents, and so the whole look to add, also recommended to see friends here look at the official documentation for more details.