MySQL啟動報錯問題排查:InnoDB: Unable to lock ./ibdata1 error
在OS X環境下MySQL啟動時報錯:
016-03-03T00:02:30.483037Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 35 2016-03-03T00:02:30.483100Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
終端不斷地重複列印上面的錯誤日誌,從錯誤日誌看起來似乎有另外一個mysqld程序佔用了./ibdata1檔案,於是使用ps命令檢視是否有mysqld程序在執行:
ps -ef |grep mysqld 74 7711 1 0 8:04上午 ?? 0:00.34 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid
發現有一個7711的程序在執行,於是強制kill掉:
sudo kill -9 7711
再次ps查詢:
ps -ef |grep mysqld 74 7759 1 0 8:10上午 ?? 0:00.29 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid
發現還在,只不過pid由原來的7711變成了現在的7759,那麼看看mysqld程序打開了哪些檔案:
lsof -c mysqld
該程序沒有開啟任何檔案,這就見鬼了。
Mac OS X, lsof only shows your own processes unless running as root with sudo
於是再次執行:
sudo lsof -c mysqld COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 8655 _mysql cwd DIR 1,4 544 3090250 /usr/local/mysql/data mysqld 8655 _mysql txt REG 1,4 31130736 3089789 /usr/local/mysql/bin/mysqld
的確發現有一個實實在在的mysqld程序在執行,也佔用的這些mysql檔案,經過一番Google大法,發現在OS X中啟動MySQL跟在Linux中啟動方式完全是牛馬不相及,在OS X中啟動/重啟MySQL的正確姿勢是:
sudo launchctl unload -w /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
此時再來看看是否還有mysqld程序:
ps -ef |grep mysqld
嗯,發現確實沒有了,再來啟動MySQL:
sudo launchctl load -w /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
問題總算解決,但還沒完,總得把原理搞清楚才行。
launchd是什麼?
launchd是Mac OS X從10.4開始引入,用於用於初始化系統環境的關鍵程序,它是核心裝載成功之後在OS環境下啟動的第一個程序。傳統的Linux會使用/etc/rc.*或者/etc/init來管理開機要啟動的服務,而在OS X中就是使用launchd來管理。採用這種方式來配置啟動項很簡單,只需要一個plist檔案。/Library/LaunchDaemons目錄下的plist檔案都是系統啟動後立即啟動程序。使用launchctl命令載入/解除安裝plist檔案,載入配置檔案後,程式啟動,解除安裝配置檔案後程序關閉。
解除安裝配置檔案後又嘗試直接用mysqld命令來啟動mysql程序試試:
/usr/local/mysql/bin/mysqld 2016-03-03T01:35:50.359258Z 0 [ERROR] InnoDB: ./ib_logfile0 can't be opened in read-write mode. 2016-03-03T01:35:50.359283Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error 2016-03-03T01:35:50.670517Z 0 [ERROR] Plugin 'InnoDB' init function returned error. 2016-03-03T01:35:50.670555Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed. 2016-03-03T01:35:50.670568Z 0 [ERROR] Failed to initialize plugins. 2016-03-03T01:35:50.670574Z 0 [ERROR] Aborting
ib_logfile0不能被開啟,猜測是使用者許可權檔案,不能用當前系統使用者啟動mysql。那麼加上sudo看看,用root來啟動:
2016-03-03T01:38:10.977313Z 0 [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root! 2016-03-03T01:38:10.977339Z 0 [ERROR] Aborting 2016-03-03T01:38:10.977350Z 0 [Note] Binlog end 2016-03-03T01:38:10.977410Z 0 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
叫我去讀MySQL的安全手冊,還是用launchd的方式啟動吧。
關注公眾號「Python之禪」(id:vttalk)獲取最新文章