MySQL auto.cnf 文件

MySQL启动时会从data_dir/auto.cnf获取server_uuid,用于识别复制源和GTID事务。若文件不存在,MySQL会生成新的uuid。不要随意更改此值,启动时会调用init_server_auto_options和generate_server_uuid函数处理。
Python3.8

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

一、auto.cnf 是什么?

简介:MySQL启动时,会自动从data_dir/auto.cnf 文件中获取server-uuid值,并将这个值存储在全局变量server_uuid中。如果这个值或者这个文件不存在,那么将会生成一个新的uuid值,并将这个值保存在auto.cnf文件中。

源码解释:

/**
  File 'auto.cnf' resides in the data directory to hold values of options that
  server evaluates itself and that needs to be durable to sustain the server
  restart. There is only a section ['auto'] in the file. All these options are
  in the section. Only one option exists now, it is server_uuid.
  Note, the user may not supply any literal value to these auto-options, and
  only allowed to trigger (re)evaluation.
  For instance, 'server_uuid' value will be evaluated and stored if there is
  no corresponding line in the file.
  Because of the specifics of the auto-options, they need a seperate storage.
  Meanwhile, it is the 'auto.cnf' that has the same structure as 'my.cnf'.

  @todo consider to implement sql-query-able persistent storage by WL#5279.
  @return Return 0 or 1 if an error occurred.
 */

auto.cnf文件是存在MySQL数据目录中,存储了server_uuid的值.

[auto]
server-uuid=f2d0efd6-6ab7-11e8-8fdd-fa163eda8190

MySQL启动时,会自动从data_dir/auto.cnf 文件中获取server-uuid值,并将这个值存储在全局变量server_uuid中。如果这个值或者这个文件不存在,那么将会生成一个新的uuid值,并将这个值保存在auto.cnf文件中。

Important 

The auto.cnf file is automatically generated;do not attempt to write or modify this file. 

用处1:When using MySQL replication, sources and replicas know each other's UUIDs. The value of a replica's UUID can be seen in the output of SHOW SLAVE HOSTS. Once START SLAVE has been executed, the value of the source's UUID is available on the replica in the output of SHOW SLAVE STATUS.

用处2:A server's server_uuid is also used in GTIDs for transactions originating on that server.

When starting, the replication I/O thread generates an error and aborts if its source's UUID is equal to its own unless the --replicate-same-server-id option has been set. In addition, the replication I/O thread generates a warning if either of the following is true:

所以不要轻易去改变这个值。

 

二,server_uuid 的生成

mysql启动的时候会调用函数init_server_auto_options()来读取auto.cnf文件,如果文件丢失,则会调用函数generate_server_uuid(),生成一个新的server_uuid.

static int init_server_auto_options()
{...
if (uuid)
  {
    if (!Uuid::is_valid(uuid))
    {
      sql_print_error("The server_uuid stored in auto.cnf file is not a valid UUID.");
      goto err;
    } 
  ...
/* server_uuid will be set in the function */
    if (generate_server_uuid())
      goto err;
    DBUG_PRINT("info", ("generated server_uuid=%s", server_uuid));
...
/*
    The uuid has been copied to server_uuid, so the memory allocated by
    my_load_defaults can be freed now.
   */

函数generate_server_uuid()

static int generate_server_uuid()
{
  THD *thd;
  Item_func_uuid *func_uuid;
  String uuid;

  /*
    To be able to run this from boot, we allocate a temporary THD
   */
... 
 /*
    Initialize the variables which are used during "uuid generator
    initialization" with values that should normally differ between
    mysqlds on the same host. This avoids that another mysqld started
    at the same time on the same host get the same "server_uuid".
  */

相关影响项:

1,数据库的启动时间

2,线程的LWP ID

3,一个随机的内存地址

  const time_t save_server_start_time= server_start_time;//获取mysql启动时间
  server_start_time+= ((ulonglong)current_pid << 48) + current_pid; //加入LWP号运算
  thd->status_var.bytes_sent= (ulonglong)thd;

  lex_start(thd);
  func_uuid= new (thd->mem_root) Item_func_uuid();
  func_uuid->fixed= 1;
  func_uuid->val_str(&uuid);

  sql_print_information("Generated uuid: '%s', "
                        "server_start_time: %lu, bytes_sent: %llu",
                        uuid.c_ptr(),
                        (ulong)server_start_time, thd->status_var.bytes_sent);

长期关注mysql,redis,python,个人成长,认知提升等,喜欢请添加微信weihaodong0557,或QQ群:979480263

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值