MongoDB cluster on a single machine - RHEL8

190 Views Asked by At

I followed the following guide to create a MongoDB replica-set on a single RHEL 8 VM. I used following commands as mentioned in that guide.

sudo mkdir -p /usr/local/var/mongodb/pki/
sudo su
openssl rand -base64 741 > /usr/local/var/mongodb/pki/replicaset-keyfile
chmod 400 /usr/local/var/mongodb/pki/replicaset-keyfile

Then, I created a node1.conf as described below.

storage:
  dbPath: /usr/local/var/mongodb/node1
net:
  bindIp: localhost
  port: 27011
security:
  authorization: enabled
  keyFile: /usr/local/var/mongodb/pki/replicaset-keyfile
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/node1/mongod.log
  logAppend: true
processManagement:
  fork: true
replication:
  replSetName: replicaset-example

After that I used following commands as instructed in the guide.

sudo mkdir -p /usr/local/var/mongodb/node1
sudo mkdir -p /usr/local/var/log/mongodb/node1

I did exactly the same thing for node2 and node3 as well.

Next step in that guide was sudo mongod -f node1.conf which didn't work because mongod hadn't been installed on the system. Therefore, I installed mongod using yum.

After this, the above command worked and I was able to configure a MongoDB replica-set on those three nodes. However, after restarting the machine port 27011 was no longer active and I couldn't access the replica-set. Default port 27017 (which is mentioned in the config file created on mongodb yum installation) is active and I'm able to use MongoDB on that.

sudo mongod -f node1.conf (node2 and node3 as well), allows me to enter to the replica-set again. However, this has to be repeated after each shutdown.

How can I run the replica-set as a normal mongod service(s)?. A guide which would point me in the correct direction is also appreciated.

**Update

I tried adding a new service as guided in the below answer. However, I get this error.

Apr 12  localhost.localdomain systemd[1]: mongod_node2.service: Control process exited, code=dumped status=6
Apr 12 localhost.localdomain systemd[1]: mongod_node2.service: Failed with result 'core-dump'.

I changed Environment and PIDFile variables as instructed. Also, changed the ownership of the keyfile. I tried with and without adding ExecStartPre parts. Still the result is the same. I believe there couldn't be an issue with node1.conf file as it works fine with sudo mongod -f node1.conf command.

1

There are 1 best solutions below

1
On BEST ANSWER

First something general: You run all mongod processes on a single machine. In this case you need to set also the processManagement.pidFilePath, e.g.

processManagement:
  pidFilePath: /var/run/node1/mongod.pid

otherwise all processes will use the same default file /var/run/mongodb/mongod.pid and you have a conflict.

Now, regarding your actual question:

Install it as a service. The easiest way is to check the existing service like this:

$ systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: https://docs.mongodb.org/manual

Make a copy of /usr/lib/systemd/system/mongod.service to /etc/systemd/system/mongod_node1.service

Edit the copied file and change it according to your preferences. Mainly you need to change lines

Environment="OPTIONS=-f /etc/mongod.conf"
PIDFile=/var/run/mongodb/mongod.pid

to

Environment="OPTIONS=-f .../node1.conf"
PIDFile=/var/run/node1/mongod.pid

adding these lines would be also useful:

ExecStartPre=/usr/bin/mkdir -p /usr/local/var/mongodb/node1
ExecStartPre=/usr/bin/chown mongod:mongod /usr/local/var/mongodb/node1
ExecStartPre=/usr/bin/chmod 0755 /usr/local/var/mongodb/node1
ExecStartPre=/usr/bin/mkdir -p /usr/local/var/log/mongodb/node1
ExecStartPre=/usr/bin/chown mongod:mongod /usr/local/var/log/mongodb/node1
ExecStartPre=/usr/bin/chmod 0755 /usr/local/var/log/mongodb/node1

Then you can enable the service with

systemctl enable mongod_node1

For manual start (if required) use systemctl start mongod_node1

Just some notes:

Do not edit service file /usr/lib/systemd/system/mongod.service directly. If you run an update of your MongoDB package, then the installer will revert the file to default.

MongoDB is running under user mongod, thus you must also change the ownership of the keyfile:

 chown mongod:mongod /usr/local/var/mongodb/pki/replicaset-keyfile