Home  >  Article  >  Database  >  What is master-slave replication in mysql? How to configure?

What is master-slave replication in mysql? How to configure?

青灯夜游
青灯夜游forward
2021-09-27 11:10:572321browse

This article is an advanced study of mysql. It introduces the principle and configuration of master-slave replication. I hope it will be helpful to everyone!

What is master-slave replication in mysql? How to configure?

For current systems, in systems with complex business, the database is often the bottleneck of the application. A single machine often cannot withstand the concurrency pressure of a large system. This Sometimes it is necessary to solve the bottleneck from the database aspect. For example, the SQL statement needs to lock the table, resulting in the temporary inability to use the read service, which will greatly affect the running business. After using the master-slave, the read operation of the slave library is not affected. [Related recommendations: mysql video tutorial]

1. Why is master-slave replication needed?

1, use master-slave replication, let the main library be responsible for writing, and the slave library is responsible for reading. In this way, even if the main library locks the table, the normal operation of the business can be guaranteed by reading from the slave library. .

2, architecture extension. The business volume is getting larger and larger, and the frequency of I/O access is too high, which cannot be satisfied by a single machine. At this time, multi-database storage is used to reduce the frequency of disk I/O access and improve the I/O performance of a single machine.

3. Multiple master and slave servers can also be used as data backup.

2. What is mysql master-slave replication?

MySQL master-slave replication means that data can be copied from a MySQL database server master node to one or more slave nodes. MySQL uses asynchronous replication by default, so that the slave node does not have to access the master server all the time to update its own data. Data updates can be performed on a remote connection. The slave node can copy all databases or specific databases or specific tables in the master database.

3. Mysql replication principle

Principle:

(1) The master server records the data changes in the binary binlog log. When the data on the master When a change occurs, the change is written to the binary log;

(2) The slave server will detect whether the master binary log has changed within a certain time interval. If a change occurs, an I will be started. /O Thread requests master binary events;

(3) At the same time, the master node starts a dump thread for each I/O thread to send binary events to it and save it to the local relay log of the slave node , the slave node will start the SQL thread to read the binary log from the relay log, and replay it locally to make its data consistent with that of the master node. Finally, I/OThread and SQLThread will enter the sleep state, waiting to be awakened next time.

也就是说:
从库会生成两个线程,一个I/O线程,一个SQL线程;
I/O线程会去请求主库的binlog,并将得到的binlog写到本地的relay-log(中继日志)文件中;
主库会生成一个log dump线程,用来给从库I/O线程传binlog;
SQL线程,会读取relay log文件中的日志,并解析成sql语句逐一执行;

Note:

  • master records the operation statement into the binlog log, and then grants the slave remote connection permission (the master must enable the binlog binary log function ; Usually for data security reasons, the slave also turns on the binlog function).
  • slave opens two threads: IO thread and SQL thread. Among them: the IO thread is responsible for reading the binlog content of the master into the relay log; the SQL thread is responsible for reading the binlog content from the relay log and updating it to the slave database, so as to ensure that the slave data and the master data are maintained Consistent.
  • Mysql replication requires at least two Mysql services. Of course, Mysql services can be distributed on different servers, or multiple services can be started on one server.
  • Mysql replication is best to ensure that the Mysql versions on the master and slave servers are the same (if the versions cannot be consistent, then ensure that the version of the master master node is lower than the version of the slave slave node)
  • master The time between the two nodes and slave needs to be synchronized.

What is master-slave replication in mysql? How to configure?

Figure source "High Performance MySQL"

Specific steps:

1. From the database Connect to the master library by manually executing the change master to statement, providing all the conditions for the connected user (user, password, port, ip), and letting the slave library know the starting point of the binary log (file name position number); start slave

2. Establish a connection between the IO thread of the slave library and the dump thread of the main library.

3. Based on the file name and position number provided by the change master to statement, the slave library initiates a binlog request to the master library from the IO thread.

4. The main library dump thread sends the local binlog to the slave library IO thread in the form of events according to the slave library's request.

5. Receive binlog events from the library IO thread and store them in the local relay-log. The transmitted information will be recorded in master.info.

