1. 程式人生 > 其它 >MongoDB新建複製集-三臺機器一主一從一仲裁

MongoDB新建複製集-三臺機器一主一從一仲裁

1 . 建立複製集目錄

mkdir -vp /etc/mongodbceshi/

2. 進一步建立對應目錄

cd /etc/mongodbceshi/

mkdir -v conf log db

cd conf/

3 .撰寫配置檔案

[root@ceshi-03 /etc/mongodbceshi/conf]# pwd
/etc/mongodbceshi/conf
[root@ceshi-03 /etc/mongodbceshi/conf]#
[root@ceshi-03 /etc/mongodbceshi/conf]#
[root@ceshi-03 /etc/mongodbceshi/conf]#
[root@ceshi-03 /etc/mongodbceshi/conf]# cat mongodb.conf


dbpath=/etc/mongodbceshi/db/
logpath=/etc/mongodbceshi/log/mongoceshi.log
pidfilepath=/etc/mongodbceshi/db/mongo.pid
logappend=true
journal=true
port=30000
replSet=rsceshi
bind_ip = 0.0.0.0
fork=true


[root@ceshi-02 conf]# pwd
/etc/mongodbceshi/conf
[root@ceshi-02 conf]#
[root@ceshi-02 conf]#
[root@ceshi-02 conf]#
[root@ceshi-02 conf]# cat mongodb.conf


dbpath=/etc/mongodbceshi/db/
logpath=/etc/mongodbceshi/log/mongoceshi.log
pidfilepath=/etc/mongodbceshi/db/mongo.pid
logappend=true
journal=true
port=30000
replSet=rsceshi
bind_ip = 0.0.0.0
fork=true


[root@ceshi-01 /etc/mongodbceshi/conf]# pwd
/etc/mongodbceshi/conf
[root@ceshi-01 /etc/mongodbceshi/conf]#
[root@ceshi-01 /etc/mongodbceshi/conf]# cat mongodb.conf
dbpath=/etc/mongodbceshi/db/
logpath=/etc/mongodbceshi/log/mongoceshi.log
pidfilepath=/etc/mongodbceshi/db/mongo.pid
logappend=true
journal=true
port=30000
replSet=rsceshi
bind_ip = 0.0.0.0
fork=true

4 .在三臺機器上分別啟動例項

/usr/local/mongodb/bin/mongod -f /etc/mongodbceshi/conf/mongodb-test.conf


5. 在primary 機器上 連線mongo ,執行 rs.initiate() 初始化複製集

注意:在哪一臺機器上執行初始化命令,哪一臺就成為了主庫,只執行一次初始化,剩下的只能是從庫。


[root@ceshi-03 /etc/mongodbceshi/conf]# /usr/local/mongodb/bin/mongo --port 30000 #連線mongodb
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:30000/test
Server has startup warnings:
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 102400 processes, 1024000 files. Number of processes should be at least 512000 : 0.5 times number of files.
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
> rs.status();
{
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94
}
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "ceshi-03:30000",
"ok" : 1
}
rsceshi:OTHER> rs.status();
{
"set" : "rsceshi",
"date" : ISODate("2021-07-05T08:32:58.275Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "ceshi-03:30000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 458,
"optime" : Timestamp(1625473972, 1),
"optimeDate" : ISODate("2021-07-05T08:32:52Z"),
"electionTime" : Timestamp(1625473972, 2),
"electionDate" : ISODate("2021-07-05T08:32:52Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}
rsceshi:PRIMARY> rs.conf()
{
"_id" : "rsceshi",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "ceshi-03:30000",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {

},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
rsceshi:PRIMARY>

6. 在主庫上執行rs.add("localhost:28018") 新增另外一臺secondary


rsceshi:PRIMARY> rs.add("192.168.1.3:30000")

rsceshi:PRIMARY> rs.add("192.168.1.3:30000")
{ "ok" : 1 }
rsceshi:PRIMARY>
rsceshi:PRIMARY> rs.status();
{
"set" : "rsceshi",
"date" : ISODate("2021-07-05T08:38:23.934Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "ceshi-03:30000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 783,
"optime" : Timestamp(1625474298, 1),
"optimeDate" : ISODate("2021-07-05T08:38:18Z"),
"electionTime" : Timestamp(1625473972, 2),
"electionDate" : ISODate("2021-07-05T08:32:52Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.1.3:30000",
"health" : 1,
"state" : 5,
"stateStr" : "STARTUP2",
"uptime" : 5,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2021-07-05T08:38:22.491Z"),
"lastHeartbeatRecv" : ISODate("2021-07-05T08:38:22.496Z"),
"pingMs" : 0,
"configVersion" : 2
}
],
"ok" : 1
}
rsceshi:PRIMARY> rs.status();
{
"set" : "rsceshi",
"date" : ISODate("2021-07-05T08:38:58.691Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "ceshi-03:30000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 818,
"optime" : Timestamp(1625474298, 1),
"optimeDate" : ISODate("2021-07-05T08:38:18Z"),
"electionTime" : Timestamp(1625473972, 2),
"electionDate" : ISODate("2021-07-05T08:32:52Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.1.3:30000",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 40,
"optime" : Timestamp(1625474298, 1),
"optimeDate" : ISODate("2021-07-05T08:38:18Z"),
"lastHeartbeat" : ISODate("2021-07-05T08:38:58.511Z"),
"lastHeartbeatRecv" : ISODate("2021-07-05T08:38:58.514Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "could not find member to sync from",
"configVersion" : 2
}
],
"ok" : 1
}
rsceshi:PRIMARY>


7 . 新增仲裁節點

rsceshi:PRIMARY> rs.addArb("192.168.1.2:30000")
{ "ok" : 1 }
rsceshi:PRIMARY>
rsceshi:PRIMARY> rs.status();
{
"set" : "rsceshi",
"date" : ISODate("2021-07-05T08:40:41.494Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "ceshi-03:30000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 921,
"optime" : Timestamp(1625474438, 1),
"optimeDate" : ISODate("2021-07-05T08:40:38Z"),
"electionTime" : Timestamp(1625473972, 2),
"electionDate" : ISODate("2021-07-05T08:32:52Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.1.3:30000",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 143,
"optime" : Timestamp(1625474438, 1),
"optimeDate" : ISODate("2021-07-05T08:40:38Z"),
"lastHeartbeat" : ISODate("2021-07-05T08:40:40.121Z"),
"lastHeartbeatRecv" : ISODate("2021-07-05T08:40:40.590Z"),
"pingMs" : 0,
"syncingTo" : "ceshi-03:30000",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "192.168.1.2:30000",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 3,
"lastHeartbeat" : ISODate("2021-07-05T08:40:40.122Z"),
"lastHeartbeatRecv" : ISODate("2021-07-05T08:40:40.133Z"),
"pingMs" : 0,
"configVersion" : 3
}
],
"ok" : 1
}
rsceshi:PRIMARY>


[root@ceshi-03 /etc/mongodbceshi/db]# /usr/local/mongodb/bin/mongo --port 30000
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:30000/test
Server has startup warnings:
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 102400 processes, 1024000 files. Number of processes should be at least 512000 : 0.5 times number of files.
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
rsceshi:PRIMARY>


[root@ceshi-01 /etc/mongodbceshi/conf]# /usr/local/mongodb/bin/mongo --port 30000
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:30000/test
Server has startup warnings:
2021-07-05T16:25:55.019+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-07-05T16:25:55.019+0800 I CONTROL [initandlisten]
2021-07-05T16:25:55.019+0800 I CONTROL [initandlisten]
2021-07-05T16:25:55.019+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-07-05T16:25:55.019+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-07-05T16:25:55.019+0800 I CONTROL [initandlisten]
2021-07-05T16:25:55.019+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-07-05T16:25:55.019+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-07-05T16:25:55.019+0800 I CONTROL [initandlisten]
rsceshi:SECONDARY>

[root@ceshi-02 conf]# /usr/local/mongodb/bin/mongo --port 30000
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:30000/test
Server has startup warnings:
2021-07-05T16:26:11.944+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-07-05T16:26:11.944+0800 I CONTROL [initandlisten]
2021-07-05T16:26:11.945+0800 I CONTROL [initandlisten]
2021-07-05T16:26:11.945+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-07-05T16:26:11.945+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-07-05T16:26:11.945+0800 I CONTROL [initandlisten]
2021-07-05T16:26:11.945+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-07-05T16:26:11.945+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-07-05T16:26:11.945+0800 I CONTROL [initandlisten]
rsceshi:ARBITER>
rsceshi:ARBITER>


8 . 測試結果 向主庫寫入一條資料,然後在從庫上執行 rs.slaveOk(true)開啟從節點讀許可權 ,在從庫看看

rsceshi:PRIMARY> use testdb2
switched to db testdb2
rsceshi:PRIMARY> show tables ;
system.indexes
testdb2
rsceshi:PRIMARY> db.createCollection("ceshi-jihe");
{ "ok" : 1 }
rsceshi:PRIMARY> show tables ;
ceshi-jihe
system.indexes
testdb2
rsceshi:PRIMARY> db.testdb2.find();
{ "_id" : ObjectId("60e2c937f5aa2b4ebdf62708"), "name" : "hello2" }
rsceshi:PRIMARY>

rsceshi:SECONDARY> rs.slaveOk(true)
rsceshi:SECONDARY>
rsceshi:SECONDARY> show dbs ;
local 2.077GB
test 0.078GB
testdb 0.078GB
testdb2 0.078GB
rsceshi:SECONDARY>
rsceshi:SECONDARY> use testdb2
switched to db testdb2
rsceshi:SECONDARY> db.testdb2.find();
{ "_id" : ObjectId("60e2c937f5aa2b4ebdf62708"), "name" : "hello2" }
rsceshi:SECONDARY>
rsceshi:SECONDARY>
rsceshi:SECONDARY> show tables ;
ceshi-jihe
system.indexes
testdb2
rsceshi:SECONDARY>
rsceshi:SECONDARY>


9 .增加節點 就在主庫上執行rs.add("IP:30000"),減少節點就在主庫上執行: rs.remove("IP:30000")

10. 也可以建立內部驗證keyFile


1、用ssl生成key
openssl rand -base64 756 > /usr/local/mongodb/data/key/mongokey


2、設定只讀許可權,否則可能導致啟動失敗
chmod 400 /usr/local/mongodb/data/key/mongokey


3、只需生成一次keyFile檔案,然後拷到對應的其他機器上,保證機器都用的同一份keyFile,許可權都是隻讀

建立好之後可以寫在配置檔案中,例如:

dbpath=/etc/mongodbceshi/db/
logpath=/etc/mongodbceshi/log/mongoceshi.log
pidfilepath=/etc/mongodbceshi/db/mongo.pid
logappend=true
journal=true
port=30000
replSet=rsceshi
bind_ip = 0.0.0.0
fork=true

#auth=true #需要認證。如果放開註釋,就必須建立MongoDB的賬號,使用賬號與密碼才可遠端訪問,第一次安裝建議註釋

keyFile=/usr/local/mongodb/data/key/mongokey

11. 在初始化階段,也可以這樣執行,在任意一臺機器上執行初始化,利用引數指定哪一臺是主,哪一臺是從

rs.initiate({
_id:"rsceshi",
members:[
{_id:0, host:"192.168.1.4:30000", priority:2},
{_id:1, host:"192.168.1.3:30000", priority:1},
{_id:2, host:"192.168.1.2:30000", arbiterOnly:true}
]
});

12. 主節點 執行db.printReplicationInfo()查詢oplog的大小及儲存的操作記錄持續的時長 。

rsceshi:PRIMARY> db.printReplicationInfo()
configured oplog size: 990MB
log length start to end: 1656secs (0.46hrs)
oplog first event time: Mon Jul 05 2021 16:32:52 GMT+0800 (CST)
oplog last event time: Mon Jul 05 2021 17:00:28 GMT+0800 (CST)
now: Mon Jul 05 2021 18:47:34 GMT+0800 (CST)

從節點執行db.printSlaveReplicationInfo() 檢視

rsceshi:SECONDARY> db.printSlaveReplicationInfo()
source: 192.168.1.4:30000
syncedTo: Mon Jul 05 2021 17:00:28 GMT+0800 (CST)
0 secs (0 hrs) behind the primary

13 .在主節點執行關閉命令檢視

[root@ceshi-03 /etc/mongodbceshi/db]# /usr/local/mongodb/bin/mongo --port 30000
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:30000/test
Server has startup warnings:
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 102400 processes, 1024000 files. Number of processes should be at least 512000 : 0.5 times number of files.
2021-07-05T16:26:13.039+0800 I CONTROL [initandlisten]
rsceshi:PRIMARY> show dbs
local 1.078GB
test 0.078GB
testdb 0.078GB
testdb2 0.078GB
rsceshi:PRIMARY> use admin;
switched to db admin
rsceshi:PRIMARY> db.shutdownServer(); #也可以執行 /usr/local/mongodb/bin/mongod --shutdown --dbpath /etc/mongodbceshi/db/
2021-07-05T19:01:47.545+0800 I NETWORK DBClientCursor::init call() failed
server should be down...
2021-07-05T19:01:47.549+0800 I NETWORK trying reconnect to 127.0.0.1:30000 (127.0.0.1) failed
2021-07-05T19:01:47.550+0800 I NETWORK reconnect 127.0.0.1:30000 (127.0.0.1) ok
2021-07-05T19:01:48.145+0800 I NETWORK Socket recv() errno:104 Connection reset by peer 127.0.0.1:30000
2021-07-05T19:01:48.145+0800 I NETWORK SocketException: remote: 127.0.0.1:30000 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:30000]
2021-07-05T19:01:48.145+0800 I NETWORK DBClientCursor::init call() failed
2021-07-05T19:01:48.147+0800 I NETWORK trying reconnect to 127.0.0.1:30000 (127.0.0.1) failed
2021-07-05T19:01:48.147+0800 W NETWORK Failed to connect to 127.0.0.1:30000, reason: errno:111 Connection refused
2021-07-05T19:01:48.147+0800 I NETWORK reconnect 127.0.0.1:30000 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:30000 (127.0.0.1), connection attempt failed
> exit
bye
[root@ceshi-03 /etc/mongodbceshi/db]# /usr/local/mongodb/bin/mongo --port 30000
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:30000/test
2021-07-05T19:02:05.584+0800 W NETWORK Failed to connect to 127.0.0.1:30000, reason: errno:111 Connection refused
2021-07-05T19:02:05.585+0800 E QUERY Error: couldn't connect to server 127.0.0.1:30000 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
[root@ceshi-03 /etc/mongodbceshi/db]#

#再啟動看看
[root@ceshi-03 /etc/mongodbceshi/db]# /usr/local/mongodb/bin/mongod -f /etc/mongodbceshi/conf/mongodb-test.conf
warning: bind_ip of 0.0.0.0 is unnecessary; listens on all ips by default
about to fork child process, waiting until server is ready for connections.
forked process: 1932
child process started successfully, parent exiting
[root@ceshi-03 /etc/mongodbceshi/db]#

#再連上去看看已經變成從庫了
[root@ceshi-03 /etc/mongodbceshi/db]# /usr/local/mongodb/bin/mongo --port 30000
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:30000/test
Server has startup warnings:
2021-07-05T19:02:54.733+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-07-05T19:02:54.733+0800 I CONTROL [initandlisten]
2021-07-05T19:02:54.733+0800 I CONTROL [initandlisten]
2021-07-05T19:02:54.733+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-07-05T19:02:54.733+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-07-05T19:02:54.733+0800 I CONTROL [initandlisten]
2021-07-05T19:02:54.733+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-07-05T19:02:54.734+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-07-05T19:02:54.734+0800 I CONTROL [initandlisten]
2021-07-05T19:02:54.734+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 102400 processes, 1024000 files. Number of processes should be at least 512000 : 0.5 times number of files.
2021-07-05T19:02:54.734+0800 I CONTROL [initandlisten]
rsceshi:SECONDARY>

#當原先的主庫關閉之後,在原先的從庫上看看

rsceshi:SECONDARY> rs.status();
{
"set" : "rsceshi",
"date" : ISODate("2021-07-05T11:02:14.895Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "ceshi-03:30000",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2021-07-05T11:02:14.172Z"),
"lastHeartbeatRecv" : ISODate("2021-07-05T11:01:45.591Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Failed attempt to connect to ceshi-03:30000; couldn't connect to server ceshi-03:30000 (192.168.1.4), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "192.168.1.3:30000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 9432,
"optime" : Timestamp(1625475628, 1),
"optimeDate" : ISODate("2021-07-05T09:00:28Z"),
"electionTime" : Timestamp(1625482908, 1),
"electionDate" : ISODate("2021-07-05T11:01:48Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.1.2:30000",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 8496,
"lastHeartbeat" : ISODate("2021-07-05T11:02:13.448Z"),
"lastHeartbeatRecv" : ISODate("2021-07-05T11:02:13.243Z"),
"pingMs" : 0,
"configVersion" : 3
}
],
"ok" : 1
}
rsceshi:PRIMARY>
rsceshi:PRIMARY>

#當原先的主庫重新啟動起來之後,再看看複製集狀態


rsceshi:PRIMARY> rs.status();
{
"set" : "rsceshi",
"date" : ISODate("2021-07-05T11:02:38.165Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "ceshi-03:30000",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2021-07-05T11:02:36.204Z"),
"lastHeartbeatRecv" : ISODate("2021-07-05T11:01:45.591Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Failed attempt to connect to ceshi-03:30000; couldn't connect to server ceshi-03:30000 (192.168.1.4), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "192.168.1.3:30000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 9456,
"optime" : Timestamp(1625475628, 1),
"optimeDate" : ISODate("2021-07-05T09:00:28Z"),
"electionTime" : Timestamp(1625482908, 1),
"electionDate" : ISODate("2021-07-05T11:01:48Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.1.2:30000",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 8519,
"lastHeartbeat" : ISODate("2021-07-05T11:02:37.460Z"),
"lastHeartbeatRecv" : ISODate("2021-07-05T11:02:37.256Z"),
"pingMs" : 0,
"configVersion" : 3
}
],
"ok" : 1
}
rsceshi:PRIMARY> rs.status();
{
"set" : "rsceshi",
"date" : ISODate("2021-07-05T11:03:06.430Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "ceshi-03:30000",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 10,
"optime" : Timestamp(1625475628, 1),
"optimeDate" : ISODate("2021-07-05T09:00:28Z"),
"lastHeartbeat" : ISODate("2021-07-05T11:03:06.237Z"),
"lastHeartbeatRecv" : ISODate("2021-07-05T11:03:04.748Z"),
"pingMs" : 0,
"configVersion" : 3
},
{
"_id" : 1,
"name" : "192.168.1.3:30000",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 9484,
"optime" : Timestamp(1625475628, 1),
"optimeDate" : ISODate("2021-07-05T09:00:28Z"),
"electionTime" : Timestamp(1625482908, 1),
"electionDate" : ISODate("2021-07-05T11:01:48Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.1.2:30000",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 8547,
"lastHeartbeat" : ISODate("2021-07-05T11:03:05.476Z"),
"lastHeartbeatRecv" : ISODate("2021-07-05T11:03:05.273Z"),
"pingMs" : 0,
"configVersion" : 3
}
],
"ok" : 1
}
rsceshi:PRIMARY>