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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL: From Small Businesses to Large Enterprises MySQL: From Small Businesses to Large Enterprises Apr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

How does MySQL index cardinality affect query performance? How does MySQL index cardinality affect query performance? Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

See all articles