mysql获取配置文件具体方法在my_default.cc 文件的init_default_directories(MEM_ROOT *alloc)方法里。
init_default_directories()方法是MySQL默认配置文件的获取顺序:
/**
Create the list of default directories.
@param alloc MEM_ROOT where the list of directories is stored
@details
The directories searched, in order, are:
- Windows: GetSystemWindowsDirectory()
- Windows: GetWindowsDirectory()
- Windows: C:/
- Windows: Directory above where the executable is located
- Unix: /etc/
- Unix: /etc/mysql/
- Unix: --sysconfdir=<path> (compile-time option)
- ALL: getenv("MYSQL_HOME")
- ALL: --defaults-extra-file=<path> (run-time option)
- Unix: ~/
On all systems, if a directory is already in the list, it will be moved
to the end of the list. This avoids reading defaults files multiple times,
while ensuring the correct precedence.
@retval NULL Failure (out of memory, probably)
@retval other Pointer to NULL-terminated array of default directories
*/
static const char **init_default_directories(MEM_ROOT *alloc)
{
const char **dirs;
char *env;
int errors= 0;
dirs= (const char **)alloc_root(alloc, DEFAULT_DIRS_SIZE * sizeof(char *));
if (dirs == NULL)
return NULL;
memset(dirs, 0, DEFAULT_DIRS_SIZE * sizeof(char *));
#ifdef _WIN32
{
char fname_buffer[FN_REFLEN];
if (my_get_system_windows_directory(fname_buffer, sizeof(fname_buffer)))
errors += add_directory(alloc, fname_buffer, dirs);
if (GetWindowsDirectory(fname_buffer, sizeof(fname_buffer)))
errors += add_directory(alloc, fname_buffer, dirs);
errors += add_directory(alloc, "C:/", dirs);
if (my_get_module_parent(fname_buffer, sizeof(fname_buffer)) != NULL)
errors += add_directory(alloc, fname_buffer, dirs);
}
#else
errors += add_directory(alloc, "/etc/", dirs);
errors += add_directory(alloc, "/etc/mysql/", dirs);
#if defined(DEFAULT_SYSCONFDIR)
if (DEFAULT_SYSCONFDIR[0])
errors += add_directory(alloc, DEFAULT_SYSCONFDIR, dirs);
#endif /* DEFAULT_SYSCONFDIR */
#endif
if ((env= getenv("MYSQL_HOME")))
errors += add_directory(alloc, env, dirs);
/* Placeholder for --defaults-extra-file=<path> */
errors += add_directory(alloc, "", dirs);
#if !defined(_WIN32)
errors += add_directory(alloc, "~/", dirs);
#endif
return (errors > 0 ? NULL : dirs);
}
靠后的配置文件中的配置会覆盖前面的配置。
需要注意的是Mysql会判断配置文件的权限,如果其他用户组也有文件写权限,则会忽略此配置文件。具体方法如下:
/**
Check file permissions of the option file.
@param file_name [in] Name of the option file.
@param is_login_file [in] TRUE, when login file is being processed.
@return 0 - Non-allowable file permissions.
1 - Failed to stat.
2 - Success.
*/
int check_file_permissions(const char *file_name, my_bool is_login_file)
{
#if !defined(_WIN32)
MY_STAT stat_info;
if (!my_stat(file_name,&stat_info,MYF(0)))
return 1;
/*
Ignore .mylogin.cnf file if not exclusively readable/writable
by current user.
*/
if (is_login_file && (stat_info.st_mode & (S_IXUSR | S_IRWXG | S_IRWXO))
&& (stat_info.st_mode & S_IFMT) == S_IFREG)
{
my_message_local(WARNING_LEVEL, "%s should be readable/writable only by "
"current user.", file_name);
return 0;
}
/*
Ignore world-writable regular files.
This is mainly done to protect us to not read a file created by
the mysqld server, but the check is still valid in most context.
*/
else if ((stat_info.st_mode & S_IWOTH) &&
(stat_info.st_mode & S_IFMT) == S_IFREG)
{
my_message_local(WARNING_LEVEL,
"World-writable config file '%s' is ignored.", file_name);
return 0;
}
#endif
return 2; /* Success */
}
ps: 代码取自 mysql-server-5.7