mongodb 在意外关闭后崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12838459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:57:08  来源:igfitidea点击:

mongodb crashes after unexpected shutdown

mongodb

提问by user1737875

after our vps got down and we boot it, mongod service didnt start automatically and we cannot start it by service mongod start, file /var/log/mongo/mongod.logcontains following log:

在我们的 vps 停机并启动它后,mongod 服务没有自动启动,我们无法启动它service mongod start,文件/var/log/mongo/mongod.log包含以下日志:

  ***** SERVER RESTARTED *****


Thu Oct 11 13:51:18 
Thu Oct 11 13:51:18 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Oct 11 13:51:18 
Thu Oct 11 13:51:18 [initandlisten] MongoDB starting : pid=3821 port=27017 dbpath=/var/lib/mongo 32-bit host=server2.paransa.org
Thu Oct 11 13:51:18 [initandlisten] 
Thu Oct 11 13:51:18 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Oct 11 13:51:18 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Oct 11 13:51:18 [initandlisten] **       with --journal, the limit is lower
Thu Oct 11 13:51:18 [initandlisten] 
Thu Oct 11 13:51:18 [initandlisten] ** WARNING: You are running in OpenVZ. This is known to be broken!!!
Thu Oct 11 13:51:18 [initandlisten] 
Thu Oct 11 13:51:18 [initandlisten] db version v2.2.0, pdfile version 4.5
Thu Oct 11 13:51:18 [initandlisten] git version: f5e83eae9cfbec7fb7a071321928f00d1b0c5207
Thu Oct 11 13:51:18 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Thu Oct 11 13:51:18 [initandlisten] options: { config: "/etc/mongod.conf", dbpath: "/var/lib/mongo", fork: "true", logappend: "true", logpath: "/var/log/mongo/mongod.log", pidfilepath: "/var/run/mongodb/mongod.pid" }
Thu Oct 11 13:51:18 [initandlisten] couldn't open /var/lib/mongo/mydb_main.ns errno:13 Permission denied
Thu Oct 11 13:51:18 [initandlisten] error couldn't open file /var/lib/mongo/mydb_main.ns terminating
Thu Oct 11 13:51:18 dbexit: 
Thu Oct 11 13:51:18 [initandlisten] shutdown: going to close listening sockets...
Thu Oct 11 13:51:18 [initandlisten] shutdown: going to flush diaglog...
Thu Oct 11 13:51:18 [initandlisten] shutdown: going to close sockets...
Thu Oct 11 13:51:18 [initandlisten] shutdown: waiting for fs preallocator...
Thu Oct 11 13:51:18 [initandlisten] shutdown: closing all files...
Thu Oct 11 13:51:18 [initandlisten] closeAllFiles() finished
Thu Oct 11 13:51:18 [initandlisten] shutdown: removing fs lock...
Thu Oct 11 13:51:18 dbexit: really exiting now

as you may see, log says mongo server cant open *.ns file cause lake of permission

如您所见,日志显示 mongo 服务器无法打开 *.ns 文件导致权限湖

but if i run the server manually by following command:

但是如果我通过以下命令手动运行服务器:

mongod --dbpath=/var/lib/mongo -f /etc/mongod.conf

it seems everything is fine and about the log file:

似乎一切都很好,关于日志文件:

***** SERVER RESTARTED *****


