今天准备新启一个MySQL实例,结果竟然无法初始化,内容如下:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[root@hadoop-node-2 ~]# cat /etc/issue
CentOS release 6.4 (Final)
[root@hadoop-node-2 ~]# mysql --version
./bin/mysql Ver 14.14 Distrib 5.6.24, for linux-glibc2.5 (x86_64) using EditLine wrapper
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
报错如下:
[root@hadoop-node-2 mysql56]# ./scripts/mysql_install_db --defaults-file=/data/db/mysql/3310/my.cnf
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.
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 3310
mysql 47574 5163 18 10:15 pts/2 00:00:03 /data/mysql56/bin/mysqld --defaults-file=/data/db/mysql/3310/my.cnf