[MySQL] Master-slave replication and its configuration

1. Why do we need master-slave replication?

  • In a system with complex business, there is such a scenario: if there is a SQL statement required 锁表, resulting in temporary 不能使用读service, it will greatly affect the running business. Using master-slave replication, the master library is responsible for writing and the slave library is responsible for reading. In this way, even if the master library locks the table, the normal operation of the business can be guaranteed by reading the slave library.

  • for data热备

  • The business volume is increasing, 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 performed to reduce the frequency of disk I/O access and improve the I/O performance of a single machine.

2. What is master-slave replication?

MySQL master-slave replication means that data can be replicated from a MySQL database server master node to 一个or from a 多个slave node .

MySQL adopts the 异步复制method by default, so that the slave node does not have to visit the master server to update its own data, the data update can be performed on a remote connection, and the slave node can copy all the databases in the master database or a specific database, or a specific table.

3. Principle

  • master服务器The data changes are recorded in the binary binlog日志, and when the data on the master occurs 改变, the changes are written to the binary log.
  • slave服务器The master binary log will be detected at a certain time interval to see if it has changed, and if there is a change, a I/OThreadrequest master binary event will be started.
  • 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 启动SQL线程will read the binary log from the relay log and replay it locally so that its data is consistent with the master node.
  • Finally I/OThread and SQLThread will go to sleep, waiting for the next wake up.

Summarize:

  • 从库Generate two threads, one I/O thread, and one SQL thread, be sure to ensure that these two are 开启in the state.
  • I/O线程It will request the binlog of the main library, and write the obtained binlog to the local relay-log (relay log) file.
  • The main library will generate one to pass the binlog log dump线程to the slave library I/O thread .
  • The SQL thread will read relay logthe log in the file and parse it into SQL statements to execute one by one.

insert image description here

4. Configuration

4.1 Prepare two servers (MySQL has been installed)

master:xxx
slave:xxx

4.2 Modify the configuration

Two machines create a database with the same name at the same time.

create database test_db charset=utf8mb4;

4.2.1 master master server

  • turn upmy.cnf
sudo vim /etc/mysql/my.cnf

insert image description here

  • add this information
#在mysqld模块中添加如下配置信息
[mysqld]
log-bin=master-bin #二进制文件名称
binlog-format=ROW  #二进制日志格式,有row、statement、mixed三种格式,row指的是把改变的内容复制过去,而不是把命令在从服务器上执行一遍,statement指的是在主服务器上执行的SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高。mixed指的是默认采用基于语句的复制,一旦发现基于语句的无法精确的复制时,就会采用基于行的复制。
server-id=1            # 要求各个服务器的id必须不一样
binlog-do-db=test_db   # 同步的数据库名称
  • Configure the account authorization for the slave server to log in to the master server
grant replication slave on *.* to 'root'@'%' identified by 'root';
  • Refresh permissions
flush privileges;
  • restart the master server
service mysql restart
  • masterViewed in mysqlstatus
show master stataus

insert image description here

4.2.2 slave slave server

  • find the my.cnffile

insert image description here

  • configuration information

This server-idmust be remembered with the main server 不一样!
insert image description here

  • restart the mysqlservice
service mysqld restart
  • connect to the main server
change master to master_host='主服务地址',master_user='root',master_password='root',master_port=3306,master_log_file='master-bin.000001',master_log_pos=154;

This master_log_fileis show master statausthe information above.
This master_log_posis show master statausthe information above.

  • start slave
start slave
  • View slave
show slave status \G(注意没有分号)

insert image description here

When we look at these two, it YES's ok, be careful not to be the otherCONNECTING

  • stop slave
stop slave

4.2.3 Verification

  • mastermain server
    insert image description here

  • slavesub server

insert image description here
The state of both servers

First, we can master节点insert a statement on

insert image description here
And then look at slave节点it
insert image description here
so we see that it has been implemented 主从复制.

Guess you like

Origin blog.csdn.net/weixin_45304503/article/details/121255955