Ubuntu 上的 MongoDB 不会作为服务启动,日志中没有任何内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12232166/
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-09 12:48:45  来源:igfitidea点击:

MongoDB on Ubuntu won't start as a service, nothing in the log

mongodbubuntuservice

提问by BishopZ

Am running MongoDB 2.2 on Ubuntu and if I run:

我在 Ubuntu 上运行 MongoDB 2.2,如果我运行:

sudo mongod

I get an error that it can't find /data/db, which is not where the database is. In mongod.conf the database path is specified as the Ubuntu 10gen default /var/lib/mongodbwhich is where the db is located. Seems like mongodis not finding the conf file. So when I run:

我收到一个错误,它找不到/data/db,这不是数据库所在的位置。在 mongod.conf 中,数据库路径被指定为 Ubuntu 10gen 默认值/var/lib/mongodb,这是 db 所在的位置。好像mongod没有找到conf文件。所以当我运行时:

sudo mongod -f /etc/mongodb.conf

The server starts up fine and output is logged to the log file: /var/log/mongodb/mongodb.log. All is happy. I can switch to another shell, log into mongo shell, see the databases and run queries.

服务器启动正常,输出记录到日志文件:/var/log/mongodb/mongodb.log. 一切都很开心。我可以切换到另一个 shell,登录到 mongo shell,查看数据库并运行查询。

So, I cancel out of that and try to run as a service:

所以,我取消了它并尝试作为服务运行:

> sudo status mongodb
mongodb stop/waiting
> sudo start mongodb
mongodb start/running, process 10468

Looks good so far, but the mongo server did not start. Running another:

到目前为止看起来不错,但是 mongo 服务器没有启动。运行另一个:

> sudo status mongodb
mongodb stop/waiting
> mongo
MongoDB shell version: 2.2.0
connecting to: test
Sat Sep  1 19:07:43 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed

"test" is not the correct database, and nothing appears in the log file.

“test”不是正确的数据库,日志文件中没有任何内容。

I am at a loss as to what could be wrong. I checked the upstart scripts and they seem fine. /etc/init/mongodb.confruns:

我不知道什么可能是错的。我检查了新贵脚本,它们看起来不错。/etc/init/mongodb.conf运行:

mongodb --exec  /usr/bin/mongod -- --config /etc/mongodb.conf

回答by Adam Comerford

OK, this all comes down to permissions, but let's take it step by step. When you run sudo mongodit does not load a config file at all, it literally starts with the compiled in defaults - port 27017, database pathof /data/db etc. - that is why you got the error about not being able to find that folder. The "Ubuntu default" is only used when you point it at the config file (if you start using the service command, this is done for you behind the scenes).

好的,这一切都归结为权限,但让我们一步一步来。当您运行sudo mongod它时,它根本不加载配置文件,它从字面上编译的默认值开始 - 端口27017,/data/db 的数据库路径等 - 这就是为什么您会收到有关无法找到该文件夹​​的错误的原因. “Ubuntu 默认”仅在您将其指向配置文件时使用(如果您开始使用 service 命令,这是在幕后为您完成的)。

Next you ran it like this:

接下来你像这样运行它:

sudo mongod -f /etc/mongodb.conf

If there weren't problems before, then there will be now - you have run the process, with your normal config (pointing at your usual dbpath and log) as the root user. That means that there are going to now be a number of files in that normal MongoDB folder with the user:group of root:root.

如果以前没有问题,那么现在就会出现 - 您已经运行了该过程,并以 root 用户身份使用您的正常配置(指向您常用的 dbpath 和日志)。这意味着现在在普通 MongoDB 文件夹中将有许多文件,其 user:group of root:root.

This will cause errors when you try to start it as a normal service again, because the mongodb user (which the service will attempt to run as) will not have permission to access those root:rootfiles, and most notably, it will probably not be able to write to the log file to give you any information.

当您再次尝试将其作为普通服务启动时,这将导致错误,因为 mongodb 用户(该服务将尝试以该用户身份运行)将无权访问这些root:root文件,最值得注意的是,它可能无法访问写入日志文件以提供任何信息。

Therefore, to run it as a normal service, we need to fix those permissions. First, make sure MongoDB is not currently running as root, then:

因此,要将其作为普通服务运行,我们需要修复这些权限。首先,确保 MongoDB 当前没有以 root 身份运行,然后:

cd /var/log/mongodb
sudo chown -R mongodb:mongodb .
cd /var/lib/mongodb
sudo chown -R mongodb:mongodb .

That should fix it up (assuming the user:group is mongodb:mongodb), though it's probably best to verify with an ls -alor similar to be sure. Once this is done you should be able to get the service to start successfully again.

这应该修复它(假设 user:group 是mongodb:mongodb),尽管最好使用 anls -al或类似的来确定。完成此操作后,您应该能够再次成功启动服务。

回答by nelsonic

First confirm that the mongodbuser/group has permission to write to both the datadirectory and logfile:

首先确认mongodb用户/组有写入数据目录和日志文件的权限:

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

$ sudo chown -R mongodb:mongodb /var/log/mongodb.log

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

$ sudo chown -R mongodb:mongodb /var/log/mongodb.log

Start up MongoDB as a Daemon(background process) using the following command:

