MongoDB Replication


MongoDB adalah salah satu database no-sql. Pengguna MongoDB sampai dengan hari ini sudah cukup banyak. Bagi yang terheran-heran, database ini tidak memiliki struktur yang komprehensif. Bagi yang mengenal relational database maka ada beberapa istilah pada MongoDB tidak ada namun cenderung mirip. Di mana istilah yang perlu diketahui di antaranya:

Table menjadi collection

Row / baris data menjadi document

Field / column menjadi key object

Value tetaplah value


Tipe data yang digunakan standardnya:

Boolean (true / false) supported

Number (int, long, decimal, float) supported

String pastinya


Artikel terkait no-sql sendiri dapat dibaca di url berikut

https://aws.amazon.com/id/nosql/


Sebelum melakukan replikasi mongodb, pertama-tama kita perlu install mongodb terlebih dahulu.

Import public key, first

$ wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add –


Add MongoDB repository

$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list


Update package repo

$ sudo apt-get update


Install MongoDB

$ sudo apt-get install mongodb-org


Run MongoDB

$ sudo systemctl start mongod


Enable MongoDB run automatically after boot

$ sudo systemctl enable mongod




Setelah proses instalasi mongodb selesai. Langkah selanjutnya adalah membuat user access mongodb.

Enter to Mongo Shell

$ mongo


Choose database “admin”

$ use admin


Create user on database admin with role userAdminAnyDatabase

$ db.createUser({user: "ubuntu", pwd: "ubuntu", roles: [{role: "userAdminAnyDatabase", db: "admin"}]})


Verify user login above

$ exit

$ mongo -u ubuntu -p –authenticationDatabase admin




Namanya pun replikasi database. Yang saya lakukan di sini menggunakan sebanyak 3 server linux ubuntu v-18.04 x64 pada virtual machines,  dengan ip address server-1 (192.168.198.128), server-2 (192.168.198.129), server-3 (192.168.198.130). Lakukan konfigurasi di bawah ini pada setiap virtual machines.


Edit BindIP

$ sudo nano /etc/mongod.conf

net:

   port: 27017

   bindIp: 127.0.0.1, 192.168.198.128


Save with nano: Ctrl + x , type y , press enter.


Restart MongoDB

$ sudo systemctl restart mongod




Setelah langkah di atas dilakukan, sekarang lakukan konfigurasi hostname.

Change hostname on current node vm (192.168.198.128) as primary

$ sudo hostnamectl set-hostname rpl-1.mongo.local


Move to 2nd node vm (192.168.198.129) as secondary

$ sudo hostnamectl set-hostname rpl-2.mongo.local


Move to 3rd node vm (192.168.198.130) as secondary

$ sudo hostnamectl set-hostname rpl-3.mongo.local




Nah, lanjutkan konfigurasi /etc/hosts , lakukan config di bawah ini pada ketiga virtual machines.

Edit /etc/hosts

$ sudo nano /etc/hosts

192.168.198.128     rpl-1.mongo.local

192.168.198.129     rpl-2.mongo.local

192.168.198.130     rpl-3.mongo.local


Save with nano: Ctrl + x , type y , press enter.




Barulah kita masuk ke config replikasi mongodb.

Edit config file of MongoDB (on vm 192.168.198.128)

$ sudo nano /etc/mongod.conf

net:

   port: 27017

   bindIp: 127.0.0.1, 192.168.198.128

replication:

  replSetName: rs0


Edit config file of MongoDB (on vm 192.168.198.129)

$ sudo nano /etc/mongod.conf

net:

   port: 27017

   bindIp: 127.0.0.1, 192.168.198.129

replication:

  replSetName: rs0


Edit config file of MongoDB (on vm 192.168.198.130)

$ sudo nano /etc/mongod.conf

net:

   port: 27017

   bindIp: 127.0.0.1, 192.168.198.130

replication:

   replSetName: rs0


Save with nano: Ctrl + x , type y , press enter.


Restart MongoDB

$ sudo systemctl restart mongod




Sekarang kita akan start replication.

Only on primary node (192.168.198.128)

$ mongo

$ rs.initiate({ _id : "rs0", members: [{ _id: 0, host: "rpl-1.mongo.local:27017"},{ _id: 1, host: "rpl-2.mongo.local:27017"},{ _id: 2, host: "rpl-3.mongo.local:27017" }]})


Check config result above

$ rs.conf()




Setelah replikasi db dimulai, kita dapat mengecek status replikasi.

Position on primary node (192.168.198.128)

$ mongo

$ rs.status()




Setelah semua langkah di atas dilakukan. Cobalah untuk shutdown masing-masing virtual machines. So, previously 192.168.198.128 as primary, but after reboot the vm 192.168.198.128 being secondary, and 192.168.198.130 become primary. (Force 1st Node as Primary Replication)

So, how to solve the problem above?

Here is the hack method revert 1st node vm 192.168.198.128 become primary again.


Position on vm 192.168.198.130 (that currently active as primary)

$ mongo

$ config = { _id : "rs0", members: [{ _id: 0, host: "rpl-1.mongo.local:27017", priority: 1},{ _id: 1, host: "rpl-2.mongo.local:27017", priority: 0},{ _id: 2, host: "rpl-3.mongo.local:27017", priority: 0}]}

$ rs.reconfig(config, {force: true})


Restart MongoDB

$ sudo systemctl restart mongod


Shutdown all vms. Start boot from 192.168.198.130, then 192.168.198.129, the last one 192.168.198.128 Don’t forget to check replication status.

$ rs.status()




Sekarang saatnya replication test.

Open Robo 3T

Connect to 192.168.198.128

Create new database for example: dbexample

and then create new collection: warga

Insert new document: {nama: “Pak RW”}

For 2nd document: {nama: “Pak RT”}

For 3rd document: {nama: “Ketua DKM”}


Now, open 192.168.198.129 and 192.168.198.130

and then check the replicate rest result.

Good luck.


Credit:

* Linux Ubuntu 18.04

* MongoDB v4.4




Source :

https://aws.amazon.com/id/nosql/

Comments