Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to auto-increment primary key in MySQL for multi-master replication

A webapp I am speccing out is going to need to be horizontally scalable, and will require a pair of MySQL servers from the start (perhaps rising to more than one soon after launch). As an auto-increment primary key is not suitable, I was wondering what alternatives people use.

Something with the date in makes the most sense, but if you go down to the millisecond level and assume a three-digit server ID it's a very big number:

sssyymmddhhmmssmicros or 001110510143622123456 for right now - plus you could perhaps do with another number at the beginning to simplify the leading zeros (datacentre ID, perhaps).

Epoch time shortens it, but only a little.

What else is there?

like image 474
Grim... Avatar asked Dec 28 '22 20:12

Grim...


1 Answers

I think what you're looking for is UUID (Universal Unique Identifier). In MySQL you can generate UUID's whith the function uuid().

SELECT uuid();

An UUID looks like this 8cb1764a-7b0d-11e0-aaa7-00270e0f8070 ... I know! it's very long but it works.

like image 149
PachinSV Avatar answered Dec 30 '22 10:12

PachinSV