Catatan untuk pengingat sendiri
Replication database pada MongoDB
Konfigurasi dasar server:
- Primary
IP: 10.0.0.123
Hostname: server01
- Secondary
IP: 10.0.0.124
Hostname: server02
- Konfigurasi MongoDB di server Primary:
Tambahkan opsi replication set pada config mongodb:
sudo nano /etc/mongodb.conf
replSet = "gogasi"
Ubah bind_ip dari 127.0.0.1 menjadi 0.0.0.0 agar bisa diakses melalui ip public
bind_ip = 0.0.0.0
Aktifkan port bawaan MongoDB
port = 27017
Simpan dan restart instance mongodb
sudo /etc/init.d/mongodb restart
Selanjutnya tambahkan ip server Secondary ke /etc/hosts
10.0.0.124 server02
- Konfigurasi MongoDB di server Secondary
Tambahkan opsi replication set pada config mongodb:
sudo nano /etc/mongodb.conf
replSet = "gogasi"
Ubah bind_ip dari 127.0.0.1 menjadi 0.0.0.0 agar bisa diakses melalui ip public
bind_ip = 0.0.0.0
Aktifkan port bawaan MongoDB
port = 27017
Simpan dan restart instance mongodb
sudo /etc/init.d/mongodb restart
Selanjutnya tambahkan ip server Primary ke /etc/hosts
10.0.0.123 server01
Di server Primary, akses mongo shell
mongo
Inisiasi sebagai Primary
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "server01:27017",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
>
> rs.conf()
{
"_id" : "\"gogasi\"",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "server01:27017"
}
]
}
"gogasi":PRIMARY>
"gogasi":PRIMARY> rs.status()
{
"set" : "\"gogasi\"",
"date" : ISODate("2015-12-29T09:56:58Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "server01:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 372,
"optime" : Timestamp(1451382915, 1),
"optimeDate" : ISODate("2015-12-29T09:55:15Z"),
"self" : true
}
],
"ok" : 1
}
"gogasi":PRIMARY>
Di server Secondary, akses mongo shell
mongo
Inisiasi sebagai Slave
> rs.slaveOk()
Balik lagi ke server Primary dan tambahkan member, yang jadi member adalah server02
"gogasi":PRIMARY> rs.add("server02:27017")
{ "ok" : 1 }
"gogasi":PRIMARY>
Sampai di sini, proses penambahan member sudah sukses. Bisa dicek di masing-masing server
Status di Primary:
"gogasi":PRIMARY> rs.status()
{
"set" : "\"gogasi\"",
"date" : ISODate("2015-12-29T10:05:37Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "server01:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 891,
"optime" : Timestamp(1451383407, 1),
"optimeDate" : ISODate("2015-12-29T10:03:27Z"),
"self" : true
},
{
"_id" : 1,
"name" : "server02:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 130,
"optime" : Timestamp(1451383407, 1),
"optimeDate" : ISODate("2015-12-29T10:03:27Z"),
"lastHeartbeat" : ISODate("2015-12-29T10:05:37Z"),
"lastHeartbeatRecv" : ISODate("2015-12-29T10:05:36Z"),
"pingMs" : 1,
"syncingTo" : "server01:27017"
}
],
"ok" : 1
}
"gogasi":PRIMARY>
Status di Secondary:
"gogasi":STARTUP2> rs.status()
{
"set" : "\"gogasi\"",
"date" : ISODate("2015-12-29T10:05:16Z"),
"myState" : 2,
"syncingTo" : "server01:27017",
"members" : [
{
"_id" : 0,
"name" : "server01:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 98,
"optime" : Timestamp(1451383407, 1),
"optimeDate" : ISODate("2015-12-29T10:03:27Z"),
"lastHeartbeat" : ISODate("2015-12-29T10:05:16Z"),
"lastHeartbeatRecv" : ISODate("2015-12-29T10:05:15Z"),
"pingMs" : 1
},
{
"_id" : 1,
"name" : "server02:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 880,
"optime" : Timestamp(1451383407, 1),
"optimeDate" : ISODate("2015-12-29T10:03:27Z"),
"self" : true
}
],
"ok" : 1
}
"gogasi":SECONDARY>
Tahap Replication sudah selesai dan proses synchronize database lagi berlangsung
- Proses voting antar server
Tahap ini sangat penting untuk diperhatikan, jika sewaktu-waktu server primary mati dan statenya tidak berubah
Di server Primary jalankan mongo shell
mongo
Lanjut dengan deperintah berikut dalam mongo shell
"gogasi":PRIMARY> cfg = rs.conf()
{
"_id" : "\"gogasi\"",
"version" : 2,
"members" : [
{
"_id" : 0,
"host" : "server01:27017"
},
{
"_id" : 1,
"host" : "server02:27017"
}
]
}
"gogasi":PRIMARY>
"gogasi":PRIMARY> cfg.members[1].priority = 0
0
"gogasi":PRIMARY>
"gogasi":PRIMARY> rs.reconfig(cfg)
Tue Dec 29 10:14:44.816 DBClientCursor::init call() failed
Tue Dec 29 10:14:44.818 trying reconnect to 127.0.0.1:27017
Tue Dec 29 10:14:44.820 reconnect 127.0.0.1:27017 ok
reconnected to server after rs command (which is normal)
"gogasi":PRIMARY> rs.conf()
{
"_id" : "\"gogasi\"",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "server01:27017"
},
{
"_id" : 1,
"host" : "server02:27017",
"priority" : 0
}
]
}
"gogasi":PRIMARY>
Sampai di sini, member (server02) sudah diset tetap statenya sebagai SECONDARY.
Catatan: Untuk Bind Ip di production silahkan diubah untuk lebih secure aksesnya, seperti menggunakan vpn, auth atau cara lain
Sumber: https://docs.mongodb.org/v2.4/core/replication-introduction/
ConversionConversion EmoticonEmoticon