MySQL服务读取参数文件my.cnf的规律研究探索

在MySQL中,它是按什么顺序或规律去读取my.cnf配置文件的呢?其实只要你花一点功夫,实验测试一下就能弄清楚,下面的实验环境为5.7.21 MySQL Community Server。其它版本如有不同,请以实际情况为准。

 

其实,MySQL是按照下面这个顺序来读取my.cnf:

 

    1: /etc/my.cnf

    2: /etc/mysql/my.cnf

    3: /usr/etc/my.cnf

    4: ~/.my.cnf

 

也就是说首先它会找/etc/my.cnf 这个文件, 如果这个文件不存在,那么它接下来去找/etc/mysql/my.cnf这个文件,依此类推(这个实验很简单,在此略过,不浪费篇幅),如果最后一个文件~/.my.cnf 也不存在,那么会怎么样呢?

[root@gettestlnx02 ~]# mysql --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf 
[root@gettestlnx02 ~]# ls /etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
ls: cannot access /etc/mysql/my.cnf: No such file or directory
ls: cannot access /usr/etc/my.cnf: No such file or directory
ls: cannot access /root/.my.cnf: No such file or directory
/etc/my.cnf
[root@gettestlnx02 ~]# 

 

如上所示,其实MySQL安装完成后,只生成了/etc/my.cnf这个配置文件。其它路径的my.cnf文件是不存在的。我们先来测试一下,将配置文件移走。在这之前,我们先查看一下log_error的位置。如下所示:

 

 

mysql> show variables like '%log_error%';
+---------------------+---------------------+
| Variable_name       | Value               |
+---------------------+---------------------+
| binlog_error_action | ABORT_SERVER        |
| log_error           | /var/log/mysqld.log |
| log_error_verbosity | 3                   |
+---------------------+---------------------+
3 rows in set (0.00 sec)
 
mysql> exit
Bye

 

 

[root@gettestlnx02 ~]# mv /etc/my.cnf  /tmp/my.cnf
[root@gettestlnx02 ~]# ls -lrt /etc/my.cnf
ls: cannot access /etc/my.cnf: No such file or directory
[root@gettestlnx02 ~]# service mysqld stop
Stopping mysqld:  [  OK  ]
[root@gettestlnx02 ~]# service mysqld start
Starting mysqld:  [  OK  ]
[root@gettestlnx02 ~]# 

 

 

clip_image001[4]

 

 

如上所示,即使没了my.cnf配置文件,MySQL服务依然可以启动,那么这个是怎么回事呢? 我们知道service mysqld start启动MySQL,其实是运行/etc/init.d/mysqld这个脚本。下面是脚本获取给变量datadir、socketfile、errlogifle赋值的部分脚本,如下所示:

 

# Extract value of a MySQL option from config files
# Usage: get_mysql_option OPTION DEFAULT SECTION1 SECTION2 SECTIONN
# Result is returned in $result
# We use my_print_defaults which prints all options from multiple files,
# with the more specific ones later; hence take the last match.
get_mysql_option () {
    option=$1
    default=$2
    shift 2  #移动到第3个参数,详情见下面调试。
    result=$(/usr/bin/my_print_defaults "$@" | sed -n "s/^--${option}=//p" | tail -n 1)
    if [ -z "$result" ]; then
        # not found, use default
        result="${default}"
    fi
}
 
get_mysql_option datadir "/var/lib/mysql" mysqld
datadir="$result"
get_mysql_option socket "$datadir/mysql.sock" mysqld
socketfile="$result"
get_mysql_option log-error "/var/log/mysqld.log" mysqld mysqld_safe
errlogfile="$result"
get_mysql_option pid-file "/var/run/mysqld/mysqld.pid" mysqld mysqld_safe
mypidfile="$result"

 

