Distributed topic|The interviewer asked me if I understand the principle of Mysql master-slave replication, can I say no?

Before setting up Mysql master-slave synchronization, let's talk about the process and principle of synchronization between them:

Synchronous replication process

Present a picture, this picture explains the whole synchronization process
Insert picture description here

Master-slave replication process:

  • The slave node connects with the master node, establishes a master-slave relationship, and sends the synchronization from where and which log file to the master.
  • The master saves the modified data to binlog
  • The master starts the binlog dump thread and pushes the binlog log to the connected slave
  • The slave receives the pushed binlog, and the slave starts the IO thread to write the data to the relay log (relay log)
  • The slave will also start a SQL thread at the same time, compare the new content in the relay log, parse the SQL, and replay the data to the slave database

Okay, the replication process is over. Let's quickly build a mysql master-slave architecture in practice.

That's right, I still use docker to build here, because it is very fragrant!

Build Mysql master and slave

Create mysql network

docker network create mysqlNet

Install mysql

  • Run mysql master node
docker run -p 3306:3306 --name mysql_master -h mysql_master --net=mysqlNet \
	-v ~/docker/mysql/mysql_master/log:/var/log/mysql \
	-v ~/docker/mysql/mysql_master/data:/var/lib/mysql \
	-v ~/docker/mysql/mysql_master/conf:/etc/mysql  \
	-e lower_case_table_names=1 \
	-e MYSQL_ROOT_PASSWORD=root \
	-d mysql:5.7
  • Modify the configuration file
# 编辑文件
vim ~/docker/mysql/mysql_master/conf

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection=utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
## 同一局域网内注意要唯一
server-id=1  
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin
  • Restart mysql_master
docker restart mysql_master
  • Create users on the master node to replicate from the node
docker exec -it mysql_master mysql -uroot -proot
# 创建用户
CREATE USER 'mysql_slave'@'%' IDENTIFIED BY '111111';
# 授权
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'mysql_slave'@'%';
  • Run mysql slave node
docker run -p 33306:3306 --name mysql_slave -h mysql_slave --net=mysqlNet \
	-v ~/docker/mysql/mysql_slave/log:/var/log/mysql \
	-v ~/docker/mysql/mysql_slave/data:/var/lib/mysql \
	-v ~/docker/mysql/mysql_slave/conf:/etc/mysql  \
	-e lower_case_table_names=1 \
	-e MYSQL_ROOT_PASSWORD=root \
	-d mysql:5.7
  • Edit slave node configuration file
# 编辑文件
vim ~/docker/mysql/mysql_master/conf

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection=utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
## 设置server_id,注意要唯一
server-id=2 
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin   
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
## 设置只读权限
read_only = 1
innodb_read_only
## 使得更新的数据写进二进制日志中
log_slave_updates = 1
#如果master库名与salve库名不同,使用以下配置[需要做映射]
#replicate-rewrite-db = [主库名] -> [从库名]
#如果不是要全部同步[默认全部同步],则指定需要同步的表
#replicate-wild-do-table=库名.表1
#replicate-wild-do-table=库名.表2

Now that the master and slave nodes have been installed, we need to let them establish a relationship:

First check the current status of the master node, mainly look at the log file and current location

docker exec -it mysql_master mysql -uroot -proot
show master status;
exit
exit

The results are as follows: the file is mysql-bin.000001 and the location is 629
Insert picture description here

After getting this information, we can now establish a relationship between the slave node and the master node and perform synchronization:

docker exec -it mysql_slave mysql -uroot -proot
change master to master_host='mysql_master', master_user='mysql_slave', master_password='111111', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos= 629, master_connect_retry=30;

Let's continue to look at the status of the slave node

show slave status\G;

Insert picture description here

We see that the two threads we mentioned at the beginning of the article are not started, so we need to manually start the two threads, just one command

start slave;

Insert picture description here

Now you can see that the two threads are already running.
At this point, it has been built

test

I will not demonstrate this. You can create a database on mysql_master and see if it will be synchronized to mysql_slave.

Wechat search for a search [Le Zai open talk] Follow the handsome me, reply [Receive dry goods], there will be a lot of interview materials and architect must-read books waiting for you to choose, including java basics, java concurrency, microservices, middleware, etc. More information is waiting for you.

Guess you like

Origin blog.csdn.net/weixin_34311210/article/details/109965472