mysql懸案 之 為什么用docker啟動的mysql配置文件不生效


 

 

故事前景

接了個私活,需要安裝canal,canal需要mysql開啟binlog功能,查看了mysql的配置文件,看到已經寫了log_bin參數,此時進入mysql,執行sql語句確認binlog功能是否為ON [sql語句:show variables like 'log_bin';],結果顯示為OFF,於是開啟了排查之路

查看docker啟動時掛載了哪些目錄

docker inspect 9e33b294e948 | grep Binds -A 4

預期出現類似如下的輸出,以本地實際環境為准

docker run啟動的時候,-v參數所掛載的目錄,會在docker inspectBinds這塊找到

"Binds": [
    "/etc/localtime:/etc/localtime:ro",
    "/data/mysql-test/conf:/etc/mysql",
    "/data/mysql-test/data:/var/lib/mysql"
],

這時,查看一下本地持久化配置文件的目錄,發現,只有一個my.cnf文件

問題就出現在這一塊:本地直接使用yum安裝的mysql默認的配置文件存儲路徑是/etc/mysql/my.cnf

但是docker容器其實並非如此

# tree /data/mysql-test/conf
/data/mysql-test/conf
└── my.cnf

使用相同鏡像啟動一個mysql

因為只是查看一下mysql的配置文件情況,就簡單的啟動mysql即可

如果不給-e MYSQL_ROOT_PASSWORD=root參數,容器無法在后台運行,就無法把配置文件獲取到宿主機

docker run -d -e MYSQL_ROOT_PASSWORD=root  mysql:5.7

新建一個目錄用來存放容器內的mysql配置文件

mkdir -p /data/mysql-new/conf

復制容器內的mysql配置文件到本地

docker cp <容器ID>:/etc/mysql/ /data/mysql-new/conf/

查看mysql配置文件目錄結構

為什么要拿到本地?

反正也要拿到本地重新掛載,早晚都要拿,總不能手擼配置文件吧

# tree /data/mysql-new/conf/
/data/mysql-new/conf/
├── conf.d
│   ├── docker.cnf
│   ├── mysql.cnf
│   └── mysqldump.cnf
├── my.cnf -> /etc/alternatives/my.cnf
├── my.cnf.fallback
├── mysql.cnf
└── mysql.conf.d
    └── mysqld.cnf

那么問題來了,這么多文件,到底哪個才是默認的配置文件呢,那就一個個看吧

conf/conf.d/docker.cnf

[mysqld]
skip-host-cache
skip-name-resolve

conf/conf.d/mysql.cnf

[mysql]

conf/conf.d/mysqldump.cnf

[mysqldump]
quick
quote-names
max_allowed_packet      = 16M

conf/my.cnf

這個文件在本地看不了,因為他是一個軟連接文件,文件鏈接的路徑是/etc/alternatives/my.cnf

/etc/alternatives/my.cnf這個文件也是一個軟連接文件,文件的連接路徑是/etc/mysql/mysql.cnf

咱也不知道官方為啥要這樣套娃,咱也不敢問

conf/my.cnf.fallback

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

!includedir /etc/mysql/conf.d/

conf/mysql.cnf

# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

mysql.conf.d/mysqld.cnf

# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
#log-error      = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

真假配置文件已經顯而易見了

docker容器啟動的mysql默認的配置文件其實是/etc/mysql/mysql.conf.d/mysqld.conf

因此,如果需要將本地配置文件掛載到容器里面,只需要掛載這一個文件即可,此時我們修改本地的mysql.conf.d/mysqld.conf文件,開啟binlog,並驗證是否修改成功

啟動mysql容器

精簡一下本地mysql配置文件目錄,就保留一個mysqld.cnf文件即可

# tree /data/mysql-new/conf/
/data/mysql-new/conf/
└── mysqld.cnf

mysqld.cnf文件最后加上這兩行,用來開啟binlog日志

log_bin=mysql-bin
server_id=33091

啟動mysql容器

docker run -d \
-e MYSQL_ROOT_PASSWORD=root \
-v /etc/localtime:/etc/localtime \
-v /data/mysql-new/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \
-v /data/mysql-new/data:/var/lib/mysql \
-p 3309:3306 \
--name mysql-new \
mysql:5.7

數據庫就不進去了,直接使用-e參數將結果返回到終端頁面

# mysql -uroot -p -P3309 -h192.168.100.200 -e "show variables like 'log_bin';"
Enter password:
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
  • 此時,找到了為何已經啟動的mysql容器加載不到配置文件的原因了

  • 同時,也學到了一個新的經驗,當容器需要持久化的時候,最好是簡單啟動一下這個容器,查看一下持久化目錄的結構以及是否存在依賴的情況,根據實際來選擇到底是目錄掛載,還是單配置文件掛載,避免本地錯誤目錄結構覆蓋了容器內的目錄結構,當一些配置沒有更新的時候,排查真的很頭疼

  • 后續將會在頭腦清醒的時候去修復已經啟動的mysql環境,預知后事如何,請看下集 [填別人留下的坑,真的難頂]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM