【いじり1】 

コマンドラインからレプリカセット作成と実行。

 

[pc3_centos7_k2]$ mkdir /tmp/rs0_{0,1,2}

[pc3_centos7_k2]$  mongod --replSet rs0 --port 27018 --bind_ip localhost,192.168.101.32 --dbpath /tmp/rs0_0 --oplogSize 128 &
[pc3_centos7_k2]$ mongod --replSet rs0 --port 27019 --bind_ip localhost,192.168.101.32 --dbpath /tmp/rs0_1 --oplogSize 128 &
[pc3_centos7_k2]$ mongod --replSet rs0 --port 27020 --bind_ip localhost,192.168.101.32 --dbpath /tmp/rs0_2 --oplogSize 128 &

 
[pc3_centos7_k2]$ mongo --port 27019
> rsconf = {
...   _id: "rs0",
...   members: [
...     {
...      _id: 0,
...      host: "localhost:27018"
...     },
...     {
...      _id: 1,
...      host: "localhost:27019"
...     },
...     {
...      _id: 2,
...      host: "localhost:27020"
...     }
...    ]
... }

> rs.initiate(rsconf)

rs0:PRIMARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

rs0:PRIMARY> exit
bye

 

【いじり2】 

別ホストからネットワーク越しに接続してみる

[pc3_centos7_k3]$ mongo --host 192.168.101.32 --port 27018

rs0:SECONDARY> exit
bye

[pc3_centos7_k3]$ mongo --host 192.168.101.32 --port 27019

rs0:PRIMARY> exit
bye

[pc3_centos7_k3]$ mongo --host 192.168.101.32 --port 27020

rs0:SECONDARY> exit
bye

[pc3_centos7_k3]$ ssh 192.168.101.32 ls -lh /tmp/rs0_0

root@192.168.101.32's password:

合計 800K
-rw-------. 1 root root   47 10月 19 14:51 WiredTiger
-rw-------. 1 root root   21 10月 19 14:51 WiredTiger.lock
-rw-------. 1 root root 1.3K 10月 19 16:24 WiredTiger.turtle
-rw-------. 1 root root 116K 10月 19 16:24 WiredTiger.wt
-rw-------. 1 root root  36K 10月 19 16:24 WiredTigerHS.wt
-rw-------. 1 root root  36K 10月 19 15:23 _mdb_catalog.wt
-rw-------. 1 root root  20K 10月 19 14:52 collection-0-5878475113836075537.wt
-rw-------. 1 root root  36K 10月 19 15:22 collection-10-5878475113836075537.wt
-rw-------. 1 root root  44K 10月 19 16:24 collection-16-5878475113836075537.wt
-rw-------. 1 root root  20K 10月 19 15:22 collection-17-5878475113836075537.wt
-rw-------. 1 root root  36K 10月 19 16:24 collection-2-5878475113836075537.wt
-rw-------. 1 root root  20K 10月 19 15:22 collection-21-5878475113836075537.wt
-rw-------. 1 root root 4.0K 10月 19 15:22 collection-23-5878475113836075537.wt
-rw-------. 1 root root 4.0K 10月 19 15:22 collection-25-5878475113836075537.wt
-rw-------. 1 root root  20K 10月 19 15:23 collection-27-5878475113836075537.wt
-rw-------. 1 root root  24K 10月 19 15:29 collection-29-5878475113836075537.wt
-rw-------. 1 root root  36K 10月 19 16:24 collection-4-5878475113836075537.wt
-rw-------. 1 root root  36K 10月 19 15:22 collection-6-5878475113836075537.wt
-rw-------. 1 root root  20K 10月 19 14:52 collection-8-5878475113836075537.wt
drwx------. 2 root root   71 10月 19 16:24 diagnostic.data
-rw-------. 1 root root  20K 10月 19 14:52 index-1-5878475113836075537.wt
-rw-------. 1 root root  20K 10月 19 15:21 index-11-5878475113836075537.wt
-rw-------. 1 root root  20K 10月 19 15:21 index-18-5878475113836075537.wt
-rw-------. 1 root root  20K 10月 19 15:22 index-22-5878475113836075537.wt
-rw-------. 1 root root 4.0K 10月 19 15:22 index-24-5878475113836075537.wt
-rw-------. 1 root root 4.0K 10月 19 15:22 index-26-5878475113836075537.wt
-rw-------. 1 root root  20K 10月 19 15:23 index-28-5878475113836075537.wt
-rw-------. 1 root root  20K 10月 19 15:22 index-3-5878475113836075537.wt
-rw-------. 1 root root  24K 10月 19 15:29 index-30-5878475113836075537.wt
-rw-------. 1 root root  24K 10月 19 15:29 index-31-5878475113836075537.wt
-rw-------. 1 root root  20K 10月 19 14:52 index-5-5878475113836075537.wt
-rw-------. 1 root root  20K 10月 19 14:52 index-7-5878475113836075537.wt
-rw-------. 1 root root  20K 10月 19 14:52 index-9-5878475113836075537.wt
drwx------. 2 root root  110 10月 19 14:51 journal
-rw-------. 1 root root    5 10月 19 14:51 mongod.lock
-rw-------. 1 root root  36K 10月 19 16:24 sizeStorer.wt
-rw-------. 1 root root  114 10月 19 14:51 storage.bson

