Skip to content

Instantly share code, notes, and snippets.

@steamonimo
Last active April 28, 2021 18:29
Show Gist options
  • Save steamonimo/8a8733be4fb9dd98ce1952616fe809e0 to your computer and use it in GitHub Desktop.
Save steamonimo/8a8733be4fb9dd98ce1952616fe809e0 to your computer and use it in GitHub Desktop.

MongoDB: replication sets on Debian

  • start with fresh VM!
  • install Debian with XFS file system!
  • determine pool of fixed IPs (192.168.1.231-235)
  • DNS assignment of computer names (mongodb1.corp.com-mongodb5.corp.com) to IPs
  • we assume that our DNS server has the IP 192.168.1.9
  • do not create mondodb user accounts yet!

Give VM1 static IP

nano /etc/network/interfaces

iface eth0 inet static
    address 192.168.1.231
    netmask 255.255.255.0
    gateway 192.168.1.1

Configure your DNS server on VM1

nano /etc/resolv.conf

192.168.1.9 8.8.8.8 8.8.4.4

Install mongodb

apt-get install mongodb

Enable mongodb service

systemctl enable mongod

On some Debian machines the ownership of mongodb directories must be corrected

chown -R mongodb:mongodb /var/lib/mongodb

Create secret key with openssl

mkdir /etc/keys
openssl rand -base64 756 > /etc/keys/mongors.key

Secure secret key locally

chmod 400 /etc/keys/mongors.key

Configure name of replication set and IP address

nano /etc/mongod.conf

net:
    port: 27017
    bindIp: 127.0.0.1,192.168.1.231
    
replication:
    replSetName: "mongors"

Restart VM1

shutdown -r

Verify that mondodb has been started

service mongod status

Verify that DNS will be resolved

ping mongodb2.corp.com

Clone your current VM1

  • use your vm software to achieve this

Step1: On cloned VMx adjust IP

nano /etc/network/interfaces

iface eth0 inet static
    address 192.168.1.23x

Step2: On cloned VMx adjust mongodb config

nano /etc/mongod.conf

net:
    port: 27017
    bindIp: 127.0.0.1,192.168.1.23x

Step3: Restart VMx

shutdown -r

Loop above three steps for all VMs

Logon to mongodb shell on VM1

mongo --host mongodb1.corp.com

mongodb shell: Initiate replication set

rs.initiate({_id : "mongors", members: [
    { _id : 0, host : "mongodb1.corp.com:27017" },
    { _id : 1, host : "mongodb2.corp.com:27017" },
    { _id : 2, host : "mongodb3.corp.com:27017" }
    { _id : 3, host : "mongodb4.corp.com:27017" }
    { _id : 4, host : "mongodb5.corp.com:27017" }
]})

mongodb shell: verify status of replication set

rs.status()

mongodb shell: logout and then login to replication set [mongors:PRIMARY]

mongo mongodb://mongodb1.corp.com,mongodb2.corp.com,mongodb3.corp.com,mongodb4.corp.com,mongodb5.corp.com/?replicaSet=mongors

mongodb shell: create cluster admin account for replication set

admin = db.getSiblingDB("admin")
admin.createUser({user: "cladmin", pwd: "secret", roles: [
    { "role": "root", db: "admin" },
    { "role": "dbAdminAnyDatabase", db: "admin" },
    { "role": "userAdminAnyDatabase", db: "admin" },
    { "role": "clusterAdmin", db: "admin" },
    { "role" : "restore", "db" : "admin"}
]})

Step1: On VMx activate user security and key based communication

nano /etc/mongod.conf

security:
    authorization: enabled
    keyFile: /etc/keys/mongors.key

Step2: On VMx restart mongodb

service mongod restart

Step3: On VMx check status of mongodb service

service mongod status

Loop above three steps for all VMs

Restore dump to secured replication set

mongorestore --host
mongors/mongodb1.corp.com,
mongodb2.corp.com,
mongodb3.corp.com,
mongodb4.corp.com,
mongodb5.corp.com
-u cladmin --authenticationDatabase "admin"

Connect to mongodb shell of secured replication set

mongo
mongodb://mongodb1.corp.com,
mongodb2.corp.com,
mongodb3.corp.com,
mongodb4.corp.com,
mongodb5.corp.com/?replicaSet=mongors
-u cladmin --authenticationDatabase "admin"

Restore mongodb node from endless state "RECOVERING"

service mongod stop
cd /var/lib/mongodb
rm *.*
cd /var/lib/mongodb/journal
rm *.*
service mongod start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment