Installing MongoDB
MongoDB is configured as a replica set across multiple nodes with an arbiter, and includes user and role setup. Monitoring is enabled using MongoDB Exporter.
Install MongoDB on the designated VMs and configure replica set with required ports and users.
To install the MongoDB, follow the steps below:
- Configure MongoDB repository and
install.
vi /etc/yum.repos.d/mongodb-org-6.0.repo [mongodb-org-6.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/6.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://pgp.mongodb.com/server-6.0.asc yum install -y mongodb-org - Update systemd configuration for replica
set.
echo "exclude=mongodb-org,mongodb-org-database,mongodb-org-server,mongodb-mongosh,mongodb-org-mongos,mongodb-org-tools" >> /etc/yum.conf sed -i "s|OPTIONS=-f /etc/mongod.conf|OPTIONS=--replSet cdprsg -f /etc/mongod.conf" /usr/lib/systemd/system/mongod.service vi /etc/mongod.conf - Configure data directories and SELinux
permissions.
chown -R mongod: /sdd chcon -R --reference=/var/lib/mongo /sdd semanage port -a -t mongod_port_t -p tcp 51090 systemctl daemon-reload systemctl restart mongod systemctl status mongod nc -zv puapso1bhxcdmgodbs1002.hxcd.aws.hclsw.internal 5190 nc -zv puapso1ahxcdmgodbs1003.hxcd.aws.hclsw.internal 5190 nc -zv puapso1bhxcdmgodbs1004.hxcd.aws.hclsw.internal 5190 nc -zv puapso1ahxcdmgodbs1005 5190 - Configure replica
set.
mongosh --host <primary-host> --port 51090 use admin; conf = { _id: "cdprsg", members: [ { _id: 0, host: "<host1>:51090", tags: { usage: "schedule" } }, { _id: 1, host: "<host2>:51090", tags: { usage: "schedule" } }, { _id: 2, host: "<host3>:51090", tags: { usage: "segloader" } }, { _id: 3, host: "<host4>:51090", tags: { usage: "analytics" } } ] } rs.initiate(conf) rs.addArb("<arbiter-host>:51090") conf = rs.conf() conf.members[3].hidden = true rs.status() - Create required MongoDB users.
use admin; db.createUser({ user: "rtsms", pwd: "<password>", roles: [{ role: "readWrite", db: "segmentation" }, { role: "dbAdmin", db: "segmentation" }] }); db.createUser({ user: "segdbconnector", pwd: "<password>", roles: [{ role: "readWrite", db: "segmentation" }] }); db.createUser({ user: "pmm", pwd: "<password>", roles: [ { role: "backup", db: "admin" }, { role: "clusterMonitor", db: "admin" }, { role: "read", db: "local" }, { role: "readWrite", db: "admin" }, { role: "restore", db: "admin" } ] }); openssl rand -base64 16
Node Exporter
Node Exporter installed on MongoDB virtual machines collect systems metrics such as CPU, memory, disk, and network usage. These metrics are then scraped by Prometheus for monitoring and visualization.
To install node exporter, follow the steps below:
- Download and extract Node
Exporter.
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz tar xfz node_exporter-1.7.0.linux-amd64.tar.gz - Move binary and create system
user.
sudo mv node_exporter-1.7.0.linux-amd64/node_exporter /usr/local/bin/ sudo useradd -rs /bin/false node_exporter - Create a systemd service
unit.
Paste the following contents:vi /etc/systemd/system/node_exporter.service[Unit] Description=Node Exporter After=network.target [Service] User=node_exporter Group=node_exporter Type=simple ExecStart=/usr/local/bin/node_exporter [Install] WantedBy=multi-user.target - Start and enable
service.
restorecon -Rv /usr/local/bin systemctl daemon-reload systemctl start node_ systemctl status node_exporter systemctl enable node_exporter - To validate metrics endpoint and exporter status, run this command to confirm the
service is
up.
curl -kv http://localhost:9100/metrics
MongoDB Prometheus Exporter
This section details how to install and configure Percona's MongoDB Exporter to expose MongoDB metrics to Prometheus for scraping and monitoring. To install Prometheus exporter, follow the steps below:
- Install the Exporter
Package.
wget https://github.com/percona/mongodb_exporter/releases/download/v0.40.0/mongodb_exporter-0.40.0.linux-64-bit.rpm rpm -Uhv mongodb_exporter-0.40.0.linux-64-bit.rpm - Create a systemd Unit
File.
Paste the following contents:vi /etc/systemd/system/mongodb_exporter.service[Unit] Description=Prometheus MongoDB Exporter After=network.target [Service] Type=simple User=mongodb_exporter Group=mongodb_exporter Environment="OPTIONS=--mongodb.uri=mongodb://pmm:<password>@<mongo-host>:51090 --compatible-mode" ExecStart=/usr/bin/mongodb_exporter $OPTIONS Restart=always [Install] WantedBy=multi-user.targetNote: Replace<password>and<mongo-host>with actual credentials and hostname. - Reload and Start the
Service.
systemctl daemon-reload systemctl start mongodb_exporter systemctl status mongodb_exporter systemctl enable mongodb_exporter - Run this command to validate and confirm the service is
up.
curl -kv http://localhost:9216/metrics