Mysql replication from the primary analysis and build depth principle

First, in the docker mysql can be set up as a test used to build a database of learning, hub docker has been encapsulated in our complex steps to avoid mysql database installation, and are independent docker container, has its own ip and you can set a different port, the port will not cause conflict.

Install mysql environment docker

sudo docker pull  mysql:5.7

Do not just here to download the latest version, docker container in some packages of mysql service is not complete.

View docke images can be found mysql: 5.7 The image has been downloaded

Next image started as Master and slave

Master
docker run -p 3339:3306 --name master -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
Slave
docker run -p 3340:3306 --name slave -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

Mysql configuration from the master copy

View container ID

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
d34a83c63bbc        mysql:5.7           "docker-entrypoint.s…"   4 hours ago         Up 3 hours          33060/tcp, 0.0.0.0:3340->3306/tcp   slave
e46d81b5bfdf        mysql:5.7           "docker-entrypoint.s…"   4 hours ago         Up 3 hours          33060/tcp, 0.0.0.0:3339->3306/tcp   master

Enter docker

//进入 slave
sudo docker exec -it d3 bash

//进入 master
sudo docker exec -it e4 bash
  1. Configuration Master

vim /etc/mysql/mysql.conf.d/mysqld.cnf

Note: There is no need to install vim, to update apt-get apt-get install vim before installation

[mysqld]
## 同一局域网内注意要唯一
server-id=100  
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin

After the configuration server-id, you need to restart mysql

使用service mysql restart完成重启

sudo docker start master启动容器

Then create the Master database data synchronization user, the user grants permission slave REPLICATION SLAVE and REPLICATION CLIENT privilege for the primary synchronization between the data from the database.

CREATE USER 'slave'@'%' IDENTIFIED BY '123456';

GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';

position last view files

show master status;
Before the back of the operation is complete, the need to ensure Master library can not do anything, otherwise it will cause a state change, changes in the value of File and Position fields. Fill in the sql from the library
  1. Slave configuration
[mysqld]
## 设置server_id,注意要唯一
server-id=101  
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin   
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin 

Mysql into the slave of execution

CHANGE MASTER TO
MASTER_HOST='172.17.0.2',
MASTER_USER='slave',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=609,
MASTER_PORT=3306;
//启动主从复制
start slave;

Here master container by ip

sudo docker inspect --format='{{.NetworkSettings.IPAddress}}' master
进行查看
  1. test
mysql -h 127.0.0.1  -P 3339 -u root -p 123456
在主库创建一个数据库,查看从库是否也存在

create database http_system;

mysql -h 127.0.0.1  -P 3340 -u root -p 123456

show databases;

Mysql master-slave replication major scene

mysql default asynchronous replication is master-slave, and can specify a specific table copied from a library and a specific library from the library

Experience in sql operation, a SQL required to lock the entire table, causing temporary service can not be read, this will affect the existing work, separated from the main reading and writing, the main library to read, write from the library , can guarantee the normal operation of the business.

While ensuring high availability of the database to prevent data loss.

Copy the master-slave principle

mysql consists of three main threads to complete the copy:

log dump thread running in the primary node

I / O and SQL threads run from the node

  1. binary log dump thread is responsible for sending the contents of bin-log in to read bin-log of time, will be locked bin-log, finished reading it released.

  2. In the relay-log "start slave" from node to start an I / O thread to connect the main nodes, requesting master database updates bin-log, I / O process after receiving the master database updates bin-log saved from execution in a node in. And save the read binary log file name and location of the master-info file so that you can clearly tell Master "from which position I need a bin-log log back in to start the next time the content is read, please send to me"

  3. SQL thread is responsible for reading from the relay-log in, resolve to perform specific operations and ensure data consistency.

If a master multi-slave, Mysql will each create a log dump thread from the node, you must first open the Master end of the binary log (bin-log) function

mysql default is asynchronous mode, the user performs a log dump sql and no correlation

Explanation

Mysql Mysql master-slave replication is highly available, high-performance foundation, with this foundation, mysql deployment will become simple, flexible and diverse, which can respond flexibly adjusted according to different business scenarios.

Reproduced in: https: //www.jianshu.com/p/ded2c4ee4ae9

Guess you like

Origin blog.csdn.net/weixin_34273046/article/details/91111116