1. 程式人生 > >儲存引擎配置引發的MongoDB啟動失敗

儲存引擎配置引發的MongoDB啟動失敗

前言

系統環境:

[[email protected] mongo]# uname -r
3.10.0-693.21.1.el7.x86_64
[[email protected] mongo]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 
[[email protected] mongo]# 

mongodb版本:MongoDB shell version v3.4.10.1

問題描述

同事的測試伺服器的MongoDB在被他kill -9殺掉程序之後,發現服務無法啟動了,報下面的錯誤:

[
[email protected]
lib64]# /usr/local/mongo/bin/mongod -f /usr/local/mongo/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 2691 ERROR: child process failed, exited with error number 100 [[email protected] lib64]#

檢視mongodb.log,發現有下面的報錯:

2018-11-02T16:08:17.698+0800 I CONTROL  [initandlisten] MongoDB starting : pid=2691 port=27017 dbpath=/usr/local/mongo/data 64-bit host=rabbitmq3
2018-11-02T16:08:17.698+0800 I CONTROL  [initandlisten] db version v3.4.10.1
2018-11-02T16:08:17.698+0800 I CONTROL  [initandlisten] git version: c8bea1aa423c3b6b8d2cea9206b48a915411236a
2018-11-02T16:08:17.698+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2k-fips  26 Jan 2017
2018-11-02T16:08:17.698+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2018-11-02T16:08:17.698+0800 I CONTROL  [initandlisten] modules: none
2018-11-02T16:08:17.698+0800 I CONTROL  [initandlisten] build environment:
2018-11-02T16:08:17.698+0800 I CONTROL  [initandlisten]     distmod: rhel70
2018-11-02T16:08:17.698+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-11-02T16:08:17.698+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-11-02T16:08:17.698+0800 I CONTROL  [initandlisten] options: { config: "/usr/local/mongo/mongodb.conf", net: { port: 27017 }, processManagement: { fork: true }, storage: { dbPath: "/usr/local/mongo/data", e
ngine: "mmapv", journal: { enabled: false } }, systemLog: { destination: "file", path: "/usr/local/mongo/logs/mongodb.log" } }
2018-11-02T16:08:17.732+0800 I STORAGE  [initandlisten] exception in initAndListen: 18656 Cannot start server with an unknown storage engine: mmapv, terminating
2018-11-02T16:08:17.732+0800 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2018-11-02T16:08:17.732+0800 I NETWORK  [initandlisten] removing socket file: /tmp/mongodb-27017.sock
2018-11-02T16:08:17.732+0800 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2018-11-02T16:08:17.732+0800 I CONTROL  [initandlisten] now exiting
2018-11-02T16:08:17.732+0800 I CONTROL  [initandlisten] shutting down with code:100

根據報錯資訊的“exception in initAndListen: 18656 Cannot start server with an unknown storage engine: mmapv, terminating”,檢視mongodb的配置檔案:

[[email protected] mongo]# cat /usr/local/mongo/mongodb.conf
dbpath=/usr/local/mongo/data
logpath=/usr/local/mongo/logs/mongodb.log
port=27017
fork=true
journal=false
storageEngine=mmapv        #看到了這一行資訊
[
[email protected]
mongo]#

將配置檔案mongodb.conf裡面的“storageEngine=mmapv”這一行註釋掉,再啟動服務,發現可以啟動了:

[[email protected] mongo]# /usr/local/mongo/bin/mongod -f /usr/local/mongo/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3146
child process started successfully, parent exiting
[[email protected] mongo]# 
[[email protected] mongo]# ps -ef|grep mongo
root      3146     1  1 16:05 ?        00:00:00 /usr/local/mongo/bin/mongod -f /usr/local/mongo/mongodb.conf
root      3163  2823  0 16:05 pts/2    00:00:00 grep --color=auto mongo
[[email protected] mongo]# 

然後將配置檔案裡面的“storageEngine=mmapv”這一行放開註釋,發現啟動還是報原來的錯

[[email protected] lib64]# /usr/local/mongo/bin/mongod -f /usr/local/mongo/mongodb.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 2352
ERROR: child process failed, exited with error number 100
[[email protected] lib64]# 

問題原因

據同事說,上面的配置檔案是他直接從網上覆制貼上的,這裡面有兩個問題:
1、同事可能是想將儲存引擎改成mmapv1,但是可能是貼上錯誤,也可能是他自己定位過程中改成了mmapv;

