如何在 Ubuntu 16.04 LTS 上将 Mongodb 作为服务运行? 几天前,我将服务器升级到了 Ubuntu 16.04。我注意到我的 MongoDB 服务在我重新启动时没有启动。 尝试使用
sudo initctl start mongod
没有成功。 有人知道如何解决这个问题吗?
最佳答案
任何升级或安装 Ubuntu 16.04(也称为 Ubuntu Xenial xerus)的人都注意到一些旧服务停止运行。 这个问题从 15.04 版本就知道了,但我会重点关注上面的版本。
这就是我对 MongoDB 的情况。长话短说,Ubuntu 从 Upstart 转向了 systemd。对于这些问题,一种常见的解决方案是切换回 Upstart 。我不认为该选项是真正的解决方案,从长远来看肯定不是。
该问题的真正解决方案(恕我直言)是编写将启动 MongodDB 的 systemd 脚本。不幸的是,MongoDB 的人还没有提供一个。
所以我不得不从头开始写一个。要创建您自己的一个,请按照以下步骤操作:
- 切换到root使用
sudo su
or use sudo for all the following steps.
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
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.
Now to start mongodb:
sudo systemctl start mongodb
停止使用 mongodb 服务:
sudo systemctl stop mongodb
在启动时启用 mongodb
sudo systemctl enable mongodb.service
如果您需要刷新服务,请使用:
sudo systemctl daemon-reload
关于mongodb - 在 ubuntu 16.04 LTS 上运行 mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37014186/