很多童鞋反映,在Docker官方CentOS鏡像中安裝了Mysql server后,無法正常啟動。
無法正常啟動表現為兩種情況:
1> 初始完數據庫后,mysqld啟動報錯
2> systemctl start mysqld或者service mysqld start報錯
首先重現一下現場。
第一種情況
一、啟動CentOS鏡像,安裝Mysql Server
注意,Docker官方CentOS鏡像latest版本是7.1。CentOS 7 yum源中默認沒有Mysql Server的。
關於如何在CentOS 7中安裝Mysql Server,可參考這篇博客 CentOS 7中如何安裝mysql server
二、初始化數據庫
[root@e80a5553b647 ~]# mysql_install_db
三、啟動Mysqld服務
[root@e80a5553b647 ~]# mysqld 2015-09-25 03:46:43 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2015-09-25 03:46:43 0 [Note] mysqld (mysqld 5.6.26) starting as process 775 ... 2015-09-25 03:46:43 775 [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root! 2015-09-25 03:46:43 775 [ERROR] Aborting 2015-09-25 03:46:43 775 [Note] Binlog end 2015-09-25 03:46:43 775 [Note] mysqld: Shutdown complete
報以上錯誤。很多童鞋到這一步就不知所措了,怎么會啟動失敗呢?但細心的童鞋看到報錯信息,就知道失敗的原因在於mysqld命令是用roor身份執行的。
四、嘗試以mysql身份啟動Mysqld服務
[root@e80a5553b647 ~]# mysqld --user=mysql 2015-09-25 02:56:43 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2015-09-25 02:56:43 0 [Note] mysqld (mysqld 5.6.26) starting as process 167 ... 2015-09-25 02:56:43 167 [Note] Plugin 'FEDERATED' is disabled. mysqld: Can't find file: './mysql/plugin.frm' (errno: 13 - Permission denied) 2015-09-25 02:56:43 167 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it. 2015-09-25 02:56:43 167 [Note] InnoDB: Using atomics to ref count buffer pool pages 2015-09-25 02:56:43 167 [Note] InnoDB: The InnoDB memory heap is disabled 2015-09-25 02:56:43 167 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2015-09-25 02:56:43 167 [Note] InnoDB: Memory barrier is not used 2015-09-25 02:56:43 167 [Note] InnoDB: Compressed tables use zlib 1.2.3 2015-09-25 02:56:43 167 [Note] InnoDB: Using Linux native AIO 2015-09-25 02:56:43 167 [Note] InnoDB: Using CPU crc32 instructions 2015-09-25 02:56:43 167 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2015-09-25 02:56:43 167 [Note] InnoDB: Completed initialization of buffer pool 2015-09-25 02:56:43 167 [ERROR] InnoDB: ./ibdata1 can't be opened in read-write mode 2015-09-25 02:56:43 167 [ERROR] InnoDB: The system tablespace must be writable! 2015-09-25 02:56:43 167 [ERROR] Plugin 'InnoDB' init function returned error. 2015-09-25 02:56:43 167 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed. 2015-09-25 02:56:43 167 [ERROR] Unknown/unsupported storage engine: InnoDB 2015-09-25 02:56:43 167 [ERROR] Aborting 。。。。。
還是啟動失敗。
第二種情況
以systemctl啟動,
[root@e80a5553b647 ~]# systemctl start mysqld Failed to get D-Bus connection: No connection to service manager. [root@e80a5553b647 ~]# service mysqld start Starting mysqld (via systemctl): Failed to get D-Bus connection: No connection to service manager. [FAILED]
報“Failed to get D-Bus connection: No connection to service manager.”錯誤,在網上找了好久,原因在於該CentOS鏡像為精簡版,有很多包再制作的過程中沒有安裝。故導致systemctl命令無法啟動。
基於第二種情況,很多童鞋就認為CentOS鏡像不完善,導致mysql服務無法啟動。
失敗原因:
深究下去,第一種方式失敗的原因在於第二步初始化數據庫的時候是用的ROOT賬戶運行的。這樣,會導致數據庫的datadir(即/var/lib/mysql)目錄的屬主為root。
[root@e80a5553b647 ~]# ll /var/lib/mysql/ total 110600 -rw-rw---- 1 root root 50331648 Sep 25 04:46 ib_logfile0 -rw-rw---- 1 root root 50331648 Sep 25 04:46 ib_logfile1 -rw-rw---- 1 root root 12582912 Sep 25 04:46 ibdata1 drwx------ 2 root root 4096 Sep 25 04:46 mysql drwx------ 2 root root 4096 Sep 25 04:46 performance_schema
因為mysqld在以ROOT賬戶執行時會出錯,這個與數據庫初始化無關,而是數據庫基於安全的考慮,不推薦使用ROOT賬戶啟動數據庫 !!!
而此時,如果指定mysql用戶運行mysqld命令,因為var/lib/mysql目錄的屬主為root,必然報出“mysqld: Can't find file: './mysql/plugin.frm' (errno: 13 - Permission denied)”錯誤。
正確的啟動方式:
主要有以下兩種:
1> 初始化數據庫時指定以mysql用戶運行,即 mysql_install_db --user=mysql
啟動mysql服務,同樣有兩種方式:
(1) mysqld --user=mysql
(2) mysqld_safe
2> 以 /etc/init.d/mysqld start 方式啟動
總結:
1> 如果第一次以mysql_install_db初始化數據庫,mysqld --user=mysql啟動mysql服務失敗后,再次用mysql_install_db --user=mysql初始化數據庫,還是會啟動失敗,其實看看來看看/var/lib/mysql/的屬主就知道了,
[root@e80a5553b647 /]# ll /var/lib/mysql/ total 110600 -rw-rw---- 1 root root 12582912 Sep 25 05:57 ibdata1 -rw-rw---- 1 root root 50331648 Sep 25 05:57 ib_logfile0 -rw-rw---- 1 root root 50331648 Sep 25 05:57 ib_logfile1 drwx------ 2 mysql mysql 4096 Sep 25 05:57 mysql drwx------ 2 root root 4096 Sep 25 05:57 performance_schema
只有mysql目錄的屬主變為mysql了,其它依舊是root,可通過chown -R mysql:mysql /var/lib/mysql重新設置目錄的屬性。
2> 啟動mysql服務失敗的原因還是在於對mysql不熟悉,建議看看mysql服務腳本,即/etc/init.d/mysqld。
3> 關於mysql的啟動方式和停止方式(與docker無關)
啟動方式主要有以下三種:
(1)使用service啟動
service mysqld start 在CentOS7中,相當於systemctl start mysqld
(2)使用腳本啟動
/etc/inint.d/mysqld start
(3) 使用safe_mysqld或mysqld --user=mysql啟動
關閉方式也有以下三種:
(1)使用service關閉
service mysqld stop 在CentOS7中,相當於systemctl stop mysqld
(2)使用腳本關閉
/etc/inint.d/mysqld stop
(3)mysqladmin shutdown
注意:使用safe_mysqld或mysqld --user=mysql啟動的服務,只能通過mysqladmin shutdown關閉,不能通過service或腳本關閉。
mysqladmin shutdown可關閉以上三種服務。腳本可關閉service開啟的服務,同樣service也可關閉腳本開啟的服務。
