■参考URL
https://goodbyegangster.hatenablog.com/entry/2020/06/17/055527
https://garafu.blogspot.com/2018/02/mongodb-3instance-replicaset.html
■構成
ホスト名 IPアドレス
VM1 pc3_centos7_k2 192.168.101.32
VM2 pc3_centos7_k3 192.168.101.33
※VM1とVM2はいずれも下記の通り
OS CentOS7.3
メモリ 2GB
CPU 2core
MongoDBバージョン version v4.4.1
■レプリカセット作成前の状態確認
[pc3_centos7_k2]$ mongo
----(略)----
> rs.status()
{
"ok" : 0,
"errmsg" : "not running with --replSet",
"code" : 76,
"codeName" : "NoReplicationEnabled"
}
> rs.conf()
uncaught exception: Error: Could not retrieve replica set config: {
"ok" : 0,
"errmsg" : "not running with --replSet",
"code" : 76,
"codeName" : "NoReplicationEnabled"
} :
rs.conf@src/mongo/shell/utils.js:1595:11
@(shell):1:1
> rs.initiate()
{
"ok" : 0,
"errmsg" : "This node was not started with the replSet option",
"code" : 76,
"codeName" : "NoReplicationEnabled"
}
> rs.status()
{
"ok" : 0,
"errmsg" : "not running with --replSet",
"code" : 76,
"codeName" : "NoReplicationEnabled"
}
> rs.conf()
uncaught exception: Error: Could not retrieve replica set config: {
"ok" : 0,
"errmsg" : "not running with --replSet",
"code" : 76,
"codeName" : "NoReplicationEnabled"
} :
rs.conf@src/mongo/shell/utils.js:1595:11
@(shell):1:1
> exit
bye
■検証準備
[pc3_centos7_k2]$ setenforce 0
[pc3_centos7_k2]$ iptables -F
[pc3_centos7_k2]$ cp -p /etc/mongod.conf{,.org}
[pc3_centos7_k3]$ setenforce 0
[pc3_centos7_k3]$ iptables -F
[pc3_centos7_k3]$ cp -p /etc/mongod.conf{,.org}
■Replica Setの作成
[pc3_centos7_k2]$ vi /etc/mongod.conf
----(略)----
[pc3_centos7_k2]$ diff /etc/mongod.conf{.org,}
29c29,30
< bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
---
> # bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
> bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
36a38,41
> replication:
> oplogSizeMB: 30
> replSetName: "replicaset0"
> enableMajorityReadConcern: false
[pc3_centos7_k2]$ scp mongod.conf 192.168.101.33:/etc/
----(略)----
■Replica Setの初期化
・まだmongodサービスを再起動してない。
[pc3_centos7_k2]$ mongo
----(略)----
> rs.status()
{
"ok" : 0,
"errmsg" : "not running with --replSet",
"code" : 76,
"codeName" : "NoReplicationEnabled"
}
> rs.config()
uncaught exception: Error: Could not retrieve replica set config: {
"ok" : 0,
"errmsg" : "not running with --replSet",
"code" : 76,
"codeName" : "NoReplicationEnabled"
} :
rs.conf@src/mongo/shell/utils.js:1595:11
@(shell):1:1
→当然まだ何も変わってない。
> rs.initiate( {
... _id: "replicaset0",
... members: [
... { _id: 0, host: "192.168.101.32:27017" },
... { _id: 1, host: "192.168.101.33:27017" }
... ]
... })
{
"ok" : 0,
"errmsg" : "This node was not started with the replSet option",
"code" : 76,
"codeName" : "NoReplicationEnabled"
}
→mongodサービスを再起動しないと初期化はできない。
> exit
bye
・mongodサービスを再起動
[pc3_centos7_k2]$ systemctl restart mongod
[pc3_centos7_k2]$ echo $?
0
[pc3_centos7_k3]$ systemctl restart mongod
[pc3_centos7_k3]$ echo $?
0
[pc3_centos7_k2]$ mongo
----(略)----
> rs.status()
{
"operationTime" : Timestamp(0, 0),
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
> rs.config()
uncaught exception: Error: Could not retrieve replica set config: {
"operationTime" : Timestamp(0, 0),
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
rs.conf@src/mongo/shell/utils.js:1595:11
@(shell):1:1
→エラー内容が「まだ初期化してない」に変わった。
・Replica Setの初期化
※初期化はプライマリ側(pc3_centos7_k2)でのみ行う。
> rs.initiate( {
... _id: "replicaset0",
... members: [
... { _id: 0, host: "192.168.101.32:27017" },
... { _id: 1, host: "192.168.101.33:27017" }
... ]
... })
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1602744480, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1602744480, 1)
}
・確認
replicaset0:PRIMARY> rs.status()
{
"set" : "replicaset0",
"date" : ISODate("2020-10-15T06:54:58.001Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 2,
"writableVotingMembersCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1602744892, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-10-15T06:54:52.132Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1602744892, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-10-15T06:54:52.132Z"),
"appliedOpTime" : {
"ts" : Timestamp(1602744892, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1602744892, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-10-15T06:54:52.132Z"),
"lastDurableWallTime" : ISODate("2020-10-15T06:54:52.132Z")
},
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-10-15T06:48:11.864Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1602744480, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2020-10-15T06:48:12.012Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-10-15T06:48:13.203Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.101.32:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1839,
"optime" : {
"ts" : Timestamp(1602744892, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-10-15T06:54:52Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1602744491, 1),
"electionDate" : ISODate("2020-10-15T06:48:11Z"),
"configVersion" : 1,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.101.33:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 417,
"optime" : {
"ts" : Timestamp(1602744892, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1602744892, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-10-15T06:54:52Z"),
"optimeDurableDate" : ISODate("2020-10-15T06:54:52Z"),
"lastHeartbeat" : ISODate("2020-10-15T06:54:56.105Z"),
"lastHeartbeatRecv" : ISODate("2020-10-15T06:54:57.631Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "192.168.101.32:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1602744892, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1602744892, 1)
}
replicaset0:PRIMARY> rs.config()
{
"_id" : "replicaset0",
"version" : 1,
"term" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "192.168.101.32:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.101.33:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5f87f0a0f4ed6ead8f503a5d")
}
}
・すかさずpc3_centos7_k3で確認。
[pc3_centos7_k3]$ mongo
----(略)----
replicaset0:SECONDARY> rs.status()
{
"set" : "replicaset0",
"date" : ISODate("2020-10-15T06:58:36.655Z"),
"myState" : 2,
"term" : NumberLong(1),
"syncSourceHost" : "192.168.101.32:27017",
"syncSourceId" : 0,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 2,
"writableVotingMembersCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1602745112, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-10-15T06:58:32.139Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1602745112, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-10-15T06:58:32.139Z"),
"appliedOpTime" : {
"ts" : Timestamp(1602745112, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1602745112, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-10-15T06:58:32.139Z"),
"lastDurableWallTime" : ISODate("2020-10-15T06:58:32.139Z")
},
"electionParticipantMetrics" : {
"votedForCandidate" : true,
"electionTerm" : NumberLong(1),
"lastVoteDate" : ISODate("2020-10-15T06:48:11.877Z"),
"electionCandidateMemberId" : 0,
"voteReason" : "",
"lastAppliedOpTimeAtElection" : {
"ts" : Timestamp(1602744480, 1),
"t" : NumberLong(-1)
},
"maxAppliedOpTimeInSet" : {
"ts" : Timestamp(1602744480, 1),
"t" : NumberLong(-1)
},
"priorityAtElection" : 1,
"newTermStartDate" : ISODate("2020-10-15T06:48:12.012Z"),
"newTermAppliedDate" : ISODate("2020-10-15T06:48:13.186Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.101.32:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 635,
"optime" : {
"ts" : Timestamp(1602745112, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1602745112, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-10-15T06:58:32Z"),
"optimeDurableDate" : ISODate("2020-10-15T06:58:32Z"),
"lastHeartbeat" : ISODate("2020-10-15T06:58:35.740Z"),
"lastHeartbeatRecv" : ISODate("2020-10-15T06:58:36.211Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1602744491, 1),
"electionDate" : ISODate("2020-10-15T06:48:11Z"),
"configVersion" : 1,
"configTerm" : 1
},
{
"_id" : 1,
"name" : "192.168.101.33:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1868,
"optime" : {
"ts" : Timestamp(1602745112, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-10-15T06:58:32Z"),
"syncSourceHost" : "192.168.101.32:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1602745112, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1602745112, 1)
}
replicaset0:SECONDARY> rs.config()
{
"_id" : "replicaset0",
"version" : 1,
"term" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "192.168.101.32:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.101.33:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5f87f0a0f4ed6ead8f503a5d")
}
}