使用以下命令将 MongoDB作为守护程序(后台进程)启动:

$ mongod --fork --dbpath /var/lib/mongodb/ --smallfiles --logpath /var/log/mongodb.log --logappend

$ mongod --fork --dbpath /var/lib/mongodb/ --smallfiles --logpath /var/log/mongodb.log --logappend

To Shut Down MongoDBenter the Mongo CLI, access the admin and issue the shutdown command:

关闭 MongoDB,请进入 Mongo CLI,访问管理员并发出关闭命令:

$ ./mongo

> use admin

> db.shutdownServer()

$ ./mongo

> 使用管理员

> db.shutdownServer()

Ref: http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo

参考:http: //www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo

回答by Damodaran

I too had the same problem. So I went to cd /var/lib/mongodb/ and deleted the mongod.lock file Then it worked for me.

我也有同样的问题。所以我去了 cd /var/lib/mongodb/ 并删除了 mongod.lock 文件然后它对我有用。

回答by Pedro Israel

After checking all permission in the data, journal and log folders as suggested by @nelsonic, my problem was solved by giving permission to lock file in the /tmp folder

按照@nelsonic 的建议检查数据、日志和日志文件夹中的所有权限后,通过授予在 /tmp 文件夹中锁定文件的权限解决了我的问题

sudo chown mongod:mongod mongodb-27017.sock  

I was running it as a AWS Amazon Linux instance. I figured that out by executing as the mongod user as below, and then, researching the error code. It might be useful for other troubleshooting.

我将它作为 AWS Amazon Linux 实例运行。我通过以下面的 mongod 用户身份执行来解决这个问题,然后研究错误代码。它可能对其他故障排除有用。

sudo -S -u mongod mongod -f /etc/mongod.conf

回答by bekce

Nothing worked for me, then I've found that it was a permissions problem on /tmpdirectory:

没有什么对我有用,然后我发现这是/tmp目录的权限问题:

sudo chmod 1777 /tmp
sudo chown root:root /tmp

回答by Abram

None of the above answers worked for me. I finally figured it out by debugging the init script with:

以上答案都不适合我。我终于通过调试init脚本来解决这个问题:

sudo bash -x /etc/init.d/mongodb start

须藤 bash -x /etc/init.d/mongodb 启动

And seeing it was passing the wrong config path to mongod. I simply changed the line in /etc/init.d/mongodb from "CONF=/etc/mongodb.conf" to "CONF=/etc/mongod.conf". Version 2 uses the former, and installing version 3 added /etc/mongod.conf with the new format but apparently did not update the init script.

并且看到它向 mongod 传递了错误的配置路径。我只是将 /etc/init.d/mongodb 中的行从“CONF=/etc/mongodb.conf”更改为“CONF=/etc/mongod.conf”。版本 2 使用前者,安装版本 3 使用新格式添加了 /etc/mongod.conf 但显然没有更新 init 脚本。

UPDATE: I now have a much stranger problem where the init script works, but only if I run it with "sudo bash -x /etc/init.d/mongodb start" and not with "sudo service mongodb start". Same thing for stop.

更新:我现在有一个更奇怪的问题,init 脚本可以工作,但前提是我使用“sudo bash -x /etc/init.d/mongodb start”而不是“sudo service mongodb start”运行它。停止同样的事情。

回答by J.Jai

Just try this command:

试试这个命令:

sudo chown mongodb /tmp/mongodb-27017.sock

回答by Julien Laurenceau

My mongodb was starting when launched from the command line as the mongod user, but not as a service with User=mongod. After an hour checking permissions, definition of the service, sockets... it was SElinux !

我的 mongodb 在以 mongod 用户身份从命令行启动时启动,但不是作为 User=mongod 的服务启动的。一个小时后检查权限、服务定义、套接字......它是 SElinux !

In /etc/selinux/config I switched from enforcingto permissiveand reboot. It is now ok.

在 /etc/selinux/config 中,我从强制切换到许可并重新启动。现在好了。

回答by GPS

check your HDD space as well, because if /var/log is full, service cannot append the logs.

还要检查您的硬盘空间,因为如果 /var/log 已满,则服务无法附加日志。

回答by Mark

These days this error can occur if you've updated mongod and you are running and old database. Mongod will be using the wiredTiger engine by default and you'll have a mmapv1 database

现在,如果您更新了 mongod 并且正在运行旧数据库,则可能会发生此错误。Mongod 将默认使用wiredTiger 引擎,您将拥有一个 mmapv1 数据库

edit the engine setting in /etc/mongod.conf

编辑 /etc/mongod.conf 中的引擎设置

# engine: wiredTiger
engine: mmapv1

Careful - YAML is whitespace sensitive

小心 - YAML 对空格敏感

journalctl/systemd won't see this problem. Check the mongod log in /var/log/mongodb/mongod.log

journalctl/systemd 不会看到这个问题。查看 /var/log/mongodb/mongod.log 中的 mongod 日志

I presume you can convert the database with something like the steps outlined here

我想您可以使用此处概述的步骤来转换数据库

https://docs.mongodb.com/manual/tutorial/change-standalone-wiredtiger/

https://docs.mongodb.com/manual/tutorial/change-standalone-wiredtiger/