MySQL的server_uuid获取之uuid()函数和uuid_short()函数
发表于:2025-11-15 作者:千家信息网编辑
千家信息网最后更新 2025年11月15日,1、uuid()函数mysql> select uuid(),uuid();+--------------------------------------+----------------------
千家信息网最后更新 2025年11月15日MySQL的server_uuid获取之uuid()函数和uuid_short()函数1、uuid()函数
server_start_time是server启动时的时间,单位是秒。之后每次执行该函数都+1
3、server启动时,会自动产生server_uuid
- mysql> select uuid(),uuid();
- +--------------------------------------+--------------------------------------+
- | uuid() | uuid() |
- +--------------------------------------+--------------------------------------+
- | 50120c25-d75c-11e7-9484-000c29c9278a | 50120c53-d75c-11e7-9484-000c29c9278a |
- +--------------------------------------+--------------------------------------+
- 1 row in set (0.00 sec)
- String *Item_func_uuid::val_str(String *str)
- {
- ...
- mac\rand get
- ...
- uint32 time_low= (uint32) (tv & 0xFFFFFFFF);
- uint16 time_mid= (uint16) ((tv >> 32) & 0xFFFF);
- uint16 time_hi_and_version= (uint16) ((tv >> 48) | UUID_VERSION);
- s[8]=s[13]='-';
- tohex(s, time_low, 8);
- tohex(s+9, time_mid, 4);
- tohex(s+14, time_hi_and_version, 4);
- strmov(s+18, clock_seq_and_node_str);
- ...
- }
time_low、time_mid、time_high_and_version转成16进制后分别对应第1 2 3段。
这个时间是从1582-10-15 00:00:00.00到当前时间的100ns值。(实际上系统只能取到精确us,再乘以10)。所以你短时间连续执行的话,比较可能只有第一个值在改,实际上1 2 3都可能会改变。第4段是你启动这个MySQL后第一次执行select uuid()时的随机数,每次重启会改变。第5段是mac值转过来的,同一个机器多实例的一般相同。如果mac值获取不到,则是一个随机值。所以这个值可以认为是每次执行都不相同。并且不同实例之间也只有极微小概率重复。
2、uuid_short()函数
- mysql> select uuid_short(),uuid_short();
- +--------------------+--------------------+
- | uuid_short() | uuid_short() |
- +--------------------+--------------------+
- | 818004335832072194 | 818004335832072195 |
- +--------------------+--------------------+
- 1 row in set (0.00 sec)
- main->init_common_variables()->
- server_start_time= flush_status_time= my_time(0);
- //获取server启动时的时间戳
- ulonglong uuid_value;
- main->init_common_variables->item_init->
- void uuid_short_init()
- {
- uuid_value= ((((ulonglong) server_id) << 56) +
- (((ulonglong) server_start_time) << 24));
- }
- longlong Item_func_uuid_short::val_int()
- {
- ulonglong val;
- mysql_mutex_lock(&LOCK_uuid_generator);
- val= uuid_value++;
- mysql_mutex_unlock(&LOCK_uuid_generator);
- return (longlong) val;
- }
server_start_time是server启动时的时间,单位是秒。之后每次执行该函数都+1
3、server启动时,会自动产生server_uuid
- main->init_server_auto_options:处理server_uuid
- my_load_defaults(fname, groups, &argc, &argv, NULL)//fname:auto.cnf
- handle_options(&argc, &argv, auto_options, mysqld_get_one_option);
- if(uuid){
- strcpy(server_uuid, uuid);
- }else{
- flush= TRUE;
- generate_server_uuid();
- }
- if (flush)
- DBUG_RETURN(flush_auto_options(fname));
- //O_CREAT|O_RDWR形式open,即如果没有auto.cnf文件,
- //则创建一个并将生成的server_uuid写入文件并flush
- generate_server_uuid:
- func_uuid= new (thd->mem_root) Item_func_uuid();
- func_uuid->fixed= 1;
- func_uuid->val_str(&uuid);
- strncpy(server_uuid, uuid.c_ptr(), UUID_LENGTH);
函数
文件
时间
不同
相同
只有
实例
实际
实际上
第一次
过程
微小
精确
之间
单位
字符
字符串
形式
机器
概率
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器 市场占有率
象棋软件开发的布局
网吧计费系统数据库代码
sql数据库表设计
普陀区智能软件开发销售电话
数据库表的最大值
安徽综合软件开发市场报价
如何设置数据库的引擎
插入数据库时中文出现问好
网络安全p1p2定级
流量无法连接到服务器
江西数据库安全
异构数据库转换同步
西安大地测绘软件开发
成都软件开发行业前景分析
穿越火线最奇怪的服务器奇葩人物
数据库支持emoji表情
加强网络安全防护的策略
宁津 灵猫有数网络技术
福州星空网络技术有限公司
臻游网络安全黑板报
江西数据库安全
程序访问数据库慢
雄安新区人才数据库
12核处理器的服务器
网络安全小主播内容
目前主要的专利数据库
构建网络数据库的意义
祝你愉快吉他谱软件开发
百度seo服务器ip