简体   繁体   English

mysql UUID()和java UUID.randomUUID()

[英]mysql UUID() and java UUID.randomUUID()

I'm wondering if I can safely replace ids generated with mysql's UUID() function with ids generated by java's UUID.randomUUID() function? 我想知道我是否可以安全地用java的UUID.randomUUID()函数生成的id替换用mysql的UUID()函数生成的id?

I'm looking to share these ids in urls with my users, but the mysql UUID() output looks very similar to each other after a few runs: 我想在我的用户的网址中分享这些ID,但几次运行后,mysql UUID()输出看起来非常相似:

3ae2c9c4-47df-11e1-8c2a-a46b34c02a9e
976de634-47e3-11e1-8c2a-a46b34c02a9e
530cc5c6-47e7-11e1-8c2a-a46b34c02a9e
...

I'm not saying they're not unique, but is this how the results of UUID() usually appear? 我不是说它们不是唯一的,但这是UUID()的结果通常出现的吗? I suppose it's tied to some identifier on my machine. 我想它与我机器上的某些标识符有关。

Java's UUID.randomUUID() "look" more random: Java的UUID.randomUUID()“看起来”更随机:

c042437b-298a-41c4-c2b6-0f83552bdb8b
e33d8ab7-d9d3-4ffe-a592-650a125d2a93
ecb12c54-5741-45c8-8b85-1825c19a9cae
...

From my naive understanding of uniqueness, the ids generated by the java method should work as a perfect replacement for mysql's UUID()? 从我对唯一性的天真理解,java方法生成的id应该是mysql的UUID()的完美替代品?

(another bonus for using java's version is that I won't have to re-fetch an inserted record which uses UUID() when I need to provide it in a reply to a client) (使用java版本的另一个好处是,当我需要在回复客户端时提供它时,我不必重新获取使用UUID()的插入记录)

Thank you 谢谢

Are you using linux or FreeBSD? 你使用的是Linux还是FreeBSD? From mysql's documentation : 从mysql的文档

The fifth number is an IEEE 802 node number that provides spatial uniqueness. 第五个数字是提供空间唯一性的IEEE 802节点号。 A random number is substituted if the latter is not available (for example, because the host computer has no Ethernet card, or we do not know how to find the hardware address of an interface on your operating system). 如果后者不可用,则替换随机数(例如,因为主机没有以太网卡,或者我们不知道如何在操作系统上找到接口的硬件地址)。 In this case, spatial uniqueness cannot be guaranteed. 在这种情况下,无法保证空间唯一性。 Nevertheless, a collision should have very low probability. 然而,碰撞应该具有非常低的概率。

Currently, the MAC address of an interface is taken into account only on FreeBSD and Linux. 目前,仅在FreeBSD和Linux上考虑接口的MAC地址。 On other operating systems, MySQL uses a randomly generated 48-bit number. 在其他操作系统上,MySQL使用随机生成的48位数字。

Also note: 另请注意:

Warning : 警告

Although UUID() values are intended to be unique, they are not necessarily unguessable or unpredictable. 虽然UUID()值是唯一的,但它们不一定是不可预测的或不可预测的。 If unpredictability is required, UUID values should be generated some other way. 如果需要不可预测性,则应以其他方式生成UUID值。

EDIT 编辑

If you want user ids to appear more random, you can pass them through a hashing function like MD5(UUID()) , which gives a more convincing random string 如果您希望用户ID 看起来更随机,您可以通过像MD5(UUID())这样的散列函数传递它们,这样可以提供更有说服力的随机字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM