mongodb非正常關閉無法啟動
剛剛接觸mongodb,遇到了個小問題,網上查到的資料好像都不是很全,總結記錄下,希望對大家有幫助。
一、問題描述
在重新配置mongodb的主從時,非正常關閉了數據庫【kill mongod進程】,結果在重新啟動時拋出錯誤如下
PS:正常應該是: mongod -f /mvnobss/users/mgousr01/mongodb/conf/master.conf --shutdown
[email protected]:/mvnobss/users/mgousr01/mongodb/bin>./start_mongodb.sh about to fork child process, waiting until server is ready for connections. forked process: 30717 ERROR: child process failed, exited with error number 100
二、問題解決
查看日誌:
2017-05-11T16:01:42.370+0800 I CONTROL ***** SERVER RESTARTED ***** 2017-05-11T16:01:42.404+0800 I JOURNAL [initandlisten] journal dir=/mvnobss/users/mgousr01/mongodb/dbdata/rs0/journal 2017-05-11T16:01:42.404+0800 I JOURNAL [initandlisten] recover begin 2017-05-11T16:01:42.404+0800 I JOURNAL [initandlisten] recover lsn: 75715637 2017-05-11T16:01:42.404+0800 I JOURNAL [initandlisten] recover /mvnobss/users/mgousr01/mongodb/dbdata/rs0/journal/j._23 2017-05-11T16:01:42.408+0800 I JOURNAL [initandlisten] recover skipping application of section seq:59812763 < lsn:75715637 2017-05-11T16:01:43.187+0800 I JOURNAL [initandlisten] recover skipping application of section seq:59872073 < lsn:75715637 2017-05-11T16:01:43.517+0800 I JOURNAL [initandlisten] recover /mvnobss/users/mgousr01/mongodb/dbdata/rs0/journal/j._24 2017-05-11T16:01:44.633+0800 I JOURNAL [initandlisten] recover /mvnobss/users/mgousr01/mongodb/dbdata/rs0/journal/j._25 2017-05-11T16:01:45.762+0800 I JOURNAL [initandlisten] recover /mvnobss/users/mgousr01/mongodb/dbdata/rs0/journal/j._26 2017-05-11T16:01:45.765+0800 I JOURNAL [initandlisten] recover skipping application of section seq:60050033 < lsn:75715637 2017-05-11T16:01:45.766+0800 F JOURNAL [initandlisten] dbexception during recovery: 15923 couldn‘t get file length when opening mapping /mvnobss/users/mgousr01/mongodb/dbdata/rs0/lxvomgodb1/lxvomgodb1.19 boost::filesystem::file_size: No such file or directory: "/mvnobss/users/mgousr01/mongodb/dbdata/rs0/lxvomgodb1/lxvomgodb1.19" 2017-05-11T16:01:45.766+0800 I STORAGE [initandlisten] exception in initAndListen: 15923 couldn‘t get file length when opening mapping /mvnobss/users/mgousr01/mongodb/dbdata/rs0/lxvomgodb1/lxvomgodb1.19 boost::filesystem::file_size: No such file or directory: "/mvnobss/users/mgousr01/mongodb/dbdata/rs0/lxvomgodb1/lxvomgodb1.19", terminating 2017-05-11T16:01:45.766+0800 I CONTROL [initandlisten] now exiting 2017-05-11T16:01:45.766+0800 I NETWORK [initandlisten] shutdown: going to close listening sockets... 2017-05-11T16:01:45.767+0800 I NETWORK [initandlisten] shutdown: going to flush diaglog... 2017-05-11T16:01:45.767+0800 I NETWORK [initandlisten] shutdown: going to close sockets... 2017-05-11T16:01:45.767+0800 I STORAGE [initandlisten] shutdown: waiting for fs preallocator... 2017-05-11T16:01:45.767+0800 I STORAGE [initandlisten] shutdown: final commit... 2017-05-11T16:01:45.767+0800 I STORAGE [initandlisten] shutdown: closing all files... 2017-05-11T16:01:45.767+0800 I STORAGE [initandlisten] closeAllFiles() finished 2017-05-11T16:01:45.767+0800 I CONTROL [initandlisten] dbexit: rc: 100
日誌說找不到/mvnobss/users/mgousr01/mongodb/dbdata/rs0/lxvomgodb1/lxvomgodb1.19文件,這個是正常的,因為我停庫之前備份數據庫後刪除了數據庫,常規情況是可以啟動起來的。
網上百度說是需要刪除非正常關閉後產生的mongod.lock文件再去重新啟動,好,刪除來一遍:
[email protected]:/mvnobss/users/mgousr01/mongodb/dbdata/rs0>ll total 20 drwxrwxr-x 3 mgousr01 mongodb 4096 2015-10-10 17:46 admin drwxrwxr-x 2 mgousr01 mongodb 4096 2017-05-11 11:12 journal drwxrwxr-x 3 mgousr01 mongodb 4096 2015-10-10 17:43 local drwxrwxr-x 3 mgousr01 mongodb 4096 2017-05-11 15:47 lxvomgodb1 -rwxrwxr-x 1 mgousr01 mongodb 0 2017-05-11 16:02 mongod.lock -rw-rw-r-- 1 mgousr01 mongodb 69 2015-10-10 17:09 storage.bson [email protected]
重新啟動依舊報錯,並且又生成了mongod.lock文件:
[email protected]:/mvnobss/users/mgousr01/mongodb/bin>./start_mongodb.sh about to fork child process, waiting until server is ready for connections. forked process: 30717 ERROR: child process failed, exited with error number 100 [email protected]:/mvnobss/users/mgousr01/mongodb/dbdata/rs0>ll total 20 drwxrwxr-x 3 mgousr01 mongodb 4096 2015-10-10 17:46 admin drwxrwxr-x 2 mgousr01 mongodb 4096 2017-05-11 11:12 journal drwxrwxr-x 3 mgousr01 mongodb 4096 2015-10-10 17:43 local drwxrwxr-x 3 mgousr01 mongodb 4096 2017-05-11 15:47 lxvomgodb1 -rwxrwxr-x 1 mgousr01 mongodb 0 2017-05-11 16:15 mongod.lock
試過了很多遍還是不行,繼續看日誌:
2017-05-11T16:15:42.379+0800 I CONTROL ***** SERVER RESTARTED ***** 2017-05-11T16:15:42.413+0800 I STORAGE [initandlisten] ************** 2017-05-11T16:15:42.413+0800 I STORAGE [initandlisten] Error: journal files are present in journal directory, yet starting without journaling enabled. 2017-05-11T16:15:42.413+0800 I STORAGE [initandlisten] It is recommended that you start with journaling enabled so that recovery may occur. 2017-05-11T16:15:42.413+0800 I STORAGE [initandlisten] ************** 2017-05-11T16:15:42.413+0800 I STORAGE [initandlisten] exception in initAndListen: 13597 can‘t start without --journal enabled when journal/ files are present, terminating 2017-05-11T16:15:42.413+0800 I CONTROL [initandlisten] dbexit: rc: 100
journal文件目前存在在journal目錄下,並且本次啟動過程沒有啟動journaling,由於啟動數據庫時伴隨啟動了journaling所以才會拋出錯誤。
於是進到journal目錄下,將非正常關閉時間點的journal文件刪除:
[email protected]:/mvnobss/users/mgousr01/mongodb/dbdata/rs0/journal>ls -lrt total 3179664 -rw------- 1 mgousr01 mongodb 1076690944 2017-05-11 11:11 j._23 -rw------- 1 mgousr01 mongodb 1081876480 2017-05-11 11:11 j._24 -rw------- 1 mgousr01 mongodb 1093656576 2017-05-11 11:12 j._25 -rw------- 1 mgousr01 mongodb 3735552 2017-05-11 15:38 j._26 -rw------- 1 mgousr01 mongodb 88 2017-05-11 15:38 lsn [email protected]:/mvnobss/users/mgousr01/mongodb/dbdata/rs0/journal>rm -rf j._26
啟動成功:
[email protected]:/mvnobss/users/mgousr01/mongodb/bin>ll total 4 -rwxrwxr-x 1 mgousr01 mongodb 60 2015-08-11 17:44 start_mongodb.sh [email protected]:/mvnobss/users/mgousr01/mongodb/bin>./start_mongodb.sh about to fork child process, waiting until server is ready for connections. forked process: 30927 child process started successfully, parent exiting
由於不是很懂這個文件目錄的作用,網上查閱各種資料才知道,這個journal就是oracle 的redo log,MySQL的binlog,用於故障恢復的。
具體是怎樣的運行機制還需要進一步研究。
我上面的環境是測試環境,不怕丟數據所以刪除無所謂,生產環境應該要慎重。
【個人覺得,應該是可以在配置文件中指定disable journaling,然後去啟動mongodb,啟動成功後再enable重啟數據庫,還未嘗試~】
mongodb非正常關閉無法啟動