Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Primary key conflict resolution during primary primary replication

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The problem that must be avoided under master and master replication-synchronization conflict

Example:

Create table stu (

Id int primary key auto_increment.

).

2 mysql have equal status, if 2 requests arrive at 2 servers at the same time

The requested A node, the id of stu is 1

The requested B node, the id of stu is 1

Synchronization-> conflict

How to solve?

Let 1 server grow from 1 to 3 to 5 to 7.

The other server, 2pencils, 4pencils, 6pens, 8s to grow.

One server:

Set global auto_increment_increment = 2

Set global auto_increment_offset = 1

Set session auto_increment_increment = 2

Set session auto_increment_offset = 1

Another server:

Set global auto_increment_increment = 2

Set global auto_increment_offset = 2

Set session auto_increment_increment=2

Set session auto_increment_offset = 2

Note: auto-increment-increment and auto-increment-offset should be written to the configuration file to prevent failure after the next restart.

Similar results are obtained after operation as follows

If you need to add a server at a later stage, this method is limited.

We can solve it in terms of business logic.

For example, there is a sequnce sequence in oracle.

Each time the sequence is accessed, increasing / decreasing data is generated.

Taking redis as an example, we can build a special global:userid.

Every time PHP is inserted into Mysql, incr- > global:userid is used to get a non-repetitive userid.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report