MYSQL master-slave replication knowledge points summary

MYSQL master-slave replication knowledge points summary

An optimization solution when a single MYSQL server cannot meet the current website traffic. Need to build MySQL cluster technology.

1. Function:

When inserting, modifying, or deleting data on the master server, the data will be automatically synchronized to the slave server.

Note: Master-slave replication is one-way, only master->slave

There are two types: Transmitting type (one master and multiple slaves): generally used in: backup and read-write separation.

Ring (multi-master and multi-slave): General use: when the main server is under great pressure, cross-regional websites can achieve data synchronization

In a ring structure, if records are inserted into the same table on three servers at the same time, an "ID conflict problem" will occur.

Solution: Let the three servers generate different IDs;

The first one: 1,4,7...

Second channel: 2,5,8...

Channel 3: 3, 6, 9...

This can be set in the MYSQL configuration file:

2. The principle of master-slave (using bin log)

There is a log in MySQL called bin log (binary log). This log records all SQL statements that modify the database (insert, update, delete, ALTER TABLE, grant, etc.). The principle of master-slave replication is actually to copy the BIN log on the master server to the slave server and execute it again, so that the data on the slave server is the same as the data on the master server.

Extension: Logs in mysql:

Query log Error log Bin log

Slow log: You can set a time threshold, such as 0.5 seconds, so that all SQL statements whose execution time exceeds this value will be recorded in the future. In this way, we can record the slow SQL statements and optimize them specifically.

Purpose: You can quickly locate the SQL that is dragging down the website, and then optimize it by: creating an index and caching the results of this SQL.

3. Actual Configuration

Use the MYSQL of Windows system as the master server and the one under LINUX as the slave server.

Primary Server:

1. Enable bin logging

Modify the mysql configuration file: my.ini add:

Assign a server-id to the server (the ID values ​​of the master and slave servers cannot be repeated)

If it is a ring server, you need to add the following items:

log-slave-updates = on // If it is a ring multi-server, you need to set this item.

Create an account on the master server for the slave server to synchronize data

Login to MYSQL

Execute a SQL:

Created an account with only REPLICATION SLAVE permissions: Username: slave Password: 1234

Execute SQL on the primary server to view the status of the current bin log of the primary server

Note: These two values ​​will change every time you modify the data, so after checking these two values, do not operate the master server and go directly to the slave server configuration. Otherwise, the values ​​will not correspond and the synchronization will fail.

From the server (linux):

Enable bin log

Modify the configuration file /etc/my.cnf

Set a server-id:

3. If it is a ring server, you need to add the following items:

log-slave-updates = on // If it is a ring multi-server, you need to set this item.

4. Execute SQL statements on the slave server to configure the address of the master server:

Log in to MYSQL:

Set up the slave server and start replication

5. Execute SQL to query whether the status of the slave server is configured successfully:

If it is two YES then the code succeeds! Finish!

Note: Before the configuration is successful, the data on the master server will not automatically be transferred to the slave server. Therefore, you need to manually import all the data on the master server to the slave server before configuration, and then after configuring the master and slave, the data will be synchronized.

Applications:

The slave server acts as a backup server for data. When the server is under great pressure, you can use master-slave servers to separate read and write traffic to reduce the pressure on the server. Cross-region website optimization

How to create a message board function, taking into account the architectural ideas optimized for different regions?

The above is all the knowledge points about the principles of MYSQL master-slave replication. Thank you for your learning and support for 123WORDPRESS.COM.

You may also be interested in:
  • MySQL master-slave replication delay causes and solutions
  • MySQL master-slave replication configuration process
  • Comprehensive interpretation of MySQL master-slave replication, from principle to installation and configuration
  • MySQL master-slave replication principle and practice detailed explanation
  • Detailed explanation of the principles and usage of MySQL master-slave replication and read-write separation
  • Detailed explanation of the role and working principle of MySQL master-slave replication
  • Summary of MYSQL full backup, master-slave replication, cascading replication, and semi-synchronization
  • In-depth understanding of MySQL master-slave replication thread state transition
  • How to skip errors in mysql master-slave replication

<<:  Linux jdk installation and environment variable configuration tutorial (jdk-8u144-linux-x64.tar.gz)

>>:  js to achieve simple front-end paging effect

Recommend

Detailed explanation of how to implement secondary cache with MySQL and Redis

Redis Introduction Redis is completely open sourc...

Optimize MySQL with 3 simple tweaks

I don't expect to be an expert DBA, but when ...

A complete example of implementing a timed crawler with Nodejs

Table of contents Cause of the incident Use Node ...

Detailed explanation of how Vue components transfer values ​​to each other

Table of contents Overview 1. Parent component pa...

JavaScript implements select all and unselect all operations

This article shares the specific code for JavaScr...

A brief analysis of the differences between px, rem, em, vh, and vw in CSS

Absolute length px px is the pixel value, which i...

How to remount the data disk after initializing the system disk in Linux

Remount the data disk after initializing the syst...

HTML uses marquee to achieve text scrolling left and right

Copy code The code is as follows: <BODY> //...

How to Understand and Identify File Types in Linux

Preface As we all know, everything in Linux is a ...

Detailed explanation of Mysql's concurrent parameter adjustment

Table of contents Query cache optimization Overvi...

HTML Code Writing Guide

Common Convention Tags Self-closing tags, no need...

Example code and method of storing arrays in mysql

In many cases, arrays are often used when writing...

How to create a responsive column chart using CSS Grid layout

I have been playing around with charts for a whil...