2、這是3.4版本的mongdb,從MongoDB3.2 版本開始,MongoDB預設的儲存引擎就已經是WiredTiger,配置檔案裡面配置的data目錄/usr/local/mongo/data是WiredTiger引擎的資料。當引擎改成mmapv1,data目錄與WiredTiger引擎還指向同一個目錄,就會報“ERROR: child process failed, exited with error number 100”的錯。
因為WiredTiger引擎建立的資料目錄無法用mmapv1引擎去開啟。
所以將配置檔案的“storageEngine=mmapv”註釋掉,服務就正常啟動了。

補充知識

1、關閉MongoDB的正確方法

方法一:kill -2 PID
方法二:use admin;
db.shutdownServer()

2、檢視當前mongodb使用的儲存引擎的方法

登入到mongodb,執行db.serverStatus();語句,在返回的結果中,就能找到像下面的引擎資訊:

>db.serverStatus();
......                               #省略若干行 
    "storageEngine" : {
        "name" : "wiredTiger",
        "supportsCommittedReads" : true,
        "readOnly" : false,
        "persistent" : true
    },

3、切換儲存引擎的正確姿勢

3.1 搭建好MongoDB的時候就切換:
因為MongoDB不同的版本有自己預設的儲存引擎,如果在MongoDB搭建好的時候,就要修改儲存引擎,那就直接該配置檔案,啟動就好了,例如下面的檔案:

[[email protected] mongodb]# cat /etc/mongodb/mongo_mmapv1.conf 
dbpath=/usr/local/mongo/data
logpath=/usr/local/mongo/logs/mongodb.log
port=27017
fork=true
journal=false
storageEngine=mmapv1      #指定引擎型別
[[email protected] mongodb]# 

3.2 MongoDB已經使用了一段時間之後切換:
例如,將使用了一段時間的MongoDB3.4版本預設的WiredTiger引擎切換成mmapv1引擎:
1)建立一個新的mmapv1引擎的資料目錄:

[[email protected] mongo]# mkdir /opt/mongo/data -p

2)修改配置檔案(以上面的配置檔案為例,這裡重新建立了一個新的配置檔案):

[[email protected] mongo]# cat /etc/mongodb/mongodb_mmapv1.conf
dbpath=/opt/mongo/data          #修改資料目錄,/opt/mongo 目錄是重新建立的全新的目錄
logpath=/opt/mongo/mongodb.log
port=27017
fork=true
journal=false
storageEngine=mmapv1         #增加這一行      
[[email protected] mongo]# 

3)重新啟動MongoDB(將配置檔案指向新的mmapv1引擎的配置檔案):

[[email protected] bin]# ./mongod -f /etc/mongodb/mongo_mmapv1.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 93783
child process started successfully, parent exiting
[[email protected] bin]# 

4)登入mongodb,執行db.serverStatus(); 檢查當前的儲存引擎,發現已經改成了mmapv1:

>db.serverStatus();
......                               #省略若干行 
    "storageEngine" : {
        "name" : "mmapv1",         #引擎已經修改成了mmapv1
        "supportsCommittedReads" : false,
        "readOnly" : false,
        "persistent" : true

4、關於“ERROR: child process failed, exited with error number 100”的報錯

拿到這個報錯,有找過百度、谷歌,常見的說法有下面兩種,雖然沒有解決我的問題,記下作為借鑑:
1)是因為在資料目錄下生成了mongod.lock 檔案,需要將這個檔案刪除,然後再重新啟動(這種說法最常見)
2)可能是dbpath檔案的許可權問題

看了一下,正常啟動的mongodb,也有mongod.lock檔案,mongod.lock裡面就是當前mongodb的程序號:

[[email protected] mongodb]# ps -ef|grep mongo
root      94611      1  0 14:42 ?        00:00:01 ./mongod -f /etc/mongodb/mongo_mmapv1.conf
root      94627   4575  0 14:42 pts/0    00:00:00 ./mongo
root      94834   4719  0 14:46 pts/1    00:00:00 grep --color=auto mongo
[[email protected] mongodb]# 
[[email protected] mongodb]# 
[[email protected] mongodb]# 
[[email protected] mongodb]# 
[[email protected] mongodb]# 
[[email protected] mongodb]# cat /usr/local/mongo/data/mongod.lock 
94611
[[email protected] mongodb]#