The implementation principle of Mysql master-slave synchronization

The implementation principle of Mysql master-slave synchronization

1. What is MySQL master-slave synchronization?

When the data in the master database changes, the changes will be synchronized to the slave database in real time.

2. What are the benefits of master-slave synchronization?

  • Horizontally scale the database load capacity.
  • Fault tolerance and high availability. Failover/High Availability
  • Data backup.

3. What is the principle of master-slave synchronization?

First, let's understand the master-slave architecture.

As shown below:

Whether it is delete, update, insert, or creating functions and stored procedures, all operations are performed on the master. When the master has operations, the slave will quickly receive these operations and synchronize them.

But how is this mechanism implemented?

On the master machine, master-slave synchronization events are written to a special log file (binary-log); on the slave machine, the slave reads the master-slave synchronization events and makes corresponding changes on the slave library based on the changes in the read events.

In this way, master-slave synchronization is achieved!

Let’s take a closer look below.

3.1 What are the master-slave synchronization events?

It is mentioned above:

On the master machine, master-slave synchronization events will be written to a special log file (binary-log);

There are three forms of master-slave synchronization events: statement, row, and mixed.

  1. statement: The SQL statements for database operations will be written into the binlog.
  2. row: Each data change will be written to the binlog.
  3. mixed: a mixture of statement and row. Mysql decides when to write statement format and when to write row format binlog.

3.2 Operations on the master machine

When the data on the master changes, the event (insert, update, delete) changes will be written to the binlog in sequence.

binlog dump thread

When the slave connects to the master, the master machine will start the binlog dump thread for the slave. When the master's binlog changes, the binlog dump thread will notify the slave and send the corresponding binlog content to the slave.

3.3 Operations on the slave machine

When master-slave synchronization is turned on, two threads will be created on the slave.

  • I/O thread. This thread is connected to the master machine, and the binlog dump thread on the master machine sends the contents of the binlog to the I/O thread. After receiving the binlog content, the I/O thread writes the content to the local relay log.
  • SQL thread. This thread reads the relay log written by the I/O thread. And perform corresponding operations on the slave database according to the contents of the relay log.

3.4 How to view the above threads on the master and slave?

You can view it using the SHOW PROCESSLIST command.

As shown in the figure, view the binlog dump thread on the master machine.

As shown in the figure, check the I/O and SQL threads on the slave machine.

4. Having said so much, let me summarize it with a picture

5. About actual combat

For actual combat, please refer to my other article: MySQL master-slave synchronization actual combat

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL master-slave synchronization principle and application
  • Master-slave synchronization configuration of Mysql database
  • This article will show you the principle of MySQL master-slave synchronization
  • MySQL master-slave synchronization mechanism and synchronization delay problem tracking process
  • Detailed explanation of Mysql master-slave synchronization configuration practice
  • How to set up master-slave synchronization in MYSQL database

<<:  JavaScript code to implement Weibo batch unfollow function

>>:  Vant+postcss-pxtorem implements browser adaptation function

Recommend

Discussion on the Issues of Image Button Submission and Form Repeated Submission

In many cases, in order to beautify the form, the ...

Tutorial on downloading, installing, configuring and using MySQL under Windows

Overview of MySQL MySQL is a relational database ...

Linux C log output code template sample code

Preface This article mainly introduces the releva...

Delegating Privileges in Linux Using Sudo

Introduction to sudo authority delegation su swit...

Linux/Mac MySQL forgotten password command line method to change the password

All prerequisites require root permissions 1. End...

In-depth analysis of nginx+php-fpm service HTTP status code 502

One of our web projects has seen an increase in t...

Implement a simple search engine based on MySQL

Table of contents Implementing a search engine ba...

Understanding flex-grow, flex-shrink, flex-basis and nine-grid layout

1. flex-grow, flex-shrink, flex-basis properties ...

Zabbix monitors the process of Linux system services

Zabbix automatically discovers rules to monitor s...

Reasons and solutions for slow MySQL query stuck in sending data

Because I wrote a Python program and intensively ...

MySQL multi-master and one-slave data backup method tutorial

Overview Operations on any one database are autom...

Introduction to Linux environment variables and process address space

Table of contents Linux environment variables and...

vue front-end HbuliderEslint real-time verification automatic repair settings

Table of contents ESLint plugin installation in H...