Detailed explanation of server-id example in MySQL master-slave synchronization

Detailed explanation of server-id example in MySQL master-slave synchronization

Preface

When we build a MySQL cluster, we naturally need to complete the master-slave synchronization of the database to ensure data consistency. There are many ways of master-slave synchronization, including one master and multiple slaves, chained master-slave, and multiple masters and multiple slaves. You can set them up according to your needs. But as long as you need master-slave synchronization, you must pay attention to the configuration of server-id, otherwise master-slave replication exceptions will occur.

In controlling database data replication and log management, there are two important configurations: server-id and server-uuid, which affect binary log file records and global transaction identifiers.

server-id configuration

When you use a master-slave topology, be sure to specify a unique server-id for all MySQL instances. The default value is 0. When server-id=0, the master will still record binary logs, but will reject all slave connections; the slave will reject connections to other instances.

The server-id of the MySQL instance is a global variable and can be viewed directly:

mysql> show variables like '%server_id%';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| server_id | 171562767 |
+---------------+-----------+
1 row in set (0.00 sec)

We can modify the global variable server-id directly online, but it will not take effect immediately, so remember to restart the service after the modification. After restarting, the system configuration file will be re-read, making the previous modification invalid. Therefore, it is recommended to modify the configuration file and restart the service instead of modifying it online:

#my.cnf
[mysqld]
#replication
log-bin=mysql-bin
server-id=171562767
sync_binlog=1
binlog-ignore-db=mysql
binlog-ignore-db=information_schema

Server-id usage

The server-id is used to identify the database instance to prevent infinite loops of SQL statements in chained master-slave or multi-master-multi-slave topologies:

  • Mark the source instance of the binlog event
  • Filter the main database binlog. When the server-id is found to be the same, skip the event execution to avoid infinite loop execution.
  • If replicate-same-server-id=1 is set, all events are executed, but this may lead to an infinite loop of SQL statements.

Let's use two examples to illustrate why server-id should not be repeated:

When the server-id of the primary and standby databases are duplicated

Because replicate-same-server-id=0 by default, the slave database will skip all data synchronized with the master database, resulting in inconsistency between master and slave data.

When the server-ids of two standby databases are duplicated

This will cause the connection between the slave database and the master database to be disconnected from time to time, resulting in a large number of exceptions. According to the design of MySQL, the master and slave databases are connected and synchronized through an event mechanism. When a new connection comes, if the server-id is found to be the same, the master database will disconnect the previous connection and re-register the new connection. When database A is connected to the main database, database B will connect to it, which will disconnect database A. Database A will then reconnect, and this cycle will repeat, resulting in a large amount of abnormal information.

Rules for generating server-id

Since the server-id cannot be the same, when we have 10 instances, how can we ensure that each one is different? There are several commonly used methods:

  • Random Numbers
  • Timestamp
  • IP address + port
  • Centrally distribute in the management center and generate self-increment ID

All of the above methods are OK, but be careful not to exceed the maximum value 2^32-1, and the value should preferably be >2. The method I use is the last two digits of the IP address + the local MySQL instance number, but if you are using Docker to manage multiple instances, how do you generate this? Can you think about whether there is any elegant solution?

server-uuid configuration

The MySQL service will automatically create and generate the server-uuid configuration:

  • Read the UUID in the ${data_dir}/auto.cnf file
  • If it does not exist, automatically create the file and generate a new UUID and read it
shell> cat ~/mysql/data/auto.cnf
[auto]
server-uuid=fd5d03bc-cfde-11e9-ae59-48d539355108

The auto.cnf configuration style is similar to my.cnf, but this file only contains an auto configuration block and a server-uuid configuration line. It is created automatically, so do not modify its contents.

In the master-slave topology, the master and slave can know each other's UUID. Use show slave hosts on the master and show slave status on the slave to view the Master_UUID field.

The server-uuid parameter does not replace the server-id parameter. They have different functions. When synchronizing the master and slave instances, if the server-uuid of the master and slave instances are the same, an error will be reported and the system will exit. However, we can avoid the error by setting replicate-same-server-id=1 (not recommended).

References

  • 17.1.6 Replication and Binary Logging Options and Variables: https://dev.mysql.com/doc/ref...
  • How to generate a unique server ID: https://www.jb51.net/article/169550.htm

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Two ways to solve the problem of MySQL master-slave database not being synchronized
  • Master-slave synchronization configuration of Mysql database
  • This article will show you the principle of MySQL master-slave synchronization
  • How to run MySQL in Docker environment and enable Binlog to configure master-slave synchronization
  • MySQL master-slave synchronization, implementation principle of transaction rollback
  • Detailed explanation of the actual process of master-slave synchronization of MySQL database
  • Master-slave synchronization configuration and read-write separation of MySQL database
  • MySQL master-slave synchronization principle and application

<<:  How to implement ansible automated installation and configuration of httpd in Linux system

>>:  The whole process of realizing website internationalization using Vite2 and Vue3

Recommend

MySQL 5.7.11 zip installation and configuration method graphic tutorial

1. Download the MySQL 5.7.11 zip installation pac...

Float and Clear Float in Overview Page

1. Float: The main purpose is to achieve the effe...

Example code for implementing a pure CSS pop-up menu using transform

Preface When making a top menu, you will be requi...

Do you know what are the ways to jump routes in Vue?

Table of contents The first method: router-link (...

How to delete the container created in Docker

How to delete the container created in Docker 1. ...

Solution to the problem that VC6.0 cannot be used when installed on WIN10

VC6.0 is indeed too old VC6.0 is a development to...

jQuery achieves breathing carousel effect

This article shares the specific code of jQuery t...

Linux file system operation implementation

This reading note mainly records the operations r...

Solution for Nginx installation without generating sbin directory

Error description: 1. After installing Nginx (1.1...

A brief discussion on the corresponding versions of node node-sass sass-loader

Table of contents The node version does not corre...

MySQL high availability solution MMM (MySQL multi-master replication manager)

1. Introduction to MMM: MMM stands for Multi-Mast...