Phần 5 Add thêm node trong cụm MongoDB Replicaset đã có sẵn dữ liệu
Phần 5 Add thêm node trong cụm MongoDB Replicaset đã có sẵn dữ liệu
1./ Cài đặt server MongoDB
Cài đặt cùng version với cụm cluster cũ. Ở đây là MongoDB 4.4
curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
apt-key list
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt update
sudo apt install mongodb-org
sudo systemctl start mongod.service
sudo systemctl enable mongod
sudo systemctl status mongod
#Check connection
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
#
MongoDB shell version v4.4.12
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3ea974a4-b595-4255-a6a8-a0745beac434") }
MongoDB server version: 4.4.12
{
"authInfo" : {
"authenticatedUsers" : [ ],
"authenticatedUserRoles" : [ ]
},
"ok" : 1
}
https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-20-04
2./ Join vào cụm đã tồn tại
Tạo file hosts
10.0.2.11 mongo0.replset.member
10.0.2.12 mongo1.replset.member
10.0.2.13 mongo2.replset.member
10.0.2.14 mongo3.replset.member
Kiểm tra thông tin Replicaset Cluster
rs.status()
"set" : "rs0",
#Như vậy Replicaset name là rs0
Trên node thứ 4
Sửa file /etc/mongo.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,mongo3.replset.member
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
#operationProfiling:
#replication:
replication:
replSetName: "rs0"
#Specify config file.
mongod --config /etc/mongod.conf
Start the new mongod instance. Specify the data directory and the replica set name. The following example specifies the /srv/mongodb/db0 data directory and the rs0 replica set:
mongod --dbpath /srv/mongodb/db0 --replSet rs0 --bind_ip localhost,<hostname(s)|ip address(es)>
#lệnh chạy:
mongod --dbpath /var/lib/mongodb --replSet rs0 --bind_ip localhost,mongo3.replset.member,127.0.0.1
#kết quả:
{"t":{"$date":"2022-02-28T10:36:28.791+07:00"},"s":"I", "c":"CONTROL", "id":4784925, "ctx":"initandlisten","msg":"Shutting down free monitoring"}
{"t":{"$date":"2022-02-28T10:36:28.792+07:00"},"s":"I", "c":"STORAGE", "id":4784927, "ctx":"initandlisten","msg":"Shutting down the HealthLog"}
{"t":{"$date":"2022-02-28T10:36:28.793+07:00"},"s":"I", "c":"STORAGE", "id":4784929, "ctx":"initandlisten","msg":"Acquiring the global lock for shutdown"}
{"t":{"$date":"2022-02-28T10:36:28.794+07:00"},"s":"I", "c":"-", "id":4784931, "ctx":"initandlisten","msg":"Dropping the scope cache for shutdown"}
{"t":{"$date":"2022-02-28T10:36:28.795+07:00"},"s":"I", "c":"FTDC", "id":4784926, "ctx":"initandlisten","msg":"Shutting down full-time data capture"}
{"t":{"$date":"2022-02-28T10:36:28.795+07:00"},"s":"I", "c":"CONTROL", "id":20565, "ctx":"initandlisten","msg":"Now exiting"}
{"t":{"$date":"2022-02-28T10:36:28.795+07:00"},"s":"I", "c":"CONTROL", "id":23138, "ctx":"initandlisten","msg":"Shutting down","attr":{"exitCode":48}}
Bước 3: kết nối đến Repicaset Primary
#
rs0:PRIMARY> db.hello()
{
"topologyVersion" : {
"processId" : ObjectId("621605c87307c6ab3cdabdf1"),
"counter" : NumberLong(6)
},
"hosts" : [
"mongo0.replset.member:27017",
"mongo1.replset.member:27017",
"mongo2.replset.member:27017"
],
"setName" : "rs0",
"setVersion" : 1,
"isWritablePrimary" : true,
"secondary" : false,
"primary" : "mongo0.replset.member:27017",
"me" : "mongo0.replset.member:27017",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1646019826, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2022-02-28T03:43:46Z"),
"majorityOpTime" : {
"ts" : Timestamp(1646019826, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2022-02-28T03:43:46Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2022-02-28T03:43:51.418Z"),
"logicalSessionTimeoutMinutes" : 30,
"connectionId" : 121,
"minWireVersion" : 0,
"maxWireVersion" : 9,
"readOnly" : false,
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1646019826, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1646019826, 1)
}
#trên node primary add member mới
#Priority 0 và votes 0 để cho node mới không tham gia quá trình được đọc do dữ liệu chưa đủ, và cũng không được trở thành 1 node chính.
Sau khi node mới được sync đầy đủ dữ liệu và trạng thái là SECONDARY thì mới tiếp tục thay đổi priority và votes
rs.add( { host: "mongo3.replset.member:27017", priority: 0, votes: 0 } )
#Kết quả
rs0:PRIMARY> rs.add( { host: "mongo3.replset.member:27017", priority: 0, votes: 0 } )
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1646019993, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1646019993, 1)
}
#Trên node chính tiếp tục kiểm tra trạng thái của node mới là node 4 xem đã chuyển trạng thái secondary chưa
rs.status()
#
{
"_id" : 3,
"name" : "mongo3.replset.member:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 8,
"optime" : {
"ts" : Timestamp(1646020886, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1646020886, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-02-28T04:01:26Z"),
"optimeDurableDate" : ISODate("2022-02-28T04:01:26Z"),
"lastAppliedWallTime" : ISODate("2022-02-28T04:01:26.809Z"),
"lastDurableWallTime" : ISODate("2022-02-28T04:01:26.809Z"),
"lastHeartbeat" : ISODate("2022-02-28T04:01:33.982Z"),
"lastHeartbeatRecv" : ISODate("2022-02-28T04:01:34.239Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 2,
"configTerm" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1646020886, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1646020886, 1)
}
Như vậy node đã ở trạng thái SECONDARY thành công.
Tiếp theo sẽ triển khai
var cfg = rs.conf();
cfg.members[3].priority = 1
cfg.members[3].votes = 1
rs.reconfig(cfg)
#Kết quả
rs0:PRIMARY> var cfg = rs.conf();
rs0:PRIMARY> cfg.members[3].priority = 1
1
rs0:PRIMARY> cfg.members[3].votes = 1
1
rs0:PRIMARY> rs.reconfig(cfg)
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1646022182, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1646022182, 1)
}
https://docs.mongodb.com/manual/tutorial/expand-replica-set/
https://docs.mongodb.com/v4.4/reference/configuration-options/