[pc3_centos7_k3]$ ssh 192.168.101.32 ls -lh /tmp/rs0_0/journal
root@192.168.101.32's password:
合計 300M
-rw-------. 1 root root 100M 10月 19 16:25 WiredTigerLog.0000000001
-rw-------. 1 root root 100M 10月 19 14:51 WiredTigerPreplog.0000000001
-rw-------. 1 root root 100M 10月 19 14:51 WiredTigerPreplog.0000000002

 

【いじり3】 

上記の3台構成のレプリカセットでフェイルオーバすることを確認する

・レプリカセットの状態確認

[pc3_centos7_k3]$ mongo --host 192.168.101.32 --port 27019

rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2020-10-19T07:29:10.312Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1603092549, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-10-19T07:29:09.414Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1603092549, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-10-19T07:29:09.414Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1603092549, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1603092549, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-10-19T07:29:09.414Z"),
                "lastDurableWallTime" : ISODate("2020-10-19T07:29:09.414Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1603092489, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-10-19T06:21:59.060Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1603088508, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-10-19T06:21:59.215Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-10-19T06:22:00.471Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27018",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 4041,
                        "optime" : {
                                "ts" : Timestamp(1603092539, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1603092539, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-10-19T07:28:59Z"),
                        "optimeDurableDate" : ISODate("2020-10-19T07:28:59Z"),
                        "lastHeartbeat" : ISODate("2020-10-19T07:29:09.128Z"),
                        "lastHeartbeatRecv" : ISODate("2020-10-19T07:29:08.652Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "localhost:27019",
                        "syncSourceId" : 1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27019",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 5821,
                        "optime" : {
                                "ts" : Timestamp(1603092549, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-10-19T07:29:09Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1603088519, 1),
                        "electionDate" : ISODate("2020-10-19T06:21:59Z"),
                        "configVersion" : 1,
                        "configTerm" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27020",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 4041,
                        "optime" : {
                                "ts" : Timestamp(1603092539, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1603092539, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-10-19T07:28:59Z"),
                        "optimeDurableDate" : ISODate("2020-10-19T07:28:59Z"),
                        "lastHeartbeat" : ISODate("2020-10-19T07:29:09.127Z"),
                        "lastHeartbeatRecv" : ISODate("2020-10-19T07:29:08.651Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "localhost:27019",
                        "syncSourceId" : 1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1603092549, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1603092549, 1)
}

・データ挿入

rs0:PRIMARY> use chinko
rs0:PRIMARY> db.createCollection("chinko0")

rs0:PRIMARY> show dbs
admin   0.000GB
chinko  0.000GB
config  0.000GB
local   0.000GB

rs0:PRIMARY> show collections
chinko0
rs0:PRIMARY> db.chinko0.insertOne({name:"うんこっこ",age:20})
rs0:PRIMARY> db.chinko0.find()

{ "_id" : ObjectId("5f8d423d22c4e4d8bd644b8d"), "name" : "うんこっこ", "age" : 20 }

rs0:PRIMARY> exit
bye

mongodを実行してるホストでTCP27019でリスニングしてるインスタンスを落とす

[pc3_centos7_k2]$ ps -ef | grep mong[o]
mongod    6858     1  1 11:22 ?        00:03:12 /usr/bin/mongod -f /etc/mongod.conf
root      7055  6727  0 14:51 pts/0    00:00:53 mongod --replSet rs0 --port 27018 --bind_ip localhost,192.168.101.32 --dbpath /tmp/rs0_0 --oplogSize 128
root      7101  6727  1 14:52 pts/0    00:01:05 mongod --replSet rs0 --port 27019 --bind_ip localhost,192.168.101.32 --dbpath /tmp/rs0_1 --oplogSize 128
root      7150  6727  0 14:53 pts/0    00:00:53 mongod --replSet rs0 --port 27020 --bind_ip localhost,192.168.101.32 --dbpath /tmp/rs0_2 --oplogSize 128

[pc3_centos7_k2]$ kill 7101;echo $?
0

[pc3_centos7_k2]$ ps -ef | grep mong[o]
mongod    6858     1  1 11:22 ?        00:03:14 /usr/bin/mongod -f /etc/mongod.conf
root      7055  6727  0 14:51 pts/0    00:00:55 mongod --replSet rs0 --port 27018 --bind_ip localhost,192.168.101.32 --dbpath /tmp/rs0_0 --oplogSize 128
root      7150  6727  0 14:53 pts/0    00:00:54 mongod --replSet rs0 --port 27020 --bind_ip localhost,192.168.101.32 --dbpath /tmp/rs0_2 --oplogSize 128

[pc3_centos7_k3]$ mongo --host 192.168.101.32 --port 27020
rs0:SECONDARY> exit
bye

[pc3_centos7_k3]$ mongo --host 192.168.101.32 --port 27018

rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2020-10-19T07:46:16.122Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1603093561, 1),
                        "t" : NumberLong(2)
                },
                "lastCommittedWallTime" : ISODate("2020-10-19T07:46:01.995Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1603093561, 1),
                        "t" : NumberLong(2)
                },
                "readConcernMajorityWallTime" : ISODate("2020-10-19T07:46:01.995Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1603093561, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1603093561, 1),
                        "t" : NumberLong(2)
                },
                "lastAppliedWallTime" : ISODate("2020-10-19T07:46:01.995Z"),
                "lastDurableWallTime" : ISODate("2020-10-19T07:46:01.995Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1603093520, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "stepUpRequestSkipDryRun",
                "lastElectionDate" : ISODate("2020-10-19T07:43:10.909Z"),
                "electionTerm" : NumberLong(2),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(1603093389, 1),
                        "t" : NumberLong(1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1603093389, 1),
                        "t" : NumberLong(1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "priorPrimaryMemberId" : 1,
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-10-19T07:43:10.943Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-10-19T07:43:12.954Z")
        },
        "electionParticipantMetrics" : {
                "votedForCandidate" : true,
                "electionTerm" : NumberLong(1),
                "lastVoteDate" : ISODate("2020-10-19T06:21:59.081Z"),
                "electionCandidateMemberId" : 1,
                "voteReason" : "",
                "lastAppliedOpTimeAtElection" : {
                        "ts" : Timestamp(1603088508, 1),
                        "t" : NumberLong(-1)
                },
                "maxAppliedOpTimeInSet" : {
                        "ts" : Timestamp(1603088508, 1),
                        "t" : NumberLong(-1)
                },
                "priorityAtElection" : 1
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27018",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 6916,
                        "optime" : {
                                "ts" : Timestamp(1603093561, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2020-10-19T07:46:01Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1603093390, 1),
                        "electionDate" : ISODate("2020-10-19T07:43:10Z"),
                        "configVersion" : 1,
                        "configTerm" : 2,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27019",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2020-10-19T07:46:15.099Z"),
                        "lastHeartbeatRecv" : ISODate("2020-10-19T07:43:11.629Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "Error connecting to localhost:27019 (127.0.0.1:27019) :: caused by :: Connection refused",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27020",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 5066,
                        "optime" : {
                                "ts" : Timestamp(1603093561, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1603093561, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2020-10-19T07:46:01Z"),
                        "optimeDurableDate" : ISODate("2020-10-19T07:46:01Z"),
                        "lastHeartbeat" : ISODate("2020-10-19T07:46:14.944Z"),
                        "lastHeartbeatRecv" : ISODate("2020-10-19T07:46:15.088Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "localhost:27018",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 2
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1603093561, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1603093561, 1)
}

rs0:PRIMARY> show dbs
admin   0.000GB
chinko  0.000GB
config  0.000GB
local   0.000GB

rs0:PRIMARY> use chinko
rs0:PRIMARY> db.chinko0.find()
{ "_id" : ObjectId("5f8d423d22c4e4d8bd644b8d"), "name" : "うんこっこ", "age" : 20 }

 

3台構成のレプリカセットでは、ちゃんとレプリケーションされていて、かつ、プライマリーを落とすとちゃんとフェイルオーバーした。

 

【いじり4】 

3台構成のレプリカセットでも1台落ちてて2台しかメンバーが居ないと、偶数台構成となっているが、この状態でプライマリを落としたらフェイルオーバーするか?

 

 [pc3_centos7_k2]$ kill 7055;echo $?
0

[pc3_centos7_k2]$ ps -ef | grep mong[o]
mongod    6858     1  1 11:22 ?        00:03:22 /usr/bin/mongod -f /etc/mongod.conf
root      7150  6727  0 14:53 pts/0    00:01:05 mongod --replSet rs0 --port 27020 --bind_ip localhost,192.168.101.32 --dbpath /tmp/rs0_2 --oplogSize 128

[pc3_centos7_k2]$ mongo --port 27020

rs0:SECONDARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2020-10-19T08:00:08.594Z"),
        "myState" : 2,
        "term" : NumberLong(3),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1603094260, 1),
                        "t" : NumberLong(2)
                },
                "lastCommittedWallTime" : ISODate("2020-10-19T07:57:40.970Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1603094260, 1),
                        "t" : NumberLong(2)
                },
                "readConcernMajorityWallTime" : ISODate("2020-10-19T07:57:40.970Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1603094260, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1603094260, 1),
                        "t" : NumberLong(2)
                },
                "lastAppliedWallTime" : ISODate("2020-10-19T07:57:40.970Z"),
                "lastDurableWallTime" : ISODate("2020-10-19T07:57:40.970Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1603094260, 1),
        "electionParticipantMetrics" : {
                "votedForCandidate" : true,
                "electionTerm" : NumberLong(2),
                "lastVoteDate" : ISODate("2020-10-19T07:43:10.920Z"),
                "electionCandidateMemberId" : 0,
                "voteReason" : "",
                "lastAppliedOpTimeAtElection" : {
                        "ts" : Timestamp(1603093389, 1),
                        "t" : NumberLong(1)
                },
                "maxAppliedOpTimeInSet" : {
                        "ts" : Timestamp(1603093389, 1),
                        "t" : NumberLong(1)
                },
                "priorityAtElection" : 1
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27018",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2020-10-19T08:00:08.153Z"),
                        "lastHeartbeatRecv" : ISODate("2020-10-19T07:57:46.947Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "Error connecting to localhost:27018 (127.0.0.1:27018) :: caused by :: Connection refused",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 2
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27019",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2020-10-19T08:00:08.154Z"),
                        "lastHeartbeatRecv" : ISODate("2020-10-19T07:43:11.627Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "Error connecting to localhost:27019 (127.0.0.1:27019) :: caused by :: Connection refused",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27020",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 7618,
                        "optime" : {
                                "ts" : Timestamp(1603094260, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2020-10-19T07:57:40Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 2,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1603094260, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1603094260, 1)
}

 

→やはり偶数台構成になってるときはフェイルオーバーしなかった。

 

【いじり5】

もう一回起動

[pc3_centos7_k2]$ mongod --replSet rs0 --port 27018 --bind_ip localhost,192.168.101.32 --dbpath /tmp/rs0_0 --oplogSize 128 &

[pc3_centos7_k2]$ mongod --replSet rs0 --port 27019 --bind_ip localhost,192.168.101.32 --dbpath /tmp/rs0_1 --oplogSize 128 &

 

[pc3_centos7_k2]$ ps aux|egrep "mongo|RSS" | grep -v grep | awk '{printf "%6s%5s%5s%5s%8s%7s%6s%5s%6s%5s%9s%13s%7s%6s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12.$13,$14,$15}'
  USER   PID %CPU %MEM       VSZ    RSS    TTY STAT START TIME   COMMAND
mongod 6858     1.0      6.1 1941132 115352       ?      Sl    11:22  3:38 /usr/bin/mongod-f/etc/mongod.conf
     root 7150     0.9      7.1 2029120 133828 pts/0      Sl    14:53  1:25   mongod --replSetrs0 --port 27020
     root 7761     0.8      6.6 1917520 125832 pts/0      Sl    17:05  0:10   mongod --replSetrs0 --port 27018
     root 7847     0.8      6.5 1922884 123420 pts/0      Sl    17:06  0:09   mongod --replSetrs0 --port 27019

 

→負荷が小さいmongodbはリソースをほとんど食わない

 

【いじり6】

プライマリとセカンダリの選出

0.初期化時と初期化後共通

→レプリカセットが3メンバー構成の場合、プライマリを停止後、メンバーが残り2インスタンスになる場合ではプライマリが選出されるが、さらに1インスタンスを停止した場合停止したメンバーがプライマリ、セカンダリの違いに関係なく最後に起動してるメンバーはセカンダリになる。

※もともとプライマリだったメンバーも最後の1インスタンスになった場合やがてセカンダリに降格する。

1.初期化時

 →初期化したインスタンスがプライマリになり、それ以外がセカンダリになる。

2.初期化後

 2-1.プライマリ停止時

  →idが最若番のセカンダリメンバーがプライマリになる

 2-2.全メンバー落とし上げ時

  →最初に起動したメンバーがプライマリになる。

   すでに1インスタンスだけ起動している状態(このインスタンスは0.で確認したように

   常にセカンダリ状態)で残り2メンバーを起動した場合は、もともと起動していたインス

   タンスがプライマリになる。

 

 

 

【課題】

1.レプリカセット2台構成(マスター1台、セカンダリ1台)だと、

・手動でテイクオーバーやテイクバックがやりづらい?

・プライマリのコールドバックアップから今のところコールドリストアがうまくできない。(元のレプリカセット名でレプリカ構成に戻して起動すると勝手にフェイルオーバーしてリストア直前にロールフォワードされてしまいバックアップ取得時点へのリストアが失敗する)

なので、レプリカセット2台構成にアービターを加えて3台構成にしてためす。

 

2.上記1.に加えてmongod.confの「replication:」の設定をいろいろ試してみる

 

3./var/lib/mongo/journalディレクトリの中にできる100MB×3個のファイルの正体

 

4.チェックポイント周期をまたぐ連続データ書き込み中のデータバックアップとリストア

 

5.レプリカセットメンバー間の通信量の計測

※oplogをメンバー間で同期してるんだから、プライマリインスタンスからセカンダリインスタンスに書き込みクエリ相応のデータが転送されるはずだよね。

 

6.レプリカセット構成にしてるんだったら、もっと効率の良いバックアップ方式があるんじゃないか?(Hidden Replica Set Membersを使うとか)

 

【参考URL】

・こちらの方々のブログがためになる。

https://goodbyegangster.hatenablog.com/entry/2020/06/17/055527

https://garafu.blogspot.com/2018/02/mongodb-3instance-replicaset.html

・公式ドキュメント

https://docs.mongodb.com/manual/tutorial/deploy-replica-set/

https://docs.mongodb.com/manual/reference/configuration-options/

https://docs.mongodb.com/manual/replication/

 

【テーマ】

上記の【課題】をこなす前に、MongoDB Replica Setの概要を理解したほうがよさそう。

行き当たりばったりにテーマを設定して調べてみる。

 

【お題1】 Convert a Standalone to a Replica Set

https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/

すでになんかのデータを持っているスタンドアロンインスタンスをレプリカセットのメンバーにする方法

 

【お題2】 アービターメンバーの必要性

アービターインスタンスは普通のmongodbインスタンスだけど、データを持たない。プライマリ/セカンダリの2台構成のレプリカセットにおいてフェールオーバに必要な多数決が必ず確定するようにメンバー数を奇数にするために「だけ」存在するインスタンス。

Three member replica sets provide enough redundancy to survive most network partitions and other system failures. These sets also have sufficient capacity for many distributed read operations. Replica sets should always have an odd number of members. This ensures that elections will proceed smoothly. For more about designing replica sets, see the Replication overview.

(上記③から抜粋)

 

【お題3】 bind IP

ローカルで実行されるmongoシェル以外のクライアントやほかのレプリカセットメンバーと通信するためには、デフォルトのlocalhostへのバインドではなく、DNS名前解決にひもづくホスト名ともバインドする必要がある。

※hostsファイルでもよい。要するにMongoDB名前解決は直接DNS問合せ(bindutilesなどを使って)せず、ホスト名解決をOSに一任している(glibc内のライブラリコール)っぽい。

 

 

 

 

 

 

 

 

 

 

 

MongoDBレプリカセット作成 その1のつづき

■参考URL

https://goodbyegangster.hatenablog.com/entry/2020/06/17/055527

https://garafu.blogspot.com/2018/02/mongodb-3instance-replicaset.html

■レプリケーション検証

・プライマリ(pc3_centos7_k2)にデータを入力してセカンダリに同期するか確認する。

[データ作成前]

replicaset0:PRIMARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

 

replicaset0:SECONDARY> show dbs
uncaught exception: Error: listDatabases failed:{
        "topologyVersion" : {
                "processId" : ObjectId("5f87ebd00a0998702e3695c0"),
                "counter" : NumberLong(4)
        },
        "operationTime" : Timestamp(1602745492, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1602745492, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:937:13
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1

→おこられたw

セカンダリへはクエリを飛ばせない?

まあいいか

・プライマリにデータを入れてみる

replicaset0:PRIMARY> use test
switched to db test
replicaset0:PRIMARY> db.createCollection("test_c1")
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1602745778, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1602745778, 1)
}

replicaset0:PRIMARY> db.test_c1.insertOne({ name:"ダギャー",age:100 })
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5f87f6028fdd454115484115")
}

replicaset0:PRIMARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB

replicaset0:PRIMARY> show collections
test_c1

 

replicaset0:SECONDARY> show dbs
uncaught exception: Error: listDatabases failed:{
        "topologyVersion" : {
                "processId" : ObjectId("5f87ebd00a0998702e3695c0"),
                "counter" : NumberLong(4)
        },
        "operationTime" : Timestamp(1602745942, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1602745942, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:937:13
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1

→セカンダリーへは相変わらずクエリを飛ばせない

・プライマリmongodサービスを停止

replicaset0:PRIMARY> exit
bye
[pc3_centos7_k2]$ systemctl stop mongod;echo $?
0

・停止直後のセカンダリ

replicaset0:SECONDARY> rs.status()
{
        "set" : "replicaset0",
        "date" : ISODate("2020-10-15T07:15:50.477Z"),
        "myState" : 2,
        "term" : NumberLong(2),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 2,
        "writableVotingMembersCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1602746092, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-10-15T07:14:52.171Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1602746092, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-10-15T07:14:52.171Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1602746092, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1602746092, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-10-15T07:14:52.171Z"),
                "lastDurableWallTime" : ISODate("2020-10-15T07:14:52.171Z")
        },
        "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
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.101.32:27017",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2020-10-15T07:15:50.038Z"),
                        "lastHeartbeatRecv" : ISODate("2020-10-15T07:14:54.700Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "Error connecting to 192.168.101.32:27017 :: caused by :: Connection refused",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                },
                {
                        "_id" : 1,
                        "name" : "192.168.101.33:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 2902,
                        "optime" : {
                                "ts" : Timestamp(1602746092, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-10-15T07:14:52Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1602746092, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1602746092, 1)
}

replicaset0:SECONDARY> show dbs
uncaught exception: Error: listDatabases failed:{
        "topologyVersion" : {
                "processId" : ObjectId("5f87ebd00a0998702e3695c0"),
                "counter" : NumberLong(4)
        },
        "operationTime" : Timestamp(1602746092, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1602746092, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:937:13
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1

・11分経過

[pc3_centos7_k2]$ systemctl status mongod | head -3
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 木 2020-10-15 16:14:55 JST; 11min ago

replicaset0:SECONDARY> rs.status()
{
        "set" : "replicaset0",
        "date" : ISODate("2020-10-15T07:27:07.802Z"),
        "myState" : 2,
        "term" : NumberLong(2),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 2,
        "writableVotingMembersCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1602746092, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-10-15T07:14:52.171Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1602746092, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-10-15T07:14:52.171Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1602746092, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1602746092, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-10-15T07:14:52.171Z"),
                "lastDurableWallTime" : ISODate("2020-10-15T07:14:52.171Z")
        },
        "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
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.101.32:27017",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2020-10-15T07:27:07.334Z"),
                        "lastHeartbeatRecv" : ISODate("2020-10-15T07:14:54.700Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "Error connecting to 192.168.101.32:27017 :: caused by :: Connection refused",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                },
                {
                        "_id" : 1,
                        "name" : "192.168.101.33:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 3579,
                        "optime" : {
                                "ts" : Timestamp(1602746092, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-10-15T07:14:52Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1602746092, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1602746092, 1)
}

→フェイルオーバーしないぷに。

レプリカセットのメンバーが奇数台(Arbiterが居る場合はこれを含めて)でないとフェイルオーバーしないのか?

 

ていうか、例えばHAクラスタリングを目的としてレプリカセットするのではなく、Hidden Replica Set Membersを使ってバックアップをすることが目的なら、プライマリからセカンダリまでちゃんとデータが同期してることだけを確認してみればいいのではないか?

■セカンダリにデータが同期していることの確認2

・セカンダリを強制的にプライマリに初期化してみる(自己流)

replicaset0:SECONDARY> rs.initiate( {
... _id: "replicaset0",
... members: [
...   { _id: 0, host: "192.168.101.33:27017" }
... ]
... })

{
        "operationTime" : Timestamp(1602746092, 1),
        "ok" : 0,
        "errmsg" : "already initialized",
        "code" : 23,
        "codeName" : "AlreadyInitialized",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1602746092, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

→できない。

 

・セカンダリのconfigからレプリカセットの記述を削除して再起動してみる(自己流)

replicaset0:SECONDARY> exit
bye

[pc3_centos7_k3]$ cp -p /etc/mongod.conf /etc/mongod.conf.replocaset0
[pc3_centos7_k3]$ cp -p /etc/mongod.conf.org /etc/mongod.conf

cp: `/etc/mongod.conf' を上書きしますか? y
[pc3_centos7_k3]$ systemctl restart mongod;echo $?
0

[pc3_centos7_k3]$ mongo

----(略)----

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB     ←居た

> db.test_c1.find()
{ "_id" : ObjectId("5f87f6028fdd454115484115"), "name" : "ダギャー", "age" : 100 }

→同期はされていて、configをoriginal(シングル構成)に戻してmongodを再起動したら、プライマリに書き込まれたデータが普通に読めた。また、データを追加したあとmongodを再起動しても同様にデータのread/writeが普通にできた。-----①

 

・レプリカセットから抜けたpc3_centos7_k3にデータを追記して、pc3_centos7_k2とpc3_centos7_k3を再起動してプライマリとセカンダリをあべこべにして(pc3_centos7_k3をプライマリ)、初期化したら今度は、pc3_centos7_k2に追記したデータがpc3_centos7_k3に同期されて普通に読み書きできるか?

→確かめる

・pc3_centos7_k3にデータを追記

> use test
switched to db test
> db.test_c1.insertOne({ name:"ぴょろりん",age:0 })
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5f880b3595172ac02547b162")
}

> db.test_c1.find()
{ "_id" : ObjectId("5f87f6028fdd454115484115"), "name" : "ダギャー", "age" : 100 }
{ "_id" : ObjectId("5f880b3595172ac02547b162"), "name" : "ぴょろりん", "age" : 0 }

> exit
bye

・pc3_centos7_k3をレプリカセットreplicaset0に加える

[pc3_centos7_k3]$ cp -p /etc/mongod.conf{.replicaset0,}
cp: `/etc/mongod.conf' を上書きしますか? y

[pc3_centos7_k3]$ 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_k3のmongodを再起動

[pc3_centos7_k3]$ systemctl restart mongod;echo $?
0

・レプリカセットreplicaset0のプライマリとセカンダリをあべこべに変更

 

→やったことは下記の通り

1.プライマリからmongoシェルでプライマリとセカンダリをあべこべに初期化(自己流)

replicaset0:PRIMARY> rs.initiate( {
... _id: "replicaset0",
... members: [
...   { _id: 0, host: "192.168.101.33:27017" },
...   { _id: 1, host: "192.168.101.32:27017" },
... ]
... })

{
        "operationTime" : Timestamp(1603073825, 1),
        "ok" : 0,
        "errmsg" : "already initialized",
        "code" : 23,
        "codeName" : "AlreadyInitialized",

        "$clusterTime" : {
                "clusterTime" : Timestamp(1603073825, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

→初期化はエラー

 ※初期化は一回ポッキリしかできない?

 

2.プライマリとセカンダリのmongod.confを一旦シングル構成(original)に戻して、再起動してから、もう一度両インスタンスのmongod.confを元のレプリカセットと同じ名前でレプリカセット構成にして両インスタンスを再起動

 

→再起動したらなぜかすでに初期化済みになっていた。

rs.initiate()でプライマリとセカンダリをあべこべで初期化しようとしたら上記と同じエラーになった。

 

※プライマリとセカンダリをあべこべにする(テイクオーバーまたはテイクバック)は保留。

 

■プライマリデータのコールドバックアップ検証

・プライマリのコールドバックアップをコールドリストアしたらどうなるか?

1.レプリカセットを有効にしたままコールドリストア

[やったこと]

・プライマリとセカンダリを停止

・プライマリのデータをコールドバックアップ(/var/lib/mongoをtarでアーカイブ化)

・プライマリとセカンダリを起動

・プライマリにデータ挿入

 ----プライマリとセカンダリが同期するまで念のため10分くらい待つ----

・プライマリとセカンダリを停止

・プライマリをコールドリストア(上記アーカイブを展開して/var/lib/mongoを上書き)

・プライマリ→セカンダリの順でインスタンス起動

[結果]

元セカンダリがプライマリ、元プライマリがセカンダリにフェイルオーバーした

中のデータはリストア直前に戻っていた。(バックアップのリストア失敗)

 

2.レプリカセットを無効にしてコールドリストア

[やったこと]

・プライマリとセカンダリを停止

・プライマリのデータをコールドバックアップ(/var/lib/mongoをtarでアーカイブ化)

・プライマリとセカンダリを起動

・プライマリにデータ挿入

 ----プライマリとセカンダリが同期するまで念のため10分くらい待つ----

・プライマリとセカンダリを停止

・プライマリをコールドリストア(上記アーカイブを展開して/var/lib/mongoを上書き)

・両インスタンスのmongod.confをインストール直後の初期値に戻す

 ※両インスタンスは非レプリカセット構成に戻る。

・元プライマリ→元セカンダリの順でインスタンス起動

[結果]

・元プライマリの中のデータはバックアップ取得時点に戻っていた。(バックアップのリストア成功)

・ただし、元プライマリと元セカンダリインスタンスを、また元のレプリカセット名でレプリカセットのmongod.confに置き換えて、元プライマリ→元セカンダリの順で起動したら、1.と同様、自動でフェイルオーバーして元プライマリがセカンダリ、元セカンダリがプライマリになっていて、リストアしたデータが上書き同期されてリストア直前のデータにロールフォワードしてしまった。(リストア失敗)

 

【課題】

上記から、レプリカセット2台構成(マスター1台、セカンダリ1台)だと、

・手動でテイクオーバーやテイクバックがやりづらい?

・プライマリのコールドバックアップから今のところコールドリストアがうまくできない。(元のレプリカセット名でレプリカ構成に戻して起動すると勝手にフェイルオーバーしてリストア直前にロールフォワードされてしまいバックアップ取得時点へのリストアが失敗する)