Table of Contents
The principle of master-slave synchronization
Implementation of master-slave synchronization (asynchronous replication, databases on different servers)
1. Configure the main database to open binary-log
2. Configure the slave database to open relay-log
3. Connect two databases
4. Verification
The role of master-slave synchronization
Notes on master-slave synchronization
Interview related
Home Database Mysql Tutorial Detailed explanation of mysql master-slave synchronization principle, configuration and delay

Detailed explanation of mysql master-slave synchronization principle, configuration and delay

Aug 07, 2018 pm 04:53 PM

This article introduces the master-slave synchronization principle, master-slave synchronization configuration, and master-slave synchronization delay of mysql. First, let’s understand what master-slave synchronization is. Master-slave synchronization, as the name implies, is also called master-slave replication, which is used to establish a The database environment is exactly the same as the main database. Master-slave synchronization allows data to be copied from one database server to other servers to ensure that the data in the master database and the data in the slave database are consistent.

  • The cluster is a shared storage, which is data-sharing. There is no sharing in master-slave replication. Each machine is an independent and complete system, which is nothing-sharing.

The principle of master-slave synchronization

  • There are three main ways to implement master-slave replication after mysql5.6 :

    1. Asynchronous replication

    2. Fully synchronous replication

    3. Semi-synchronous replication

  • Master-slave synchronization schematic

1. When the update event (update, insert, delete) of the master database is written binary-log .

2. The slave library creates an I/O thread, which connects to the main library and requests the main library to send the update records in the binlog to the slave library. The main library creates a binlog The dump thread thread sends the contents of the binlog to the slave library. The I/O thread of the slave library reads the updates sent by the output thread of the main library and copies these updates to the local relay log file.

3. From The library creates a SQL thread. This thread reads the update events written to the relay log from the library I/O thread and executes them.

Implementation of master-slave synchronization (asynchronous replication, databases on different servers)

1. Configure the main database to open binary-log

vim /etc/my.cnf

在[mysqld]下添加

server-id=1(用来标识不同的数据库)log-bin=master-bin(打开bin-log并配置文件名为master-bin)log-bin-index=master-bin.index(区分不同的log-bin文件)
Copy after login

Restart the database: systemctl restart mariadb.service

2. Configure the slave database to open relay-log

vim /etc/my.cnf

在[mysqld]下添加

server-id=2relay-log=slave-relay-bin(打开relay-log并配置文件名为slave-relay-bin)

relay-log-index=slave-relay-bin.index
Copy after login

Restart the database: systemctl restart mariadb.service

3. Connect two databases

In the main database: Create user repl, which is required for each slave server An account name and password for the master database to connect to the master server.

CREATE USER 'repl'@'114.116.77.213' IDENTIFIED BY '12312';GRANT REPLICATION SLAVE ON *.* TO 'repl'@'114.116.77.213' IDENTIFIED BY '12312';
Copy after login

In the slave database:

change master to master_host='47.106.78.106',master_user='repl',master_password='12312',master_log_file='master-bin.000001',master_log_pos=0;
Copy after login

Start synchronization: start slave;

4. Verification

Create a database in the master database, and then check the slave database

The role of master-slave synchronization

1. Do hot backup of data as a backup database. After the main database server fails, you can switch to the slave database to continue working to avoid data loss.

2. Read and write Separation allows the database to support greater concurrency.

Notes on master-slave synchronization

  1. The master library can read and write data, while the slave library can only Read data, because when the slave library writes data, the positon will change, but the position of the main library will not change. When the main library writes data and changes the position, there may be a conflict.

  2. When the binarylog file of the main library stores a lot of data, that is, when the position is very large, a new binarylog file will be split and the position is set to 0;

  3. mysql of the master-slave library The versions can be different, but the mysql version of the slave library is higher than the version of the main library. If not, the statements of the main library may not be executed when reaching the slave library.
    Because mysql is backward compatible, that is It is said that the statements of the lower version are supported in the higher version, but some statements of the higher version are not supported in the lower version.

(If you ask When it comes to database master-slave issues, you must ask the following questions):

  1. What are the benefits of master-slave?

  2. What is the principle of master-slave?

  3. Do you know about the delay problem of reading from the database? How to solve?

  4. What should I do if the master server crashes after being the master and slave?

  5. The reason for the delay of master-slave synchronization

  6. The reason for the delay of master-slave synchronization

Master-slave synchronization delay problem

1. Reasons for the delay of master-slave synchronization

We know that a server is open N links are provided for the client to connect, so there will be large concurrent update operations, but there is only one thread to read the binlog from the server. When a certain SQL takes a little longer to execute on the slave server or due to some Locking the table for each SQL will result in a large backlog of SQL on the master server and not being synchronized to the slave server. This leads to master-slave inconsistency, that is, master-slave delay.

2. Solution to master-slave synchronization delay

In fact, there is no one-stop solution to master-slave synchronization delay, because all SQL must be executed in the slave server , but if the main server continues to have update operations written continuously, then once a delay occurs, the possibility of aggravating the delay will be greater. Of course we can take some mitigation measures.

a. We know that because the master server is responsible for update operations, it has higher security requirements than the slave server. All settings can be modified, such as sync_binlog=1, innodb_flush_log_at_trx_commit = 1, etc., but slave does not need to For such high data security, you can set sync_binlog to 0 or turn off binlog, innodb_flushlog, innodb_flush_log_at_trx_commit can also be set to 0 to improve the execution efficiency of SQL. This can greatly improve efficiency. The other is to use a better hardware device than the main library as the slave.

b. That is, a slave server is used as a backup without providing queries. When its load is reduced, the efficiency of executing the SQL in the relay log will naturally be higher.

c. The purpose of adding slave servers is to disperse the reading pressure, thereby reducing the server load.

Related recommendations:

MYSQL master-slave synchronization delay principle analysis and solutions

MYSQL master-slave synchronization delay principle

The above is the detailed content of Detailed explanation of mysql master-slave synchronization principle, configuration and delay. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

See all articles