MySQL (6) master-slave replication

MySQL Series Articles

MySQL (1) Basic structure, SQL statement operation, try
MySQL (2) Index principle and optimization
MySQL (3) SQL optimization, Buffer pool, Change buffer
MySQL (4) Transaction principle and analysis
MySQL (5) Caching strategy
MySQL (6) Three paradigms of master-slave replication database


foreword

MySQL has its own master-slave synchronization mechanism. The device installed with MySQL can realize the synchronization function of the master-slave database by setting related parameters.

What are the benefits of setting up a MySQL master-slave?

  • Similar to the benefits of clustering, improving availability, performance, fault tolerance, etc.
  • read-write separation
  • load balancing

What mode does the master-slave use?

Usually one master and one slave, or one master and many slaves.

The master server is only responsible for writing, while the slave server is only responsible for reading, which improves efficiency and reduces pressure.

Master-slave replication principle

insert image description here

  1. The main database is updated , that is, when performing update, insert, delete operations, the main database will write the operation logic to binlog through io-thread;
  2. The slave library will request to read the binlog from the main library, and the main library will send the binlog content to the slave library through the log dump thread.
  3. The slave library writes the updated binlog to the local relay-log (relay log) of the slave library through the io-thread thread;
  4. The slave library will eventually read the relay-log through sql-thread, parse the content into specific operations, and finally ensure that the master-slave data is consistent;

Data synchronization method

There are two types of binlog data passed during the synchronization process: statement and row data.

statement replication

That is, the entire master-slave replication process transmits operation records instead of database data. Therefore, it is necessary to execute an operation command from the database after it is complicated to the database, so that the data results can be finally synchronized to the disk.

advantage:

  • It can reduce the amount of data transmission and replication delay , and improve the efficiency of master-slave replication (because the entire library needs to be transferred to transfer data).
  • Assuming there are one million records in the table, and one sql updates all the tables, statement-based replication only needs to send one sql, while row-based replication needs to send one million updated records

shortcoming:

  • There may be a warning that it cannot be copied correctly
  • Insert ... select statements will perform a large number of row-level lock table
  • The Update statement will perform a large number of row-level lock table to scan the entire table

The statement replication mode is used by default in mysql5.6

row data copy

That is, update each row of data in the actual library. This leads to a relatively high pressure on replication, a large space occupied by logs, and a large amount of transmission bandwidth. But this method is more precise than statement-based replication.
advantage:

  • No query plan is required.

  • I don't know what statement is being executed.

  • For example, a statement to update the user's total points needs to count all the user's points and write them into the user table. If it is based on statement replication, the slave database needs to count the user's points again, while based on row replication, the records are directly updated without counting user points.

shortcoming:

  • log will be huge

synchronous mode

MySQL supports asynchronous replication, synchronous replication, and semi-synchronous replication.

asynchronous replication

In this mode, the master node will not actively push data to the slave nodes. The master library will immediately return the result to the client after executing the transaction submitted by the client, and does not care whether the slave library has received and processed it.

In this way, there will be a problem. If the master node crashes, the transaction that has been submitted on the master node may not be transmitted to the slave node at this time. incomplete data.

synchronous replication

A unique replication method in MySQL cluster.

When the master database executes a transaction, then all the slave databases have copied the transaction and successfully executed it before returning a success message to the client.

Because it is necessary to wait for all slave libraries to execute the transaction before returning success information, the performance of full synchronous replication will inevitably be seriously affected.

semi-synchronous replication

On the basis of asynchronous replication, it is ensured that at least one slave library has received and recorded the transaction before any transaction on the master library is submitted.
Between asynchronous replication and full synchronous replication, the master library does not return to the client immediately after executing the transaction submitted by the client, but waits for at least one slave library to receive and write to the relay log before returning success information to the client end (you can only ensure that the Binlog of the main library has been transferred to at least one slave node), otherwise you need to wait until the timeout period and then switch to asynchronous mode before submitting.

Compared with asynchronous replication, semi-synchronous replication improves data security and ensures that data can be successfully backed up to the slave database to a certain extent. At the same time, it also causes a certain degree of delay, but it is lower than the delay of full-synchronous mode, which is the least delay. is the time for one TCP/IP round trip. Therefore, semi-synchronous replication is best used in low-latency networks.

For details, refer to
Principle MySQL Master-Slave Replication
Configure Mysql Master-Slave Synchronization (Replication)

Guess you like

Origin blog.csdn.net/weixin_44477424/article/details/131742569