6. Apply relay-log from the database SQL thread, and record the applied relay to relay-log.info. By default, the applied relay will automatically be purged.

四、mysql主从同步延时分析

mysql的主从复制都是单线程的操作,主库对所有DDL和DML产生的日志写进binlog,由于binlog是顺序写,所以效率很高,slave的sql thread线程将主库的DDL和DML操作事件在slave中重放。DML和DDL的IO操作是随机的,不是顺序,所以成本要高很多,另一方面,由于sql thread也是单线程的,当主库的并发较高时,产生的DML数量超过slave的SQL thread所能处理的速度,或者当slave中有大型query语句产生了锁等待,那么延时就产生了。

解决方案:

1.业务的持久化层的实现采用分库架构,mysql服务可平行扩展,分散压力。

2.单个库读写分离,一主多从,主写从读,分散压力。这样从库压力比主库高,保护主库。

3.服务的基础架构在业务和mysql之间加入memcache或者redis的cache层。降低mysql的读压力。

4.不同业务的mysql物理上放在不同机器,分散压力。

5.使用比主库更好的硬件设备作为slave,mysql压力小,延迟自然会变小。

6.使用更加强劲的硬件设备。

mysql5.7之后使用MTS并行复制技术,永久解决复制延时问题 这个后面文章在说下吧

五、主从复制的配置

1、基础设置准备

本次测试mysql的版本是5.7.  比较穷,在一台机子上装了两个mysql实例,修改下不同端口即可。 当然如果有钱准备两台能互相访问的机子安装两个mysql也是可以的。

测试阶段两个mysql实例IP相同都是本机(ip=127.0.0.1),区分下分别命名 主是node1,从是node2,端口不同 我实际测试用的是3306和3307)

2、安装mysql数据库

网上很多按照的例子,这里就不重复说了,请自行百度/google(结果是数据库能正常使用),待两台mysql都按照完成之后,我们开始配置主从复制了。

3、在两台数据库中分别创建数据库

--注意两台必须全部执行,两台的数据库保持相同
create database test;

4、在主(node1)服务器进行如下配置:

#修改配置文件,执行以下命令打开mysql配置文件
vi /etc/my.cnf
#在mysqld模块中添加如下配置信息
log-bin=master-bin #二进制文件名称
#二进制日志格式,有row、statement、mixed三种格式,
binlog-format=ROW  
server-id=1   #要求各个服务器的id必须不一样
binlog-do-db=test   #同步的数据库名称

二进制日志格式,有row、statement、mixed三种格式;   row指的是把改变的内容复制过去,而不是把命令在从服务器上执行一遍;statement指的是在主服务器上执行的SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高;  mixed指的是默认采用基于语句的复制,一旦发现基于语句的无法精确的复制时,就会采用基于行的复制。

5、配置从(node2)服务器登录主服务器的账号授权

--授权操作
set global validate_password_policy=0;
set global validate_password_length=1;
grant replication slave on *.* to 'root'@'%' identified by '123456';
--刷新权限
flush privileges;

6、从(node2)服务器的配置

#修改配置文件,执行以下命令打开mysql配置文件
vi /etc/my.cnf
#在mysqld模块中添加如下配置信息
log-bin=master-bin #二进制文件的名称
binlog-format=ROW  #二进制文件的格式
server-id=2 #服务器的id

7、重启主服务器的mysqld服务

#重启mysql服务
service mysqld restart
#登录mysql数据库
mysql -uroot -p
#查看master的状态
show master status;

What is master-slave replication in mysql? How to configure?

8、重启从服务器并进行相关配置

#重启mysql服务
service mysqld restart
#登录mysql
mysql -uroot -p
#连接主服务器(master_host是主的IP地址,我测试本地)
change master to master_host='127.0.0.1',master_user='root',master_password='123456',master_port=3306,master_log_file='master-bin.000001',master_log_pos=154;
#启动slave
start slave
#查看slave的状态
show slave status\G #(注意没有分号)

至此主从的配置已经完成,此时可以在主服务器进行相关的数据添加删除工作,在从服务器看相关的状态,查看对应数据有没有变化。

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of What is master-slave replication in mysql? How to configure?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete