MySQL初始化故障-----mysql_config_editor中的坑

今天準備新啓一個MySQL實例,結果居然沒法初始化,內容以下:node

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------mysql

[root@hadoop-node-2 ~]# cat /etc/issue
CentOS release 6.4 (Final)linux

[root@hadoop-node-2 ~]# mysql --version
./bin/mysql  Ver 14.14 Distrib 5.6.24, for linux-glibc2.5 (x86_64) using  EditLine wrappersql

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------數據庫

報錯以下:安全

[root@hadoop-node-2 mysql56]# ./scripts/mysql_install_db  --defaults-file=/data/db/mysql/3310/my.cnfapp

Installing MySQL system tables...2016-04-28 11:07:37 0 [Warning] Ignoring user change to 'root' because the user was set to 'mysql' earlier on the command line工具

 

2016-04-28 11:07:37 0 [Warning] Using unique option prefix host instead of host_cache_size is deprecated and will be removed in a future release. Please use the full name instead.oop

Unknown suffix '.' used for variable 'host_cache_size' (value '127.0.0.1')測試

2016-04-28 11:07:37 0 [ERROR] ./bin/mysqld: Error while setting value '127.0.0.1' to 'host_cache_size'

2016-04-28 11:07:37 0 [ERROR] Aborting

換種方法初始化,繼續報錯:

[root@hadoop-node-2 mysql56]# ./scripts/mysql_install_db --no-defaults --datadir=/data/db/mysql/3310 --basedir=/data/mysql56 --user=mysql --skip_host_cache

Installing MySQL system tables...2016-04-28 15:55:24 0 [Warning] Using unique option prefix host instead of host_cache_size is deprecated and will be removed in a future release. Please use the full name instead.
Unknown suffix '.' used for variable 'host_cache_size' (value '127.0.0.1')
2016-04-28 15:55:24 0 [ERROR] /data/mysql56/bin/mysqld: Error while setting value '127.0.0.1' to 'host_cache_size'
2016-04-28 15:55:24 0 [ERROR] Aborting

2016-04-28 15:55:24 0 [Note] Binlog end

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

這種報錯通常是加載了非法的配置文件致使的

用strace跟蹤一下mysql_install_db

[root@hadoop-node-2 mysql56]# strace ./scripts/mysql_install_db  --defaults-file=/data/db/mysql/3310/my.cnf  沒發現問題

接着發現mysqld居然沒法啓動了,接着跟蹤mysqld啓動

[root@hadoop-node-2 mysql56]# strace ./bin/mysqld  --defaults-file=/data/db/mysql/3310/my.cnf 

發現其中有這樣一段:

stat("/root/.mylogin.cnf", {st_mode=S_IFREG|0600, st_size=176, ...}) = 0
open("/root/.mylogin.cnf", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0600, st_size=176, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd8d4a72000
lseek(3, 0, SEEK_CUR) = 0
lseek(3, 0, SEEK_SET) = 0
read(3, "\0\0\0\0", 4) = 4
read(3, "\6\f\30\36\35\21\5\34\22\24\16\r\37\22\16\5\r\34\27\6\20\0\0\0\301\335\346\23d\375\255\344"..., 4096) = 172
read(3, "", 4096) = 0
close(3) = 0

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

處理方法:

刪除文件/root/.mylogin.cnf,再次嘗試數據庫初始化,成功!(!!!刪除前記得確認想刪的文件是否是能夠隨便刪)

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

爲何該文件會阻止初始化呢?初步瞭解一下:

1. 這個文件哪來的?

在mysql5.6中新增了一個密碼安全工具:mysql_config_edit ,該工具的官方定義是:

The mysql_config_editor utility enables you to store authentication credentials in an encrypted login path file named .mylogin.cnf, The file can be read later by MySQL client programs to obtain authentication credentials for connecting to MySQL Server。

翻譯一下:mysql_config_editor容許用戶將登錄信息存儲在一個名爲.mylogin.cnf的加密文件中,這個文件中的加密信息能夠被MySQL讀取去鏈接MySQL數據庫(加一句:具體被哪一個路徑讀取能夠經過login-path來指定)

涉及到的文件就是咱們的罪魁禍首:.mylogin.cnf

2. 爲何該文件會阻止初始化,阻止mysqld?

先來看一下這個文件中的內容(想看.mylogin.cnf裏的內容須要用如下命令):

[root@hadoop-node-2 mysql]# mysql_config_editor print --all

[mysqld]
user = localuser
password = *****
host = 127.0.0.1

兩個緣由:(1)mysql_config_editor工具備一個參數--login-path,該參數能夠設置調用.mylogin.cnf中內容的路徑,在上面的故障中,很不幸,login-path=mysqld,在初始化進程中調用mysqld時候,mysqld                        就很聽話的去調取了該文件中的信息

               (2)mysql的參數名稱有個特性就是書寫能夠比較隨意,好比上面的參數host_cache_size,能夠寫做host-cache-size、host_cache,甚至host

結合這兩個緣由能夠知道,初始化進程調用了mysqld,mysqld加載了.mylogin.cnf中的信息,.mylogin.cnf中的host=127.0.0.1,mysqld就把值127.0.0.1賦予了參數host-cache-size,而host-cache-size的值是一個數值,這時候賦值不匹配出現了,最後進程受阻,把咱們坑了

之後須要注意:

(1).mylogin.cnf中的login-path默認是client,因此set時候就不要加這個參數了,用默認就好

(2)strace是個好工具

 

另附故障重現:

[root@hadoop-node-2 mysql56]# ps -ef|grep mysqld|grep 3310

設一個login-path=mysqld的.mylogin.cnf文件:

[root@hadoop-node-2 mysql56]# mysql_config_editor set --login-path=mysqld --host=127.0.0.1 --user=localuser --password

Enter password: enter password "localpass" here

[root@hadoop-node-2 mysql]# mysql_config_editor print --all

[mysqld]
user = localuser
password = *****
host = 127.0.0.1

啓動失敗:

[root@hadoop-node-2 mysql56]#/data/mysql56/bin/mysqld --defaults-file=/data/db/mysql/3310/my.cnf&

Installing MySQL system tables...2016-04-29 9:07:37 0 [Warning] Ignoring user change to 'root' because the user was set to 'mysql' earlier on the command line

 

2016-04-29 9:07:37 0 [Warning] Using unique option prefix host instead of host_cache_size is deprecated and will be removed in a future release. Please use the full name instead.

Unknown suffix '.' used for variable 'host_cache_size' (value '127.0.0.1')

2016-04-29 9:07:37 0 [ERROR] ./bin/mysqld: Error while setting value '127.0.0.1' to 'host_cache_size'

2016-04-29 9:07:37 0 [ERROR] Aborting

刪掉文件:

[root@hadoop-node-2 mysql56]#rm -f /root/.mylogin.cnf

從新啓動,成功

[root@hadoop-node-2 mysql56]#/data/mysql56/bin/mysqld --defaults-file=/data/db/mysql/3310/my.cnf&

......

[root@hadoop-node-2 mysql56]# ps -ef|grep mysqld|grep 3310
mysql 46532 5163 20 10:09 pts/2 00:00:03 /data/mysql56/bin/mysqld --defaults-file=/data/db/mysql/3310/my.cnf

殺掉進程,再測試下login-path=client的狀況:

[root@hadoop-node-2 mysql56]# kill 46532

[root@hadoop-node-2 mysql56]# ps -ef|grep mysqld|grep 3310

[root@hadoop-node-2 mysql56]# mysql_config_editor set --login-path=client  --host=127.0.0.1 --user=localuser --password

Enter password: enter password "localpass" here

[root@hadoop-node-2 mysql]# mysql_config_editor print --all

[client]
user = localuser
password = *****
host = 127.0.0.1

能夠成功啓動實例:

[root@hadoop-node-2 mysql56]#/data/mysql56/bin/mysqld --defaults-file=/data/db/mysql/3310/my.cnf&

......

[root@hadoop-node-2 mysql56]# ps -ef|grep mysqld|grep 3310mysql 47574 5163 18 10:15 pts/2 00:00:03 /data/mysql56/bin/mysqld --defaults-file=/data/db/mysql/3310/my.cnf

相關文章
相關標籤/搜索