RHEL7 systemd start mongo services automatically? - mongodb

I have a RHEL7 server that is part of a Mongo cluster. There are three mongo processes that I would like to be automatically started on system boot. One mongod, one arbiter and one mongos:
/usr/bin/mongod -f /etc/mongo_shard001.conf
/usr/bin/mongod -f /etc/mongoarb.conf
/usr/bin/mongos -f /etc/mongos.conf
I have been trying to create systemd services for these commands i.e
[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
When I try to do sudo systemctl daemon-reload && sudo systemctl start mongoconf, I get this error
● 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.
I have also tried using a forked type with pid file:
[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
But gives this error
● 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.
Starting the mongo config manually works fine and creates the pid file
/usr/bin/mongod -f /etc/mongoconf.conf --pidfilepath /var/lib/mongoconf/pid --fork
The version of mongod I am using is the one from mongodb.com, and I installed it following their install guide.
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
from this 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
I am wondering if I am going about this the wrong way, is there a better way to do this?

I know you said rhel7 but since it's the only answer coming up on duckduckgo for this question, this can be useful. Under Ubuntu 15 and up:
sudo systemctl enable mongod.service

Here is my solution
make a bash script with these lines
/usr/bin/mongod -f /etc/mongo_shard001.conf
/usr/bin/mongod -f /etc/mongoarb.conf
/usr/bin/mongos -f /etc/mongos.conf
and then add this line to your crontab
#reboot root cd /foldername && ./scriptname.sh
systemd would be a better solution, if anyone knows how to set it up.
the mongo documentation is no help

Related

Changing mongo dbpath to external drive causes server failure

I'm trying to use an external drive to store my mongo collections. As per this question, I changed dbPath in /etc/mongod.conf from /var/lib/mongodb to /media/user/drive/mongodb. I also chown'd the new path to the mongodb user.
However, when I sudo service start mongod, I cannot start the server:
mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Wed 2022-12-07 22:27:09 CET; 2s ago
Docs: https://docs.mongodb.org/manual
Process: 63819 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=100)
Main PID: 63819 (code=exited, status=100)
Dec 07 22:27:09 user-ThinkPad-T480s systemd[1]: Started MongoDB Database Server.
Dec 07 22:27:09 user-ThinkPad-T480s systemd[1]: mongod.service: Main process exited, code=exited, status=100/n/a
Dec 07 22:27:09 user-ThinkPad-T480s systemd[1]: mongod.service: Failed with result 'exit-code'.
When I change dbPath back to /var/lib/mongodb it starts again. How can I fix this such that I can use the path on my external drive?
Not sure which OS you run and which version of MongoDB.
On Amazon Linux 2 it's mongod user not mongodb for MongoDB-5.0
Make sure you ran chown with -R flag
-rw-r----- 1 mongod mongod 8.6M Dec 10 00:59 mongod.log
Also check logs for more hints, default location should be under
/var/log/mongodb/mongod.log

MongoDB does´t start, exit code 203

I installed MongoDB on the Ruspberry pi desktop on a VM, then I started it with the following command:
sudo service mongod start
The result of this command is the following:
systemctl list-unit-files --state enabled
mongodb.service enabled
Then when I check the status using
sudo service mongod status
The other issue is that there´s no .sock file in my /tmp folder.
PS: I removed MongoDB and reinstalled it trying to fix the issue but I always get the same problem.
Can anyone help me please?
Thank you in advance.
I get the following error:
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-06-08 12:33:17 CEST; 3s ago
Docs: https://docs.mongodb.org/manual
Process: 22168 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=203/EXEC)
Main PID: 22168 (code=exited, status=203/EXEC)
Jun 08 12:33:17 raspberry systemd[1]: Started MongoDB Database Server.
Jun 08 12:33:17 raspberry systemd[22168]: mongod.service: Failed to execute command: Exec format error
Jun 08 12:33:17 raspberry systemd[22168]: mongod.service: Failed at step EXEC spawning /usr/bin/mongod: Exec format error
Jun 08 12:33:17 raspberry systemd[1]: mongod.service: Main process exited, code=exited, status=203/EXEC
Jun 08 12:33:17 raspberry systemd[1]: mongod.service: Failed with result 'exit-code'.```

Cannot start Zookeeper service on CentOS7

When trying to start zookeeper service I get the following
● zookeeper.service
Loaded: loaded (/etc/systemd/system/zookeeper.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu 2020-04-02 16:19:24 EDT; 5min ago
Process: 5201 ExecStop=/usr/local/kafka/kafka_2.13-2.4.1/bin/zookeeper-server-stop.sh (code=exited, status=1/FAILURE)
Process: 4882 ExecStart=/usr/local/kafka/kafka_2.13-2.4.1/bin/zookeeper-server-start.sh /usr/local/kafka/kafka_2.13-2.4.1/config/zookeeper.properties (code=exited, status=127)
Main PID: 4882 (code=exited, status=127)
Apr 02 16:19:24 centos.localdomain systemd[1]: Started zookeeper.service.
Apr 02 16:19:24 centos.localdomain systemd[1]: zookeeper.service: main process exited, code=exited, status=127/n/a
Apr 02 16:19:24 centos.localdomain systemd[1]: zookeeper.service: control process exited, code=exited status=1
Apr 02 16:19:24 centos.localdomain systemd[1]: Unit zookeeper.service entered failed state.
Apr 02 16:19:24 centos.localdomain systemd[1]: zookeeper.service failed.
The zookeeper.service file is configured as follows
[Unit]
Requires=network.target remote-fs.target
After=network.target remote-fs.target
[Service]
Type=simple
User=specadmin
ExecStart=/usr/local/kafka/kafka_2.13-2.4.1/bin/zookeeper-server-start.sh /usr/local/kafka/kafka_2.13-2.4.1/config/zookeeper.properties
ExecStop=/usr/local/kafka/kafka_2.13-2.4.1/bin/zookeeper-server-stop.sh
Restart=on-abnormal
[Install]
WantedBy=multi-user.target
When trying to run zookeeper manually with the same user configured in the service file everything works fine.
Please advise
Turns out the issue was related to the environment variables systemd uses.
Systemd uses a fixed $PATH variable and the changes that are made to the /etc/profile /etc/bashrc and the like are not applied to systemd.
Zookeeper runs java which needs to be part of the search path, but since systemd doesn't use the files where the search path is set, zookeeper start script couldn't find java.
I solved it by overriding the search path by adding Environment=PATH=... parameter in the zookeeper service file and adding all the required directories.

Mongodb: Failed to run mongod.service

i dont know why this happening. its working normal before.
but now i cant start mongod service.
after research on google and i cant run /usr/bin/mongod --quiet --config /etc/mongod.conf by manual.
then i decide to create the .service file to run the command on background.
here's file that i write
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
i save it on /etc/systemd/system/mongod.service
but when i run systemctl start mongod its show me nothing output.
and i check the status with systemctl status mongod
and give me failed messages. here's detail message:
mongod.service - High-performance, schema-free document-oriented database Loaded: loaded (/etc/systemd/system/mongod.service;
enabled; vendor preset: enabled) Active: failed (Result: exit-code)
since Mon 2019-10-28 10:47:21 UTC; 27s ago Process: 17668
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
(code=exited, status=1/FAILURE) Main PID: 17668 (code=exited,
status=1/FAILURE)
Oct 28 10:47:21 production.seletra.com systemd[1]: Started
High-performance, schema-free document-oriented database. Oct 28
10:47:21 production.seletra.com systemd[1]: Starting High-performance,
schema-free document-oriented database... Oct 28 10:47:21
production.seletra.com mongod[17668]: 2019-10-28T10:47:21.743+0000 F
CONTROL Failed global initialization: BadValue Invalid or no user
locale set. Please ensure LANG...t correctly. Oct 28 10:47:21
production.seletra.com systemd[1]: mongod.service: main process
exited, code=exited, status=1/FAILURE Oct 28 10:47:21
production.seletra.com systemd[1]: Unit mongod.service entered failed
state. Oct 28 10:47:21 production.seletra.com systemd[1]:
mongod.service failed. Hint: Some lines were ellipsized, use -l to
show in full.
how i can fix it?
or maybe is there any way to manipulated this code /usr/bin/mongod --quiet --config /etc/mongod.conf to run on background?

What is difference between running mongodb with "Sudo service mongod start " and mongod -f </pathToConf File>

I have Replica set running with command "Sudo service mongod start" , i killed all mongod process using "kill" command after that i run replicaSet with (mongod -f /etc/mongod.conf), Now if i tries to run same replicaSet with "sudo service mongod start" after killing them again gives me error.
Output:
sudo service mongod start
Redirecting to /bin/systemctl start mongod.service
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
systemctl status mongod.service:
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2018-10-29 12:24:40 PDT; 23s ago
Docs: https://docs.mongodb.org/manual
Process: 4841 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=1/FAILURE)
Process: 4838 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 4836 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 4831 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Main PID: 4558 (code=exited, status=0/SUCCESS)
Oct 29 12:24:40 centos systemd[1]: Starting High-performance, schema-free document-oriented database...
Oct 29 12:24:40 centos mongod[4841]: about to fork child process, waiting until server is ready for connections.
Oct 29 12:24:40 centos mongod[4841]: forked process: 4844
Oct 29 12:24:40 centos mongod[4841]: ERROR: child process failed, exited with error number 1
Oct 29 12:24:40 centos mongod[4841]: To see additional information in this output, start without the "--fork" option.
Oct 29 12:24:40 centos systemd[1]: mongod.service: control process exited, code=exited status=1
Oct 29 12:24:40 centos systemd[1]: Failed to start High-performance, schema-free document-oriented database.
Oct 29 12:24:40 centos systemd[1]: Unit mongod.service entered failed state.
Oct 29 12:24:40 centos systemd[1]: mongod.service failed.
Thanks!