溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL的server_uuid獲取之uuid()函數和uuid_short()函數

發布時間:2020-08-07 09:10:01 來源:ITPUB博客 閱讀:775 作者:yzs87 欄目:MySQL數據庫
1、uuid()函數
  1. mysqlselect uuid(),uuid();  
  2. +--------------------------------------+--------------------------------------+  
  3. | uuid()                               | uuid()                               |  
  4. +--------------------------------------+--------------------------------------+  
  5. | 50120c25-d75c-11e7-9484-000c29c9278a | 50120c53-d75c-11e7-9484-000c29c9278a |  
  6. +--------------------------------------+--------------------------------------+  
  7. 1 row in set (0.00 sec)  
  1. String *Item_func_uuid::val_str(String *str)  
  2. {  
  3.     ...  
  4.     mac\rand get  
  5.     ...  
  6.     uint32 time_low=            (uint32) (tv & 0xFFFFFFFF);  
  7.     uint16 time_mid=            (uint16) ((tv >> 32) & 0xFFFF);  
  8.     uint16 time_hi_and_version= (uint16) ((tv >> 48) | UUID_VERSION);  
  9.     s[8]=s[13]='-';  
  10.     tohex(s, time_low, 8);  
  11.     tohex(s+9, time_mid, 4);  
  12.     tohex(s+14, time_hi_and_version, 4);  
  13.     strmov(s+18, clock_seq_and_node_str);  
  14.     ...  
  15. }  
詳細產生uuid的過程,參考上面的函數進行分析。

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()函數

  1. mysql> select uuid_short(),uuid_short();  
  2. +--------------------+--------------------+  
  3. | uuid_short()       | uuid_short()       |  
  4. +--------------------+--------------------+  
  5. | 818004335832072194 | 818004335832072195 |  
  6. +--------------------+--------------------+  
  7. 1 row in set (0.00 sec)  
函數調用過程:
  1. main->init_common_variables()->  
  2.     server_start_time= flush_status_time= my_time(0);  
  3.     //獲取server啟動時的時間戳  
  1. ulonglong uuid_value;  
  2. main->init_common_variables->item_init->  
  3. void uuid_short_init()  
  4. {  
  5.   uuid_value= ((((ulonglong) server_id) << 56) +   
  6.                (((ulonglong) server_start_time) << 24));  
  7. }  
  1. longlong Item_func_uuid_short::val_int()  
  2. {  
  3.   ulonglong val;  
  4.   mysql_mutex_lock(&LOCK_uuid_generator);  
  5.   val= uuid_value++;  
  6.   mysql_mutex_unlock(&LOCK_uuid_generator);  
  7.   return (longlong) val;  
  8. }  
與uuid()函數返回固定字符串不同,uuid_short()函數返回的是ulonglong類型的值。mysql啟動后第一次執行的值通過server_id<<56+server_start_time<<24來初始化,其中
server_start_time是server啟動時的時間,單位是秒。之后每次執行該函數都+1

3、server啟動時,會自動產生server_uuid
  1. main->init_server_auto_options:處理server_uuid  
  2.     my_load_defaults(fname, groups, &argc, &argv, NULL)//fname:auto.cnf  
  3.     handle_options(&argc, &argv, auto_options, mysqld_get_one_option);  
  4.     if(uuid){  
  5.         strcpy(server_uuid, uuid);  
  6.     }else{  
  7.         flush= TRUE;  
  8.         generate_server_uuid();  
  9.     }  
  10.     if (flush)  
  11.         DBUG_RETURN(flush_auto_options(fname));  
  12.         //O_CREAT|O_RDWR形式open,即如果沒有auto.cnf文件,  
  13.         //則創建一個并將生成的server_uuid寫入文件并flush  
  1. generate_server_uuid:  
  2.     func_uuid= new (thd->mem_root) Item_func_uuid();  
  3.     func_uuid->fixed= 1;  
  4.     func_uuid->val_str(&uuid);  
  5.     strncpy(server_uuid, uuid.c_ptr(), UUID_LENGTH);  
其中val_str函數是Item_func_uuid::val_str。server啟動時,會從auto.cnf文件中讀取,如果沒有這個文件,則新創建文件并自動產生server_uuid,寫入文件并flush。


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女