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

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

uname openssl 谷歌 語句 list 服務 根據 一段時間 lib64

前言

系統環境:

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

mongodb版本:

MongoDB shell version v3.4.10.1

問題描述

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

[root@rabbitmq3 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
[root@rabbitmq3 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的配置文件:

[root@rabbitmq3 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        #看到了這一行信息
[root@rabbitmq3 mongo]# 

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

[root@rabbitmq3 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
[root@rabbitmq3 mongo]# 
[root@rabbitmq3 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
[root@rabbitmq3 mongo]# 

然後將配置文件裏面的“storageEngine=mmapv”這一行放開註釋(改成了storageEngine=mmapv1),發現啟動還是報原來的錯

[root@rabbitmq3 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
[root@rabbitmq3 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剛剛搭建好的時候,就要修改存儲引擎,那就直接在配置文件指定引擎類型,然後啟動服務就可以了,例如下面的文件:

[root@rabbitmq3 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      #指定引擎類型
[root@test104 mongodb]# 

3.2 MongoDB已經使用了一段時間之後切換:
例如,將使用了一段時間的MongoDB3.4版本默認的WiredTiger引擎切換成mmapv1引擎:
1)創建一個新的mmapv1引擎的數據目錄:

[root@rabbitmq3 mongo]# mkdir /opt/mongo/data -p

2)修改配置文件(以上面的配置文件為例,這裏重新創建了一個新的配置文件):

[root@rabbitmq3 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         #增加這一行      
[root@rabbitmq3 mongo]# 

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

[root@rabbitmq3 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
[root@rabbitmq3 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的進程號:

[root@rabbitmq3 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
[root@rabbitmq3 mongodb]# 
[root@rabbitmq3 mongodb]# 
[root@rabbitmq3 mongodb]# 
[root@rabbitmq3 mongodb]# 
[root@rabbitmq3 mongodb]# 
[root@rabbitmq3 mongodb]# cat /usr/local/mongo/data/mongod.lock 
94611
[root@rabbitmq3 mongodb]# 

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