当前位置:网站首页>Master-slave replication principle of MySQL

Master-slave replication principle of MySQL

2022-07-07 06:58:00 Rookie ~ ~


In the actual production environment , If the mysql The read and write of the database are operated in a database server , Whether it's in security 、 High availability , Or high concurrency and other aspects can not meet the actual needs , General Synchronize data through master-slave replication , Then through read-write separation to improve the concurrent load capacity of the database .

One 、 Master slave replication concept

Master slave replication is MySQL Basic technology provided , Master slave replication process :binlog Binary log ( In addition to querying, other changes and related operations will be recorded in binlog Inside )、relay log Logs and three threads (master A thread and slave Two threads of ).

  • Main library (master) foreign Provide data addition, deletion, modification and query services , The modification of data involved in the main database will be written in binlog
  • Slave Library (slave) be used for Data synchronization and data backup , From the main library through special threads binlog Put the main database and data 、 jurisdiction 、 The changes related to the table structure are synchronized to the slave library , It means that all modifications made on the master database will be reflected on the slave database through the master-slave replication mechanism .

The benefits of master-slave replication : The data backup , We can even make a hot backup for it , It is through MySQL middleware mycat, Can achieve disaster , Disaster tolerance also reflects High availability .
disaster : If the main library hangs , Middleware agent mycat Automatically map service requests to slave libraries , The slave library continues to provide services , It shows high availability ( The back-end service allows certain exceptions to occur , But the back-end architecture services should be fault-tolerant , Deal with these abnormal errors , And provide normal services again )

Two 、 The concept of separation of reading and writing

Read write separation is based on master-slave replication . In the actual application environment , It must be that there are many reading operations , It's like we buy things on the e-commerce platform , Maybe I did 100 I just bought oneortwo . So there are always many more read operations than write update operations . So we configure the read-write separation based on master-slave replication , It is to let a master database be specially used for data modification , When writing, write it on the main database , The master database changes the data through master-slave replication binlog Sync to the slave Library , Then other client query requests will eventually be mapped to the slave Library , And we have a master database with twoorthree slave databases , The main database is specially used to update data ( Write operations ), The slave library is specially used for reading operations, which can well share the pressure of reading and writing , Don't concentrate all on the main database , The concurrent processing ability of back-end services has been greatly improved , The other is its high availability disaster tolerance , When the main library hangs up , You can change the specified slave library into the master library .
 Insert picture description here
In the picture above binlog, Even if we don't have master-slave replication , Also can write binlog Of , Only master-slave replication is through binlog To achieve .

3、 ... and 、 Master and slave Libraries

1. Main library

Main library master The server creates a binlog Dump thread , Send binary log contents to the slave server

2. Slave Library

  • There is a special one from the library I/O Threads Specially read and receive the content sent by the main database , It will send it from the main database binlog The content is received and written to a relay log Inside , The relay log is equivalent to a buffer , such master You don't have to wait slave The next event will not be sent until the execution is completed . It's not the main library binlog Read the content and execute it directly , In this way, the main database is the place where the direct implementation is not good binlog There may be a lot of content , And received from the library binlog Content execution may be slow , As a result, the gap between the updated data from the slave database and the master database will become larger and larger . Data replication may be relatively backward .
  • There will also be one from the library SQL Threads , From Relay log read Corresponding operation , be-all SQL Do it all once , That's it Synchronization of slave library content and master library content

Four 、 Master slave replication process

Master slave replication process : Two logs (binlog Binary log and relay log journal ) And three threads (master A thread and slave Two threads of ).

  • The update operation of the main library is written to binlog Binary log in ( The main library needs to be opened binlog switch )
  • master The server creates a binlog Dump thread , Send binary log contents to the slave server
  • slave Machine execution START SLAVE The command will create a... From the server IO Threads , receive master Of binary log Log copied to it ( In memory , Read and write fast ). First slave Start a worker thread (I/O Threads ),I/O Threads will actively connect master , Then the main library will open dump Threads ,dump The thread from master Of binlog Read the event in and send it to slave Of I/O Threads , If dump The thread has caught up with master( On the main library dump The thread has put binlog The content of is finished , And on the main library binlog No more content ),dump The thread will sleep and wait binlog Create new events ,slave Of I/O Events received by the thread are written to the relay log
  • slave Of SQL Threads The last step in the process ,SQL The thread from relay log Read events in , And execute the event update slave The data of , Make it relate to master Data synchronization for . as long as SQL Threads and I/O Threads are consistent , The relay log is usually located at os In cache , So the overhead of relay log is very small

5、 ... and 、 Master slave copy effect display

linux Upper centos As main storehouse win10 Upper mysql server As a slave library to demonstrate :
 Insert picture description here
Master slave replication is one-way synchronization ,master Changes to slave Sync . When configuring master-slave replication , The data of the two libraries may be different , Start by configuring master-slave replication , All changes in the master library will be synchronized to the slave library .

master establish mytest database
 Insert picture description here
see slave, Find out mytest Synced
 Insert picture description here
master Created user surface ,slave Also synchronized user surface
 Insert picture description here
 Insert picture description here
Now? linux Terminal MySQL(master) Delete mytest library
 Insert picture description here
here slave Of mytest There is no more

show processlist You can see master Worker threads in the current environment

 Insert picture description here
see slave Worker threads in the current environment
 Insert picture description here

原网站

版权声明
本文为[Rookie ~ ~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070239401437.html