問(wèn)題描述:
系統(tǒng)版本:Ubuntu 16.04
MySQL版本:5.7.17
安裝方式,通過(guò) MySQL APT 源安裝。(MySQL官網(wǎng)下載)
只要開(kāi)啟二進(jìn)制日志 log-bin 選項(xiàng),就啟動(dòng)失敗,而且在 mysql 自己的錯(cuò)誤日志里面沒(méi)有日志信息。
問(wèn)題復(fù)現(xiàn):
安裝好 MySQL 后,以默認(rèn)配置可以正常啟動(dòng) mysql.
嘗試開(kāi)啟二進(jìn)制日志:log-bin=mysql-bin
啟動(dòng)失敗!
/etc/mysql/mysql.conf.d# /etc/init.d/mysql start
[....] Starting mysql (via systemctl): mysql.serviceJob for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
failed!
經(jīng)歷幾小時(shí)查找原因,找到解決辦法,添加如下選項(xiàng)
server-id=1
試了下確實(shí)可以了。
然后回來(lái)看官方文檔是否又說(shuō)明,真的有!
In MySQL 5.7.2 and earlier, if you start a master server without using --server-id to set its ID, the default ID is 0. In this case, the master refuses connections from all slaves, slaves refuse to connect to the master, and the server sets the server_id system variable to 1. In MySQL 5.7.3 and later, the --server-id must be used if binary logging is enabled, and a value of 0 is not changed by the server. If you specify --server-id without an argument, the effect is the same as using 0. In either case, if the server_id is 0, binary logging takes place, but slaves cannot connect to the master, nor can any other servers connect to it as slaves. (Bug #11763963, Bug #56718)
MySQL 5.7.3 版本后,如果要開(kāi)啟二進(jìn)制日志,必須同時(shí)開(kāi)啟 --server-id 選項(xiàng)。
server-id 不要設(shè)置為0(不支持主從復(fù)制),應(yīng)該設(shè)置為 1 或者其他值。
補(bǔ)充
之前奇怪為什么 mysql.err 里面沒(méi)有記錄錯(cuò)誤日志。
今天看到這篇文章:AppArmor and MySQL
https://blogs.oracle.com/jsmyth/entry/apparmor_and_mysql
才明白或許應(yīng)該去 /var/log/syslog 查看日志。然后嘗試重現(xiàn)昨天的錯(cuò)誤,也就是開(kāi)啟 log-bin 選項(xiàng),但是沒(méi)有開(kāi)啟 server-id 選項(xiàng),然后 tail -f 觀察 syslog 的日志輸出:
Mar 17 09:57:58 guli-Ubuntu1 mysqld[2825]: 2017-03-17T01:57:58.021561Z 0 [ERROR] You have enabled the binary log, but you haven't provided the mandatory server-id. Please refer to the proper server start-up parameters documentation
Mar 17 09:57:58 guli-Ubuntu1 mysqld[2825]: 2017-03-17T01:57:58.022512Z 0 [ERROR] Aborting
Mar 17 09:57:58 guli-Ubuntu1 mysqld[2825]: Initialization of mysqld failed: 0
Mar 17 09:57:58 guli-Ubuntu1 mysqld[2825]: 2017-03-17T01:57:58.029601Z 0 [Note] Binlog end
Mar 17 09:57:58 guli-Ubuntu1 mysqld[2825]: 2017-03-17T01:57:58.043989Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
果然是在這里輸出錯(cuò)誤日志。而且說(shuō)得很明白 you haven't provided the mandatory server-id。
.. 昨天花了那么多時(shí)間在網(wǎng)上找原因..真是一個(gè)教訓(xùn),以后遇到啟動(dòng)時(shí)出問(wèn)題,一定要看看 syslog 的記錄。
還沒(méi)完。
這個(gè)坑可以繞過(guò)去了。但要在 Ubuntu 下定制自己的數(shù)據(jù)目錄,還有一個(gè)坑,這個(gè)坑跟 apparmor 有關(guān)系。
mysql 和 apparmor 就再另開(kāi)一篇博文來(lái)講了。