Thu Oct 11 14:02:06 
Thu Oct 11 14:02:06 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Oct 11 14:02:06 
Thu Oct 11 14:02:06 [initandlisten] MongoDB starting : pid=4090 port=27017 dbpath=/var/lib/mongo 32-bit host=server2.paransa.org
Thu Oct 11 14:02:06 [initandlisten] 
Thu Oct 11 14:02:06 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Oct 11 14:02:06 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Oct 11 14:02:06 [initandlisten] **       with --journal, the limit is lower
Thu Oct 11 14:02:06 [initandlisten] 
Thu Oct 11 14:02:06 [initandlisten] ** WARNING: You are running in OpenVZ. This is known to be broken!!!
Thu Oct 11 14:02:06 [initandlisten] 
Thu Oct 11 14:02:06 [initandlisten] db version v2.2.0, pdfile version 4.5
Thu Oct 11 14:02:06 [initandlisten] git version: f5e83eae9cfbec7fb7a071321928f00d1b0c5207
Thu Oct 11 14:02:06 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Thu Oct 11 14:02:06 [initandlisten] options: { config: "/etc/mongod.conf", dbpath: "/var/lib/mongo", fork: "true", logappend: "true", logpath: "/var/log/mongo/mongod.log", pidfilepath: "/var/run/mongodb/mongod.pid" }
Thu Oct 11 14:02:07 [websvr] admin web console waiting for connections on port 28017
Thu Oct 11 14:02:07 [initandlisten] waiting for connections on port 27017

and using --repair arg didnt help (maybe because it could start manually with mongod --dbpath=/var/lib/mongo -f /etc/mongod.confand adding --repair arg to the command, logs everything is fine!...)

并使用 --repair arg 没有帮助(也许是因为它可以手动启动mongod --dbpath=/var/lib/mongo -f /etc/mongod.conf并将 --repair arg 添加到命令中,记录一切都很好!...)

any suggestion?

有什么建议吗?

回答by Adam Comerford

This is a permissions issue, you are running as mongod (or mongodb etc.) user when you start via service, and you are likely running as root (you confirm this in the comments above) when you are using sudo or invoking as root. You need to fix the permissions for all the files in that folder and not start the service as root, ever.

这是一个权限问题,当您通过服务启动时,您以 mongod(或 mongodb 等)用户身份运行,并且当您使用 sudo 或以 root 身份调用时,您可能以 root 身份运行(您在上面的评论中确认了这一点)。您需要修复该文件夹中所有文件的权限,并且永远不要以 root 身份启动服务。

To fix the files based on what you have posted, assuming your relevant user is mongodb and the group is mongodb, you would do the following with sudo (or as root) - substitute for the appropriate user for your system:

要根据您发布的内容修复文件,假设您的相关用户是 mongodb 并且组是 mongodb,您将使用 sudo(或以 root 用户身份)执行以下操作 - 替换系统的相应用户:

chown -R mongodb:mongodb /var/lib/mongo
chown -R mongodb:mongodb /var/run/mongodb
chown mongodb:mongodb /var/log/mongo/mongod.log

Once you fix the permissions on these folders you should be able to start via service again. Note: if you post the output of ls -al /var/lib/mongothen I can give you the exact commands you need to run

修复这些文件夹的权限后,您应该能够再次通过服务启动。注意:如果您发布ls -al /var/lib/mongothen的输出,我可以为您提供您需要运行的确切命令

回答by Moin Haidar

sudo rm /data/db/mongod.lock
sudo mongod --dbpath /data/db --repair
sudo mongod --dbpath /data/db

回答by Mani K

Stop the MongoDB service

sudo service mongodb stop Remove the MongoDB lock file

sudo service mongodb stop 删除MongoDB锁文件

sudo rm /var/lib/mongodb/mongod.lock

Change ownership from root to the MongoDB path

sudo chown -R mongodb:mongodb /var/lib/mongodb/

Access the mongodb Shell   

 sudo mongod --dbpath=/var/lib/mongodb

Start the MongoDB service

sudo service mongodb start
Test the mongo application

mongo

回答by user1518659

Try deleting the *.lockfile in "\data\db"folder before starting mongod.exe in CMD.

在 CMD 中启动 mongod.exe 之前尝试删除*.lock文件"\data\db"夹中的文件。

回答by Hedi Lahouar

You can run automatically your mongodb forever from the rc.local, using this command:

您可以rc.local使用以下命令从 永远自动运行您的 mongodb :

su pi -c 'sudo mongod --repairpath your_path'

and don't worry about the unexpected shutdown.

并且不用担心意外关机。