Setup MongoDB ReplicaSet
1, Prepare mongodb
Create mongodb user:
1 | useradd -m mongodb -s /bin/bash |
Get mongodb binary: Download page
1 | wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-5.0.6.tgz |
2, Setup config file
0, change to directory: cd mongodb-5.0.6
1, Create keyfile:
1 | openssl rand -base64 756 > keyfile |
2, Create data folder: mkdir data
3, Add 50.conf:
1 | systemLog: |
3, increase number-of-file limits
1 | vim /etc/security/limits.conf |
let sudo read this limit, since we are using sudo to start mongodb in rc.local.
1 | vim /etc/pam.d/sudo |
Increase other limits: open file /etc/sysctl.conf, add content:
1 | fs.file-max = 98000 |
Then run sysctl -p to reload configuration with new value.
4, Create mongodb admin user
1, Change 50.conf, comments out sections:
- security
- setParameter
- replication
2, start mongod: ./bin/mongod -f 50.conf
3, connect to MongoDB: ./bin/mongo --port 40286
4, create super user:
1 | use admin; |
5, kill mongod process
6, in 50.conf, uncomment “security”, “setParameter”, “replication” sections
7, start mongod: ./bin/mongod -f 50.conf
8, create adminconn.sh:
1 | ./bin/mongo --port 40286 -u abc -p xyz --authenticationDatabase admin |
5, Add rs member’s ip/host mapping
1 | $ vim /etc/hosts |
Reminder: also need to add those hosts in application server.
6, Add ufw allow IP in all members
1 | ufw allow from 192.168.200.100 to any port 40286 proto tcp |
also add allow ip from application server (assume it is 192.168.200.200) :
1 | ufw allow from 192.168.200.200 to any port 40286 proto tcp |
7, Start MongoDB server on server reboot
Edit file: /etc/rc.local
1 |
|
8, Initiate Replica Set
Connect to database then init the replica set:
1 | rs.initiate( |
Reboot 3 db servers.