RHEL7 systemd 自动启动 mongo 服务?

RHEL7 systemd start mongo services automatically?

我有一个 RHEL7 服务器,它是 Mongo 集群的一部分。我希望在系统启动时自动启动三个 mongo 进程。一个 mongod,一个仲裁器和一个 mongos:

我一直在尝试为这些命令创建系统服务,即

[Unit]
Description=mongo configuration server
After=network.target

[Service]
User=mongod
Group=mongod
ExecStart=/usr/bin/mongod -f /etc/mongoconf.conf

[Install]
WantedBy=multi-user.target

当我尝试执行 sudo systemctl daemon-reload && sudo systemctl start mongoconf 时,出现此错误

● mongoconf.service - mongo configuration server
   Loaded: loaded (/etc/systemd/system/mongoconf.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2017-02-02 14:38:34 AWST; 20s ago
  Process: 5114 ExecStart=/usr/bin/mongod -f /etc/mongoconf.conf (code=exited, status=1/FAILURE)
 Main PID: 5114 (code=exited, status=1/FAILURE)

Feb 02 14:38:34 mdb1 systemd[1]: Started mongo configuration server.
Feb 02 14:38:34 mdb1 systemd[1]: Starting mongo configuration server...
Feb 02 14:38:34 mdb1 systemd[1]: mongoconf.service: main process exited, code=exited, status=1/FAILURE
Feb 02 14:38:34 mdb1 systemd[1]: Unit mongoconf.service entered failed state.
Feb 02 14:38:34 mdb1 systemd[1]: mongoconf.service failed.

我也尝试过使用带有 pid 文件的分叉类型:

[Unit]
Description=mongo configuration server
After=network.target

[Service]
User=mongod
Group=mongod
ExecStart=/usr/bin/mongod -f /etc/mongoconf.conf --pidfilepath /var/lib/mongoconf/pid --fork
Type=forking
PIDFile=/var/run/mongodb/mongoconf/pid

[Install]
WantedBy=multi-user.target

但是报错

● mongoconf.service - mongo configuration server
   Loaded: loaded (/etc/systemd/system/mongoconf.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2017-02-02 14:45:36 AWST; 4s ago
  Process: 5256 ExecStart=/usr/bin/mongod -f /etc/mongoconf.conf --pidfilepath /var/lib/mongoconf/pid --fork (code=exited, status=1/FAILURE)
 Main PID: 5114 (code=exited, status=1/FAILURE)

Feb 02 14:45:36 mdb1 systemd[1]: Starting mongo configuration server...
Feb 02 14:45:36 mdb1 mongod[5256]: about to fork child process, waiting until server is ready for connections.
Feb 02 14:45:36 mdb1 mongod[5256]: forked process: 5258
Feb 02 14:45:36 mdb1 systemd[1]: mongoconf.service: control process exited, code=exited status=1
Feb 02 14:45:36 mdb1 systemd[1]: Failed to start mongo configuration server.
Feb 02 14:45:36 mdb1 systemd[1]: Unit mongoconf.service entered failed state.
Feb 02 14:45:36 mdb1 systemd[1]: mongoconf.service failed.

手动启动 mongo 配置工作正常并创建 pid 文件

/usr/bin/mongod -f /etc/mongoconf.conf --pidfilepath /var/lib/mongoconf/pid --fork

我使用的 mongod 版本来自 mongodb.com,我按照他们的安装指南安装了它。

db version v3.4.1
git version: 5e103c4f5583e2566a45d740225dc250baacfbd7
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64

来自这个 repo

[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

我想知道我是否以错误的方式进行此操作,是否有更好的方法来执行此操作?

这是我的解决方案

用这些行制作一个 bash 脚本

/usr/bin/mongod -f /etc/mongo_shard001.conf
/usr/bin/mongod -f /etc/mongoarb.conf
/usr/bin/mongos -f /etc/mongos.conf

然后将此行添加到您的 crontab

@reboot root cd /foldername && ./scriptname.sh

systemd 将是一个更好的解决方案,如果有人知道如何设置它的话。

mongo 文档没有帮助

我知道您说的是 rhel7,但由于它是 duckduckgo 上针对此问题的唯一答案,因此它很有用。 Ubuntu 15 岁及以上:

sudo systemctl enable mongod.service