1.开启三个节点
mongod --dbpath /mongodb/db1 --port 27017 --bind_ip 192.168.75.56 -replSet myrelset/192.168.75.56:27017 &
mongod --dbpath /mongodb/db2 --port 27018 --bind_ip 192.168.75.56 -replSet myrelset/192.168.75.56:27018 &
mongod --dbpath /mongodb/db3 --port 27019 --bind_ip 192.168.75.56 -replSet myrelset/192.168.75.56:27019 &
2.查看进程
--2.查看进程
mongod 22567 3684 1 14:38 pts/1 00:00:02 mongod --dbpath /mongodb/db1 --port 27017 --bind_ip 192.168.75.56 -replSet myrelset/192.168.75.56:27017
mongod 22655 3684 1 14:39 pts/1 00:00:01 mongod --dbpath /mongodb/db2 --port 27018 --bind_ip 192.168.75.56 -replSet myrelset/192.168.75.56:27018
mongod 22794 3889 1 14:40 pts/2 00:00:00 mongod --dbpath /mongodb/db3 --port 27019 --bind_ip 192.168.75.56 -replSet myrelset/192.168.75.56:27019
3.初始化副本集
mongo 192.168.75.56 --port 27017
sql>use admin
sql>cfg={_id:'myrelset',members:[{_id:0,host:'192.168.75.56:27017'},{_id:1,host:'192.168.75.56:27018'},{_id:2,host:'192.168.75.56:27019',priority:0}]}
> cfg={_id:'myrelset',members:[{_id:0,host:'192.168.75.56:27017'},{_id:1,host:'192.168.75.56:27018'},{_id:2,host:'192.168.75.56:27019',priority:0}]}
{
"_id" : "myrelset",
"members" : [
{
"_id" : 0,
"host" : "192.168.75.56:27017"
},
{
"_id" : 1,
"host" : "192.168.75.56:27018"
},
{
"_id" : 2,
"host" : "192.168.75.56:27019",
"priority" : 0
}
]
}
sql>rs.initiate(cfg) --初始化。
{
"ok" : 1,
"operationTime" : Timestamp(1633606554, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1633606554, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
4.集群状态查看
rs.status()
myrelset:SECONDARY> rs.status()
{
"set" : "myrelset",
"date" : ISODate("2024-06-14T06:46:36.431Z"),
"myState" : 2,
"term" : NumberLong(2),
"syncingTo" : "192.168.75.56:27018",
"syncSourceHost" : "192.168.75.56:27018",
"syncSourceId" : 1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1718347593, 1),
"t" : NumberLong(2)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1718347593, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1718347593, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1718347593, 1),
"t" : NumberLong(2)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1718347583, 1),
"members" : [
{
"_id" : 0,
"name" : "192.168.75.56:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 71,
"optime" : {
"ts" : Timestamp(1718347593, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2024-06-14T06:46:33Z"),
"syncingTo" : "192.168.75.56:27018",
"syncSourceHost" : "192.168.75.56:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.75.56:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 70,
"optime" : {
"ts" : Timestamp(1718347593, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1718347593, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2024-06-14T06:46:33Z"),
"optimeDurableDate" : ISODate("2024-06-14T06:46:33Z"),
"lastHeartbeat" : ISODate("2024-06-14T06:46:35.422Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T06:46:35.035Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1718347432, 1),
"electionDate" : ISODate("2024-06-14T06:43:52Z"),
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.75.56:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 70,
"optime" : {
"ts" : Timestamp(1718347593, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1718347593, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2024-06-14T06:46:33Z"),
"optimeDurableDate" : ISODate("2024-06-14T06:46:33Z"),
"lastHeartbeat" : ISODate("2024-06-14T06:46:35.421Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T06:46:35.117Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.75.56:27018",
"syncSourceHost" : "192.168.75.56:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1718347593, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1718347593, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
当前:
192.168.75.56:27017 Secondary角色
192.168.75.56:27018 Primary角色
192.168.75.56:27019 Secondary角色
且监控状态都是1;
5.使用各个端口登录检查自己是否master;
myrelset:SECONDARY> db.isMaster()
{
"hosts" : [
"192.168.75.56:27017",
"192.168.75.56:27018"
],
"passives" : [
"192.168.75.56:27019"
],
"setName" : "myrelset",
"setVersion" : 1,
"ismaster" : false,
"secondary" : true,
"primary" : "192.168.75.56:27018",
"me" : "192.168.75.56:27017",
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1718347963, 1),
"t" : NumberLong(2)
},
"lastWriteDate" : ISODate("2024-06-14T06:52:43Z"),
"majorityOpTime" : {
"ts" : Timestamp(1718347963, 1),
"t" : NumberLong(2)
},
"majorityWriteDate" : ISODate("2024-06-14T06:52:43Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2024-06-14T06:52:51.850Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 7,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1718347963, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1718347963, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
主节点是27018,自己是27017
--现在使用27018登录。
myrelset:PRIMARY> rs.isMaster()
{
"hosts" : [
"192.168.75.56:27017",
"192.168.75.56:27018"
],
"passives" : [
"192.168.75.56:27019"
],
"setName" : "myrelset",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "192.168.75.56:27018",
"me" : "192.168.75.56:27018",
"electionId" : ObjectId("7fffffff0000000000000002"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1718348061, 1),
"t" : NumberLong(2)
},
"lastWriteDate" : ISODate("2024-06-14T06:54:21Z"),
"majorityOpTime" : {
"ts" : Timestamp(1718348061, 1),
"t" : NumberLong(2)
},
"majorityWriteDate" : ISODate("2024-06-14T06:54:21Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2024-06-14T06:54:33.569Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 7,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1718348061, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1718348061, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
--发现自己是主节点。
--现在使用27019登录。
--自己是从节点。
myrelset:SECONDARY> rs.isMaster()
{
"hosts" : [
"192.168.75.56:27017",
"192.168.75.56:27018"
],
"passives" : [
"192.168.75.56:27019"
],
"setName" : "myrelset",
"setVersion" : 1,
"ismaster" : false,
"secondary" : true,
"primary" : "192.168.75.56:27018",
"passive" : true,
"me" : "192.168.75.56:27019",
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1718348113, 1),
"t" : NumberLong(2)
},
"lastWriteDate" : ISODate("2024-06-14T06:55:13Z"),
"majorityOpTime" : {
"ts" : Timestamp(1718348113, 1),
"t" : NumberLong(2)
},
"majorityWriteDate" : ISODate("2024-06-14T06:55:13Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2024-06-14T06:55:19.452Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 7,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1718348113, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1718348113, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
复本集的名称myreplset 是自定义的。
6.关闭主节点观察主从切换情况
当前主节点是:27018,关闭主节点。
myrelset:PRIMARY> use admin
switched to db admin
myrelset:PRIMARY> db.shutdownServer()
2024-06-14T15:02:44.182+0800 E QUERY [js] Error: shutdownServer failed: {
"operationTime" : Timestamp(1718348563, 1),
"ok" : 0,
"errmsg" : "shutdown must run from localhost when running db without auth",
"code" : 13,
"codeName" : "Unauthorized",
"$clusterTime" : {
"clusterTime" : Timestamp(1718348563, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.shutdownServer@src/mongo/shell/db.js:453:1
@(shell):1:1
关闭服务器必须是本地节点。所以现在关闭重启,并写上本地节点启动。
kill -9 xxx 杀掉集群
重启
mongod --dbpath /mongodb/db1 --port 27017 --bind_ip 192.168.75.56,127.0.0.1 -replSet testset/192.168.75.56:27017 &
mongod --dbpath /mongodb/db2 --port 27018 --bind_ip 192.168.75.56,127.0.0.1 -replSet testset/192.168.75.56:27018 &
mongod --dbpath /mongodb/db3 --port 27019 --bind_ip 192.168.75.56,127.0.0.1 -replSet testset/192.168.75.56:27019 &
关闭主节点。
当前主节点:27017,从节点是:27018,27019;
[mongod@mysql57 mongodb]$ mongo --host 127.0.0.1 --port 27017
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 4.0.2
Server has startup warnings:
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten]
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten]
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten]
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten]
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten]
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 524288 files. Number of processes should be at least 262144 : 0.5 times number of files.
2024-06-14T15:59:22.683+0800 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
myrelset:PRIMARY> use admin
switched to db admin
myrelset:PRIMARY> db.shutdownServer()
server should be down...
2024-06-14T16:02:51.805+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2024-06-14T16:02:51.980+0800 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
2024-06-14T16:02:51.983+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2024-06-14T16:02:51.983+0800 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
此时可以看到:27018,变成主节点,27019变成从节点。27017节点状态不健康。
myrelset:PRIMARY> rs.status()
{
"set" : "myrelset",
"date" : ISODate("2024-06-14T08:03:26.630Z"),
"myState" : 1,
"term" : NumberLong(4),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1718352202, 1),
"t" : NumberLong(4)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1718352202, 1),
"t" : NumberLong(4)
},
"appliedOpTime" : {
"ts" : Timestamp(1718352202, 1),
"t" : NumberLong(4)
},
"durableOpTime" : {
"ts" : Timestamp(1718352202, 1),
"t" : NumberLong(4)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1718352172, 1),
"members" : [
{
"_id" : 0,
"name" : "192.168.75.56: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("2024-06-14T08:03:26.037Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:02:51.111Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Error connecting to 192.168.75.56:27017 :: caused by :: Connection refused",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "192.168.75.56:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 209,
"optime" : {
"ts" : Timestamp(1718352202, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2024-06-14T08:03:22Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1718352171, 1),
"electionDate" : ISODate("2024-06-14T08:02:51Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.75.56:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 166,
"optime" : {
"ts" : Timestamp(1718352202, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1718352202, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2024-06-14T08:03:22Z"),
"optimeDurableDate" : ISODate("2024-06-14T08:03:22Z"),
"lastHeartbeat" : ISODate("2024-06-14T08:03:25.827Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:03:25.816Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.75.56:27018",
"syncSourceHost" : "192.168.75.56:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1718352202, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1718352202, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
7.从新启动27017,看看什么情况。
myrelset:SECONDARY> rs.status()
{
"set" : "myrelset",
"date" : ISODate("2024-06-14T08:07:30.052Z"),
"myState" : 2,
"term" : NumberLong(4),
"syncingTo" : "192.168.75.56:27019",
"syncSourceHost" : "192.168.75.56:27019",
"syncSourceId" : 2,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1718352442, 1),
"t" : NumberLong(4)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1718352442, 1),
"t" : NumberLong(4)
},
"appliedOpTime" : {
"ts" : Timestamp(1718352442, 1),
"t" : NumberLong(4)
},
"durableOpTime" : {
"ts" : Timestamp(1718352442, 1),
"t" : NumberLong(4)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1718352442, 1),
"members" : [
{
"_id" : 0,
"name" : "192.168.75.56:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 124,
"optime" : {
"ts" : Timestamp(1718352442, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2024-06-14T08:07:22Z"),
"syncingTo" : "192.168.75.56:27019",
"syncSourceHost" : "192.168.75.56:27019",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.75.56:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 123,
"optime" : {
"ts" : Timestamp(1718352442, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1718352442, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2024-06-14T08:07:22Z"),
"optimeDurableDate" : ISODate("2024-06-14T08:07:22Z"),
"lastHeartbeat" : ISODate("2024-06-14T08:07:29.800Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:07:28.363Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1718352171, 1),
"electionDate" : ISODate("2024-06-14T08:02:51Z"),
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.75.56:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 123,
"optime" : {
"ts" : Timestamp(1718352442, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1718352442, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2024-06-14T08:07:22Z"),
"optimeDurableDate" : ISODate("2024-06-14T08:07:22Z"),
"lastHeartbeat" : ISODate("2024-06-14T08:07:29.800Z"),
"lastHeartbeatRecv" : ISODate("2024-06-14T08:07:28.273Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.75.56:27018",
"syncSourceHost" : "192.168.75.56:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1718352442, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1718352442, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
原来的主节点重启后变成了从节点。由此可见mongodb的副本集还是非常强大的。维护简单。
8.数据库查看
myrelset:PRIMARY> show tables;
myrelset:PRIMARY> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
myrelset:PRIMARY> use admin
switched to db admin
myrelset:PRIMARY> show tables;
myrelset:PRIMARY> show collections;
myrelset:PRIMARY> use config
switched to db config
myrelset:PRIMARY> show tables;
transactions
myrelset:PRIMARY> show collections;
transactions
myrelset:PRIMARY> use local
switched to db local
myrelset:PRIMARY> show tables
oplog.rs
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
myrelset:PRIMARY> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB