存储引擎配置引发的MongoDB启动失败

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了存储引擎配置引发的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”这一行放开注释(改成了storageEngine=mmapv1),发现启动还是报原来的错

[[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]# 

以上是关于存储引擎配置引发的MongoDB启动失败的主要内容,如果未能解决你的问题,请参考以下文章

Data Base mongodb高版本与低版本的区别

如何将代码片段存储在 mongodb 中?

Mongodb分片配置服务器不同步导致mongos进程启动失败

一次事务配置失败引发的总结

mongodb--选举失败

替换 MongoDB 存储引擎的那几个不眠夜