Primary key conflict resolution during primary primary replication
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.