一、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:
-
No source having the expected
server_uuidexists. -
The source's
server_uuidhas changed, although noCHANGE MASTER TOstatement has ever been executed.
所以不要轻易去改变这个值。
二,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
MySQL启动时会从data_dir/auto.cnf获取server_uuid,用于识别复制源和GTID事务。若文件不存在,MySQL会生成新的uuid。不要随意更改此值,启动时会调用init_server_auto_options和generate_server_uuid函数处理。
1707

被折叠的 条评论
为什么被折叠?



