gpt4 book ai didi

java - mysql UUID() 和 java UUID.randomUUID()

转载 作者:可可西里 更新时间:2023-11-01 06:32:55 25 4
gpt4 key购买 nike

我想知道我是否可以安全地将使用 mysql 的 UUID() 函数生成的 ID 替换为由 Java 的 UUID.randomUUID() 函数生成的 ID?

我希望在 url 中与我的用户共享这些 id,但运行几次后 mysql UUID() 输出看起来非常相似:

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

我并不是说它们不是唯一的,但 UUID() 的结果通常是这样显示的吗?我想它与我机器上的某个标识符有关。

Java 的 UUID.randomUUID() “看起来”更随机:

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

从我对唯一性的幼稚理解来看,java方法生成的id应该可以完美替代mysql的UUID()?

(使用 java 版本的另一个好处是,当我需要在回复客户时提供它时,我不必重新获取使用 UUID() 的插入记录)

谢谢

最佳答案

您使用的是 Linux 还是 FreeBSD?来自 mysql 的 documentation :

The fifth number is an IEEE 802 node number that provides spatial uniqueness. 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. On other operating systems, MySQL uses a randomly generated 48-bit number.

另请注意:

Warning:

Although UUID() values are intended to be unique, they are not necessarily unguessable or unpredictable. If unpredictability is required, UUID values should be generated some other way.

编辑

如果你想让用户 ID出现更随机,你可以通过像 MD5(UUID()) 这样的散列函数传递它们,这会给出一个更有说服力的随机字符串

关于java - mysql UUID() 和 java UUID.randomUUID(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9014644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com