Creating mongodb service in RHEL

Hi,

How to create a mongo DB service is RHEL?

is it possible to create a mongo DB srevice in RHEL using mongod command?

Your help is really appreciated.

Br
Raghavender

Hi @Kodumuri_Raghavender, if you installed MongoDB via the yum package manager then this is already set up as a service. If you manually installed MongoDB then you would have to build your own service file to run it as a service.

1 Like

Hi @Doug_Duncan, Thank you very much for your help. yum package manager install the mongodb software in root FS. we dont want to use root FS. we are installing in custome FS using TGZ file. i want to configure the service manually using mongod. can you please share the command if you have any?

Thank you so much for your help.

Hi @Kodumuri_Raghavender the below is the default mongod.service file installed on RHEL 7 using yum:

[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

[Install]
WantedBy=multi-user.target 

You would need to tweak the above for your system.

You can learn more about systemd files in the systemd man pages.

With this file in place you can start the service just as you would any other RHEL service with sudo systemctl start mongod.

1 Like

Hi @Kodumuri_Raghavender,

If you want to manually create a service definition and config file, you can find the versions used in the RPM packages in the MongoDB source on GitHub: mongo/rpm at master · mongodb/mongo · GitHub.

If you just need a different install path, the MongoDB RPMs should also be relocatable using the --prefix option for yum/dnf.

For example: sudo rpm --prefix /home/mongodb/ mongodb.rpm.

Regards,
Stennie

3 Likes

Hi everyone, if I did my installation manually from the tarball for centos8 and deployed some sharded clusters, should I have a service per each mongod and mongos that I used?

Hello Stennie,

Myself had used a tar ball extract for mongo software (version : 4.2.9) on a RHEL machine and configured 3 config files for 3 mongod services for a replica set. Now that, i need to make those services to be “auto start” in terms of server reboot situation. Can you advise or provide some needful pointers…?

Thanks in advance…Kesav

Hi @ramgkliye,

If you are installing without using an RPM package, you can create your own service definition as I suggested earlier in this discussion:

Assuming you are using a recent version of RHEL (7+), you’ll want to look into the documentation for Managing services with systemd.

I generally recommend using packages to simplify the effort of installing and upgrading your deployment. With a tarball install you also have to manually install dependencies.

Regards,
Stennie

2 Likes