mongodb - 在 ubuntu 16.04 LTS 上运行 mongodb

标签 mongodb ubuntu systemctl

如何在 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 的人还没有提供一个。

所以我不得不从头开始写一个。要创建您自己的一个,请按照以下步骤操作:

  1. 切换到root使用
    sudo su
    

or use sudo for all the following steps.

  1. create a service script (in this example the name of the service is Mongodb)

    nano /lib/systemd/system/mongodb.service
    
  2. 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/

相关文章:

mysql - 如何将我的数据库架构 (.mwb) 放在 ubuntu 上的 mysql 服务器上?

shell - 如何删除打开 Linux shell 时出现的静态消息?

linux - Raspbian 在 qemu 中挂起

linux - 即使强制终止后,RHEL7 上的 httpd 服务也会自动重新启动

kubernetes - 是否可以在 pod 中使用 'systemctl' ?

mongodb - AWS 将数据从 MongoDB 迁移到 DynamoDB/S3/Redshift

带或不带空占位符的 MongoDB 模式设计

javascript - 如何删除 _id 属性?

javascript - 如何检索拆分的值并将它们放入单独的变量中

redis - 无法使用systemctl在Centos 7上启动redis服务器