MongoDB on OEL 8 — Vagrant Setup
MongoDB is a popular NoSQL document database. While primarily a database administrator's domain is relational databases, understanding MongoDB is increasingly valuable. This page provides a complete Vagrant setup for MongoDB 7 on OEL 8 with replica set configuration.
Vagrantfile
Ruby — Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "generic/oracle8"
config.vm.hostname = "mongodb7"
config.vm.network "private_network", ip: "192.168.56.80"
config.vm.network "forwarded_port", guest: 27017, host: 127017
config.vm.provider "virtualbox" do |vb|
vb.name = "MongoDB7-OEL8"
vb.memory = 2048
vb.cpus = 2
end
config.vm.provider "parallels" do |prl|
prl.name = "MongoDB7-OEL8"
prl.memory = 2048
prl.cpus = 2
prl.update_guest_tools = false
end
config.vm.provision "shell", path: "scripts/install_mongodb.sh"
end
install_mongodb.sh
BASH — install_mongodb.sh
#!/bin/bash
# scripts/install_mongodb.sh
set -e
MONGO_VERSION="7.0"
ADMIN_PASS="Admin@123!"
echo "=== Installing MongoDB ${MONGO_VERSION} on OEL 8 ==="
# Add MongoDB repository
cat > /etc/yum.repos.d/mongodb-org-7.0.repo << EOF
[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc
EOF
# Install MongoDB
dnf install -y mongodb-org
# Configure MongoDB
cat > /etc/mongod.conf << EOF
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: enabled
EOF
# Start MongoDB
systemctl enable --now mongod
sleep 5
# Create admin user
mongosh --eval "
db = db.getSiblingDB('admin');
db.createUser({
user: 'admin',
pwd: '${ADMIN_PASS}',
roles: [
{ role: 'userAdminAnyDatabase', db: 'admin' },
{ role: 'readWriteAnyDatabase', db: 'admin' },
{ role: 'dbAdminAnyDatabase', db: 'admin' },
{ role: 'clusterAdmin', db: 'admin' }
]
});
print('Admin user created');
"
# Create sample database and data
mongosh -u admin -p "$ADMIN_PASS" --authenticationDatabase admin --eval "
db = db.getSiblingDB('labdb');
db.employees.insertMany([
{ name: 'John Smith', dept: 'Engineering', salary: 75000 },
{ name: 'Jane Doe', dept: 'Marketing', salary: 65000 },
{ name: 'Bob Johnson', dept: 'Engineering', salary: 80000 }
]);
db.employees.createIndex({ dept: 1 });
print('Sample data inserted: ' + db.employees.countDocuments() + ' documents');
"
echo "=== MongoDB ${MONGO_VERSION} Ready ==="
echo " Host: 192.168.56.80:27017 or 127.0.0.1:127017"
echo " User: admin"
echo " Pass: ${ADMIN_PASS}"
echo " mongosh mongodb://admin:Admin@123!@127.0.0.1:127017/?authSource=admin"
Connect and Test
BASH — Connect and Test
# Start VM
vagrant up
# Connect from host
mongosh "mongodb://admin:Admin@123!@127.0.0.1:127017/?authSource=admin"
# Inside mongosh
use labdb
db.employees.find()
db.employees.find({dept: "Engineering"})
db.employees.aggregate([
{ $group: { _id: "$dept", avg_salary: { $avg: "$salary" }, count: { $sum: 1 } } }
])
db.employees.countDocuments()
MongoDB Replica Set Setup
BASH — Replica Set
# Quick 3-node replica set Vagrantfile snippet
NODES = [
{ name: "mongo1", ip: "192.168.56.81" },
{ name: "mongo2", ip: "192.168.56.82" },
{ name: "mongo3", ip: "192.168.56.83" },
]
# After all nodes start — initiate replica set on mongo1
mongosh --eval "
rs.initiate({
_id: 'rs0',
members: [
{ _id: 0, host: '192.168.56.81:27017' },
{ _id: 1, host: '192.168.56.82:27017' },
{ _id: 2, host: '192.168.56.83:27017' }
]
});
"
# Check replica set status
rs.status()