在 ubuntu 16.04 LTS 上运行 mongodb
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37014186/
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
Running mongodb on ubuntu 16.04 LTS
提问by Sagi Forbes
How can I run Mongodb, as a service, on Ubuntu 16.04 LTS? A few days ago I had upgrade my server to Ubuntu 16.04. I have noticed the my MongoDB service does not start when I reboot. Trying to use
如何在 Ubuntu 16.04 LTS 上将 Mongodb 作为服务运行?几天前,我将服务器升级到 Ubuntu 16.04。我注意到我的 MongoDB 服务在我重新启动时没有启动。尝试使用
sudo initctl start mongod
Did not do the trick. Anyone has an idea how to solve this?
没有做的伎俩。任何人都知道如何解决这个问题?
回答by Sagi Forbes
Anyone who upgrade or installed Ubuntu 16.04 ( also known as Ubuntu Xenial xerus ) noticed that some of the old services stopped running. This issue is known from version 15.04 but I will focus on the above version.
升级或安装 Ubuntu 16.04(也称为 Ubuntu Xenial xerus)的任何人都注意到一些旧服务停止运行。这个问题从 15.04 版本就知道了,但我会关注上面的版本。
Such was my case with MongoDB. To make a long story, short, Ubuntu shifted from upstart to systemd. One common solution, to these problems, is to switch back to upstart. I do not consider that option as a real solution, certainly not for the long run.
这就是我使用 MongoDB 的情况。长话短说,Ubuntu 从新贵转向 systemd。解决这些问题的一种常见解决方案是切换回新贵。我不认为该选项是真正的解决方案,从长远来看肯定不是。
A real solution ( IMHO ) to the problem is to write systemd script that will start MongodDB. Unfortunately MongoDB guys had yet to supply one.
该问题的真正解决方案(恕我直言)是编写将启动 MongodDB 的 systemd 脚本。不幸的是,MongoDB 人员还没有提供一个。
So I had to write one from scratch. To create one of your own follow these steps:
所以我不得不从头开始写一个。要创建自己的一个,请按照下列步骤操作:
- switch to root using
sudo su
- 切换到 root 使用
sudo su
or use sudo for all the following steps.
或使用 sudo 执行以下所有步骤。
create a service script (in this example the name of the service is Mongodb)
nano /lib/systemd/system/mongodb.service
File content should be
[Unit] Description=MongoDB Database Service Wants=network.target After=network.target [Service] ExecStart=/usr/bin/mongod --config /etc/mongod.conf ExecReload=/bin/kill -HUP $MAINPID Restart=always User=mongodb Group=mongodb StandardOutput=syslog StandardError=syslog [Install] WantedBy=multi-user.target
创建一个服务脚本(在这个例子中服务的名称是 Mongodb)
nano /lib/systemd/system/mongodb.service
文件内容应该是
[Unit] Description=MongoDB Database Service Wants=network.target After=network.target [Service] ExecStart=/usr/bin/mongod --config /etc/mongod.conf ExecReload=/bin/kill -HUP $MAINPID Restart=always User=mongodb Group=mongodb StandardOutput=syslog StandardError=syslog [Install] WantedBy=multi-user.target
You can also download the file from here:
mongodb.service
Here is a quick description of the important fields:
ExecStart- Is the command to run. Mongo installs itself under /usr/bin and the configuration file is written at /etc
User- The uid of the mongod process.
Group- The gid of the mongod process. Note that the user and group are created by the installation.
您也可以从这里下载文件:
mongodb.service
这里是重要字段的简要说明:
ExecStart- 是要运行的命令。Mongo 自己安装在 /usr/bin 下,配置文件写在 /etc
用户- mongod 进程的 uid。
Group- mongod 进程的 gid。请注意,用户和组是由安装创建的。
Now to start mongodb:
现在启动mongodb:
sudo systemctl start mongodb
To stop mongodb service use:
要停止 mongodb 服务使用:
sudo systemctl stop mongodb
To enable mongodb on startup
在启动时启用 mongodb
sudo systemctl enable mongodb.service
If you need to refresh the services use:
如果您需要刷新服务,请使用:
sudo systemctl daemon-reload
回答by efkan
The latest version of MongoDB does most of things except one thing for now.
After installing the MongoDB
on Ubuntu 16.04.x
, run the commands as follows:
最新版本的 MongoDB 做了大部分事情,除了现在做的一件事。安装MongoDB
on 后Ubuntu 16.04.x
,运行如下命令:
$ sudo systemctl enable mongod.service
$ sudo systemctl daemon-reload
Now most likely mongod
starts on every boot automatically.
现在很可能mongod
在每次启动时自动启动。
This solution also works in case of getting this error:
此解决方案也适用于出现此错误的情况:
Failed to start mongod.service: Unit mongod.service not found.
启动 mongod.service 失败:未找到单元 mongod.service。
After run the commands above, the commands below start to work:
运行上面的命令后,下面的命令开始工作:
$ sudo service mongod start
$ mongo
Similarly, the all services installed must be enabled to run. For instance, after installation of Ops Manager (a.k.a MMS) the documentation says to run the commend below.
同样,安装的所有服务都必须启用才能运行。例如,安装 Ops Manager(又名 MMS)后,文档说要运行下面的命令。
$ sudo systemctl start mongodb-mms.service
$ sudo systemctl start mongodb-mms.service
Most likely Ubuntu does not start the service. Because it is not enabled yet. To enable it just run the command below:
Ubuntu 很可能没有启动该服务。因为它还没有启用。要启用它,只需运行以下命令:
$ sudo systemctl enable mongodb-mms.service
$ sudo systemctl daemon-reload
Then try to start the service:
然后尝试启动服务:
$ sudo systemctl enable mongodb-mms.service
That's all...
就这样...
回答by Nicola Pedretti
For People That Want A Fresh Install
对于想要全新安装的人
I have been struggling with this for 1 hour. Then I found this pageInstalling is as easy as doing:
我已经为此苦苦挣扎了 1 个小时。然后我发现这个页面安装就像做一样简单:
sudo apt-get update
sudo apt-get install mongodb
Then to check if everything works:
然后检查是否一切正常:
sudo service mongodb status
sudo service mongodb status
Let me know if this works for you!
让我知道这是否适合您!
回答by Hut8
The packages for 3.2+ contain upstart scripts.
3.2+ 的软件包包含新贵脚本。
First, follow the official instructions to install:
首先,按照官方说明进行安装:
# If you installed the ubuntu package, remove it, if not skip to key import
sudo apt-get remove mongodb
sudo apt-get autoremove
# import key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
# add trusty repos
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
# update apt
sudo apt-get update
# install the package
sudo apt-get install -y mongodb-org
Then unmask the mongodb.service
job (and enable and start it). I believe it was masked because the package includes an upstart jobs as well, so you wouldn't want both of them to start. In our case, however, this is clearly what we want.
然后取消mongodb.service
屏蔽作业(并启用并启动它)。我相信它被掩盖了,因为该软件包还包括一个新贵的工作,所以你不会希望他们两个都开始。然而,在我们的例子中,这显然是我们想要的。
sudo systemctl unmask mongodb
sudo service mongod start
References:
参考:
回答by Saunved Mutalik
sudo systemctl start mongod
is what worked for me on Ubuntu Linux 16.04
在 Ubuntu Linux 16.04 上对我有用