如果你对shell很熟,那么可以忽略下面步骤,如果不熟悉,那么我们可以手工调试一下(# sh -x mysqld),看看它是如何获取相关变量的值的呢?

 

 

clip_image002[4]

 

 

[root@gettestlnx02 ~]# file /usr/bin/my_print_defaults
/usr/bin/my_print_defaults: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped
[root@gettestlnx02 ~]# /usr/bin/my_print_defaults mysqld
[root@gettestlnx02 ~]# 

 

 

如上所示,因为/usr/bin/my_print_defaults mysqld输出为空,所以result为空值, 所以result被授予${default}的值,而defualt=$2,其实就是第二个变量,如下所示,第二个变量被标记为红色。

 

get_mysql_option datadir "/var/lib/mysql" mysqld

datadir="$result"

 

 

clip_image003[4]

 

另外,my.cnf的位置是会影响脚本输出结果的。如下所示:(不在几个默认路径的话,my_print_defaults是没有输出结果的)

 

[root@gettestlnx02 ~]# ls /tmp/my.cnf
/tmp/my.cnf
[root@gettestlnx02 ~]# /usr/bin/my_print_defaults mysqld
[root@gettestlnx02 ~]# mv /tmp/my.cnf  /etc/my.cnf
[root@gettestlnx02 ~]# /usr/bin/my_print_defaults mysqld
--datadir=/var/lib/mysql
--socket=/var/lib/mysql/mysql.sock
--symbolic-links=0
--log-error=/var/log/mysqld.log
--pid-file=/var/run/mysqld/mysqld.pid

 

 

 

clip_image004[4]

 

 

接下来,我们将配置文件my.cnf挪回原位(/etc/my.cnf),然后更改数据库数据存储目录(从/var/lib/mysql挪动到/data/mysqldata/mysql 步骤从略),然后我们再做下面测试:

 

 

[root@gettestlnx02 ~]# service mysqld stop
Stopping mysqld:  [  OK  ]
[root@gettestlnx02 ~]# mv /etc/my.cnf  /tmp/my.cnf
[root@gettestlnx02 ~]# service mysqld start
Initializing MySQL database:  2018-03-16T01:26:19.589182Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-03-16T01:26:20.034494Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-03-16T01:26:20.132219Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-03-16T01:26:20.193504Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 07ee8c42-28b9-11e8-a04a-005056b3ebdf.
2018-03-16T01:26:20.208662Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-03-16T01:26:20.209919Z 1 [Note] A temporary password is generated for root@localhost: O;kZmIj+.6jf
[  OK  ]
Logging to '/var/lib/mysql/gettestlnx02.err'.
Starting mysqld:  [  OK  ]

 

clip_image005[4]

 

 

MySQL服务居然也启动了,它初始化了数据库,数据文件位于/var/lib/mysql。这个确实让我吃了一惊,原本预测,如果更改数据库数据存储目录,MySQL又没有my.cnf配置文件,MySQL服务应该启动不了。当然这个启动也没有什么意义,因为你的数据和一些账号权限配置都没有了(有点类似于SQL Server里面的重建系统数据库)

 

[root@gettestlnx02 mysql]# cd /var/lib/mysql
[root@gettestlnx02 mysql]# ls -lrt
total 122948
-rw-r-----. 1 mysql mysql 50331648 Mar 16 09:26 ib_logfile1
-rw-r-----. 1 mysql mysql       56 Mar 16 09:26 auto.cnf
drwxr-x---. 2 mysql mysql     4096 Mar 16 09:26 performance_schema
drwxr-x---. 2 mysql mysql     4096 Mar 16 09:26 mysql
drwxr-x---. 2 mysql mysql    12288 Mar 16 09:26 sys
-rw-r-----. 1 mysql mysql      420 Mar 16 09:26 ib_buffer_pool
-rw-------. 1 mysql mysql     1679 Mar 16 09:26 ca-key.pem
-rw-r--r--. 1 mysql mysql     1107 Mar 16 09:26 ca.pem
-rw-------. 1 mysql mysql     1675 Mar 16 09:26 server-key.pem
-rw-r--r--. 1 mysql mysql     1107 Mar 16 09:26 server-cert.pem
-rw-------. 1 mysql mysql     1675 Mar 16 09:26 client-key.pem
-rw-r--r--. 1 mysql mysql     1107 Mar 16 09:26 client-cert.pem
-rw-------. 1 mysql mysql     1675 Mar 16 09:26 private_key.pem
-rw-r--r--. 1 mysql mysql      451 Mar 16 09:26 public_key.pem
-rw-------. 1 mysql mysql        6 Mar 16 09:26 mysql.sock.lock
srwxrwxrwx. 1 mysql mysql        0 Mar 16 09:26 mysql.sock
-rw-r-----. 1 mysql mysql 12582912 Mar 16 09:26 ibtmp1
-rw-r-----. 1 mysql mysql 12582912 Mar 16 09:26 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Mar 16 09:26 ib_logfile0
-rw-r-----. 1 mysql mysql     3277 Mar 16 09:27 gettestlnx02.err
[root@gettestlnx02 mysql]# 

 

测试、折腾过程发现并不是所有情况下都会成功初始化数据库,如果当/var/lib/mysql下存在一些文件时,初始化会报错initialize specified but the data directory has files in it. Aborting.此时,只要你清空

/var/lib/mysql下文件,就能成功初始化。

 

[root@gettestlnx02 ~]# service mysqld stop

[root@gettestlnx02 ~]# mv /etc/my.cnf  /tmp/my.cnf

[root@gettestlnx02 ~]# service mysqld start

Initializing MySQL database:  2018-03-16T03:49:45.190114Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2018-03-16T03:49:45.192215Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.

2018-03-16T03:49:45.192246Z 0 [ERROR] Aborting

 

[FAILED]

[root@gettestlnx02 ~]#

 

 

clip_image006[4]

 

其实只要稍微花费一点心思,查看一下/etc/init.d/mysqld的代码,就会发现start函数里面在条件满足的情况就会初始化数据库。

 

start(){
    [ -x $exec ] || exit 5
    # check to see if it's already running
    RESPONSE=$(/usr/bin/mysqladmin --no-defaults --socket="$adminsocket" --user=UNKNOWN_MYSQL_USER ping 2>&1)
    if [ $? = 0 ]; then
        # already running, do nothing
        action $"Starting $prog: " /bin/true
        ret=0
    elif echo "$RESPONSE" | grep -q "Access denied for user"
    then
        # already running, do nothing
        action $"Starting $prog: " /bin/true
        ret=0
    else
        # prepare for start
        if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a "x$(dirname "$errlogfile")" = "x/var/log" ]; then
            install /dev/null -m0640 -omysql -gmysql "$errlogfile"
        fi
        [ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile"
        if [ ! -d "$datadir/mysql" ] ; then
            # First, make sure $datadir is there with correct permissions
            if [ ! -d "$datadir" -a ! -h "$datadir" -a "x$(dirname "$datadir")" = "x/var/lib" ]; then
                install -d -m0751 -omysql -gmysql "$datadir" || exit 1
            fi
            if [ ! -h "$datadir" -a "x$(dirname "$datadir")" = "x/var/lib" ]; then
                chown mysql:mysql "$datadir"
                chmod 0751 "$datadir"
            fi
            if [ -x /sbin/restorecon ]; then
                /sbin/restorecon "$datadir"
                for dir in /var/lib/mysql-files /var/lib/mysql-keyring ; do
                    if [ -x /usr/sbin/semanage -a -d /var/lib/mysql -a -d $dir ] ; then
                        /usr/sbin/semanage fcontext -a -e /var/lib/mysql $dir >/dev/null 2>&1
                        /sbin/restorecon -r $dir
                    fi
                done
            fi
            # Now create the database
            initfile="$(install_validate_password_sql_file)"
            action $"Initializing MySQL database: " /usr/sbin/mysqld --initialize --datadir="$datadir" --user=mysql --init-file="$initfile"
            ret=$?
            rm -f "$initfile"
            [ $ret -ne 0 ] && return $ret
            # Generate certs if needed
            if [ -x /usr/bin/mysql_ssl_rsa_setup -a ! -e "${datadir}/server-key.pem" ] ; then
                /usr/bin/mysql_ssl_rsa_setup --datadir="$datadir" --uid=mysql >/dev/null 2>&1
            fi

 

 

另外,在多实例情况下,多实例有两种方案:

 

1、基于mysqld_multi: 多个实例共用同一个my.cnf配置文件中,利用[mysqld1]、[mysqld2]、[mysqld*]标签实现不同实例的差异化配置;

 

2、基于多配置文件:每一个实例单独一个my.cnf配置文件

 

 

多实例启动时都会指定对应的my.cnf,所以虽然这里没有详细测试,其实大致的原理也跟单实例是差不多的。当你有疑问或不解的时候,动手实践是检验真理的唯一标准。

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278280.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

将组策略编辑器添加到控制面板

If you find yourself using the Group Policy Editor all the time, you might have wondered why it doesn’t show up in the Control Panel along with all the other tools. After many hours of registry hacking, I’ve come up with a registry tweak to let you do ju…

Exchange Server 2016管理系列课件50.DAG管理之激活数据库副本

激活邮箱数据库副本是将特定被动副本指定为邮箱数据库的新主动副本的过程。我们将此过程称为数据库切换。数据库切换过程是指卸除当前的活动数据库,然后在指定的服务器上将相应的数据库副本作为新的活动邮箱数据库副本进行装载。成为活动邮箱数据库的数据库副本必须…

常见设计模式 (python代码实现)

1.创建型模式 单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在。当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场。 比如&#…

记录一次解决httpcline请求https报handshake_failure错误

概述 当使用httpclinet发起https请求时报如下错误: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failureat com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)at com.sun.net.ssl.internal.ssl.Alerts.getSSLExcep…

桌面程序explorer_备份Internet Explorer 7搜索提供程序列表

桌面程序explorerIf you are both an IE user and a fan of using custom search providers in your search box, you might be interested to know how you can back up that list and/or restore it on another computer. Yes, this article is boring, but we’re trying to…

GreenPlum数据库故障恢复测试

本文介绍gpdb的master故障及恢复测试以及segment故障恢复测试。 环境介绍:Gpdb版本:5.5.0 二进制版本操作系统版本: centos linux 7.0Master segment: 192.168.1.225/24 hostname: mfsmasterStadnby segemnt: 192.168.1.227/24 hostname: ser…

书评:Just the Computer Essentials(Vista)

Normally we try and focus on articles about how to customize your computer, but today we’ll take a break from that and do a book review. This is something I’ve not done before, so any suggestions or questions will be welcomed in the comments. 通常&#x…

python学习

为了学会能学&#xff0c;不负时间&#xff0c;为了那简练的美&#xff01; 为了一片新天地。 /t 对齐 python : # 99乘法表i 0while i < 9 : i 1 j 0 while j < i : j 1 print(j ,* , i,"" , i*j , end\t) #空格不能对齐 制表符…

hey 安装_如何在助理扬声器上调整“ Hey Google”的灵敏度

hey 安装Vantage_DS/ShutterstockVantage_DS / ShutterstockThe Google Assistant is a useful tool that allows you to control your smart home, check the weather, and more. Unfortunately, the Assistant might not hear you in a noisy environment or it might activa…

EXCEL如何进行多条件的数据查找返回

在使用EXCEL时经常会碰到一个表里的同一款产品每天的销量都不一样&#xff0c;然后我们需要查导出每一款产品每天的销量&#xff0c;即一对多条件查找。这个教复杂&#xff0c;我们要用到好几个函数的综合&#xff0c;下面小编来教你吧。 工具/原料 EXCEL软件&#xff08;本文使…

如何将Google幻灯片转换为PowerPoint

If someone sent you a presentation on Google Slides, but you’d rather work on it in Microsoft PowerPoint, you can easily convert it to a .pptx file in just a few simple steps. Here’s how it’s done. 如果有人在Google幻灯片上向您发送了演示文稿&#xff0c;但…

XP调整禁用页面文件

NOTE: You shouldn’t disable your page file unless you really really know what you are doing. 注意&#xff1a;除非您真的很清楚自己在做什么&#xff0c;否则不应该禁用页面文件。 If your computer has 1 GB of RAM or more, disabling the page file can speed up XP…

如何在Windows 10的命令提示符中更改目录

One of the first things you’ll need to learn as you become more familiar with Command Prompt on Windows 10 is how to change directories in the operating system’s file system. There are a few ways you can do this, so we’ll walk you through them. 随着您对…

MySQL优化总结

2019独角兽企业重金招聘Python工程师标准>>> 从这几天看MySQL性能优化来看&#xff0c;基本的思路就是分分分&#xff0e;&#xff0e;&#xff0e; 1&#xff0c;分读&#xff0c;用缓存来分摊读表的压力; 2&#xff0c;读写分离&#xff0c;主从分离&#xff0c;写…

将背景色添加到Word 2007文档中

Instead of using the standard white background with Word documents, here is how to add some background color to spice up your documents. 代替在Word文档中使用标准的白色背景&#xff0c;这是如何添加一些背景颜色来为文档增添色彩。 Open your word document and ch…

jquery实现增删改(伪)-老男孩作业day13

使用jquery进行&#xff0c;文件的编写&#xff0c;实现自增id,删除&#xff0c;添加&#xff0c;编辑模式。 jquery放在本地&#xff0c;src"jquery_js.js" 可以改成其他&#xff0c;或者在线的路径 readme<!DOCTYPE html> <html lang"en"> &…

uoj#119. 【UR #8】决战圆锥曲线(线段树+复杂度分析)

题解 传送门 题解 然而要我来说我感觉只是个爆搜啊…… //minamoto #include<bits/stdc.h> #define R register #define ll long long #define ls (p<<1) #define rs (p<<1|1) #define fp(i,a,b) for(R int ia,Ib1;i<I;i) #define fd(i,a,b) for(R int ia…

如何在Raspberry Pi上设置两因素身份验证

Kiklas/ShutterstockKiklas /快门The Raspberry Pi is everywhere now, which is why it’s caught the eye of threat actors and cybercriminals. We’ll show you how to secure your Pi with two-factor authentication. Raspberry Pi现在无处不在&#xff0c;这就是为什么…

vue 开发环境搭建

1.创建vue项目 1.node js 生成项目&#xff0c;编译项目 2.hbuilder 开发环境 1.下载安装node js http://nodejs.cn/download/ 确认是否安装成功 如果安装不了 代码错误2503 解决方法&#xff1a; 管理员命令运行cmd; cd\ cd C:\Users\Administrator\Desktop msiexec/package n…

iphone视图放大_如何将iPhone用作放大镜

iphone视图放大It’s a common problem: Some things are just too hard to see. Usually, they’re too far away, too dark, or too small. With a feature called Magnifier, your iPhone can function as a magnifying glass and sight aid. Here’s how to use it. 这是一…