本文介绍如何配置MongoDB的Replica Set服务,并介绍了如何做主从切换以及添加新节点。此环境都在一台服务器上完成。
1、配置MongoDB Replica Set
1.1 创建配置文件
[root@hdp05 ~]# mkdir -p /data/rep{1,2}
[root@hdp05 ~]# chown mongod:mongod /data/rep*
[root@hdp05 ~]# vi /data/rep1/mongod.rep1
net:
bindIp: 0.0.0.0
port: 27019
processManagement:
fork: "true"
replication:
replSetName: TestRepSet
storage:
dbPath: /data/rep1
systemLog:
destination: file
path: /data/rep1/mongod.log
[root@hdp05 ~]# vi /data/rep2/mongod.rep2
net:
bindIp: 0.0.0.0
port: 27020
processManagement:
fork: "true"
replication:
replSetName: TestRepSet
storage:
dbPath: /data/rep2
systemLog:
destination: file
path: /data/rep2/mongod.log
1.2 启动MongoDB服务
[root@hdp05 ~]# mongod -f /data/rep1/mongod.rep1
[root@hdp05 ~]# mongod -f /data/rep2/mongod.rep2
1.3 初始化Replica Set
[root@hdp05 ~]# mongo --port 27019
> rs.initiate( {_id : "TestRepSet",members: [{ _id: 0, host: "hdp05.thinkjoy.tt:27019" },{ _id: 1, host: "hdp05.thinkjoy.tt:27020" }]})
{
"ok" : 1,
"operationTime" : Timestamp(1534909723, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1534909723, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
--检查配置信息
TestRepSet:SECONDARY> rs.conf()
{
"_id" : "TestRepSet",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "hdp05.thinkjoy.tt:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "hdp05.thinkjoy.tt:27020",
"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("5b7cdd1ba00297bd3f560eda")
}
}
--确认primary节点
TestRepSet:PRIMARY> db.isMaster()
{
"hosts" : [
"hdp05.thinkjoy.tt:27019",
"hdp05.thinkjoy.tt:27020"
],
"setName" : "TestRepSet",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "hdp05.thinkjoy.tt:27019",
"me" : "hdp05.thinkjoy.tt:27019",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1534988860, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2018-08-23T01:47:40Z"),
"majorityOpTime" : {
"ts" : Timestamp(1534988860, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2018-08-23T01:47:40Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2018-08-23T01:47:49.713Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 7,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1534988860, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1534988860, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
到此,整个Replica Set配置完成。
2、添加新的secondary节点
2.1 创建配置文件以及数据目录
[root@hdp05 ~]# mkdir /data/rep3;chown mongod:mongod /data/rep3
[root@hdp05 ~]# vi /data/rep3/mongod.rep3
net:
bindIp: 0.0.0.0
port: 27021
processManagement:
fork: "true"
replication:
replSetName: TestRepSet
storage:
dbPath: /data/rep3
systemLog:
destination: file
path: /data/rep3/mongod.log
2.2 启动MongoDB服务
[root@hdp05 ~]# mongod -f /data/rep3/mongod.rep3
2.3 向Replica Set添加节点
使用mongo连接到主节点,如果不知道主节点是哪个,则连接到任意一个几点,使用db.isMaster()或者rs.status()确认那个是主节点。
[root@hdp05 ~]# mongo --port 27019
TestRepSet:PRIMARY> db.isMaster()
--添加节点
TestRepSet:PRIMARY> rs.add( { host: "hdp05.thinkjoy.tt:27021", priority: 0, votes: 0 } )
{
"ok" : 1,
"operationTime" : Timestamp(1534989374, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1534989374, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
TestRepSet:PRIMARY> rs.status()
{
"set" : "TestRepSet",
"date" : ISODate("2018-08-23T01:56:47.591Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1534989400, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1534989400, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1534989400, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1534989400, 1),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1534989390, 1),
"members" : [
{
"_id" : 0,
"name" : "hdp05.thinkjoy.tt:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 79787,
"optime" : {
"ts" : Timestamp(1534989400, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-08-23T01:56:40Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1534909735, 1),
"electionDate" : ISODate("2018-08-22T03:48:55Z"),
"configVersion" : 2,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "hdp05.thinkjoy.tt:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 79683,
"optime" : {
"ts" : Timestamp(1534989400, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1534989400, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-08-23T01:56:40Z"),
"optimeDurableDate" : ISODate("2018-08-23T01:56:40Z"),
"lastHeartbeat" : ISODate("2018-08-23T01:56:46.530Z"),
"lastHeartbeatRecv" : ISODate("2018-08-23T01:56:47.554Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "hdp05.thinkjoy.tt:27019",
"syncSourceHost" : "hdp05.thinkjoy.tt:27019",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 2
},
{
"_id" : 2,
"name" : "hdp05.thinkjoy.tt:27021",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 33,
"optime" : {
"ts" : Timestamp(1534989400, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1534989400, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-08-23T01:56:40Z"),
"optimeDurableDate" : ISODate("2018-08-23T01:56:40Z"),
"lastHeartbeat" : ISODate("2018-08-23T01:56:46.530Z"),
"lastHeartbeatRecv" : ISODate("2018-08-23T01:56:45.716Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "hdp05.thinkjoy.tt:27019",
"syncSourceHost" : "hdp05.thinkjoy.tt:27019",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 2
}
],
"ok" : 1,
"operationTime" : Timestamp(1534989400, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1534989400, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
--设置每个节点的优先级
TestRepSet:PRIMARY> var cfg = rs.conf();
TestRepSet:PRIMARY> cfg.members[2].priority = 1
TestRepSet:PRIMARY> cfg.members[2].votes = 1
TestRepSet:PRIMARY> rs.reconfig(cfg)
{
"ok" : 1,
"operationTime" : Timestamp(1534989596, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1534989596, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
2.4 主从节点连接信息验证
TestRepSet:PRIMARY> db.printReplicationInfo()
configured oplog size: 2048MB
log length start to end: 4390secs (1.22hrs)
oplog first event time: Thu Aug 23 2018 16:03:24 GMT+0800 (CST)
oplog last event time: Thu Aug 23 2018 17:16:34 GMT+0800 (CST)
now: Thu Aug 23 2018 17:16:37 GMT+0800 (CST)
TestRepSet:PRIMARY>
TestRepSet:SECONDARY> rs.printSlaveReplicationInfo()
source: hdp05.thinkjoy.tt:27020
syncedTo: Thu Aug 23 2018 17:17:14 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: hdp05.thinkjoy.tt:27021
syncedTo: Thu Aug 23 2018 17:17:14 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
TestRepSet:SECONDARY>
3、Replica Set主从切换
官方提供的切换方式有两种:一种是通过设置优先级进行切换,另外一种是通过MongoDB的命令进行切换。
3.1 设置优先级切换
当前三个节点的优先级都是1,而27019是primary节点,通过降低27019和27020端口对应节点的优先级,使27021对应的节点成为primary节点。
[root@hdp05 ~]# mongo --port 27019
--切换前的主从信息
TestRepSet:PRIMARY> db.isMaster()
{
"hosts" : [
"hdp05.thinkjoy.tt:27019",
"hdp05.thinkjoy.tt:27020",
"hdp05.thinkjoy.tt:27021"
],
"setName" : "TestRepSet",
"setVersion" : 6,
"ismaster" : true,
"secondary" : false,
"primary" : "hdp05.thinkjoy.tt:27019",
"me" : "hdp05.thinkjoy.tt:27019",
"electionId" : ObjectId("7fffffff0000000000000009"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1535009522, 1),
"t" : NumberLong(9)
},
"lastWriteDate" : ISODate("2018-08-23T07:32:02Z"),
"majorityOpTime" : {
"ts" : Timestamp(1535009522, 1),
"t" : NumberLong(9)
},
"majorityWriteDate" : ISODate("2018-08-23T07:32:02Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2018-08-23T07:32:06.129Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 7,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1535009522, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1535009522, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
--执行切换
TestRepSet:PRIMARY> cfg = rs.conf()
TestRepSet:PRIMARY> cfg.members[0].priority = 0.5
TestRepSet:PRIMARY> cfg.members[1].priority = 0.5
TestRepSet:PRIMARY> cfg.members[2].priority = 1
TestRepSet:PRIMARY> rs.reconfig(cfg)
--切换后的主从信息
[root@hdp05 ~]# mongo --port 27021
TestRepSet:PRIMARY> rs.status()
{
"set" : "TestRepSet",
"date" : ISODate("2018-08-23T03:30:47.189Z"),
"myState" : 1,
"term" : NumberLong(2),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1534995039, 1),
"t" : NumberLong(2)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1534995039, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1534995039, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1534995039, 1),
"t" : NumberLong(2)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1534995029, 1),
"members" : [
{
"_id" : 0,
"name" : "hdp05.thinkjoy.tt:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5672,
"optime" : {
"ts" : Timestamp(1534995039, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1534995039, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2018-08-23T03:30:39Z"),
"optimeDurableDate" : ISODate("2018-08-23T03:30:39Z"),
"lastHeartbeat" : ISODate("2018-08-23T03:30:47.048Z"),
"lastHeartbeatRecv" : ISODate("2018-08-23T03:30:46.990Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "hdp05.thinkjoy.tt:27021",
"syncSourceHost" : "hdp05.thinkjoy.tt:27021",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 5
},
{
"_id" : 1,
"name" : "hdp05.thinkjoy.tt:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5672,
"optime" : {
"ts" : Timestamp(1534995039, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1534995039, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2018-08-23T03:30:39Z"),
"optimeDurableDate" : ISODate("2018-08-23T03:30:39Z"),
"lastHeartbeat" : ISODate("2018-08-23T03:30:46.991Z"),
"lastHeartbeatRecv" : ISODate("2018-08-23T03:30:46.991Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "hdp05.thinkjoy.tt:27021",
"syncSourceHost" : "hdp05.thinkjoy.tt:27021",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 5
},
{
"_id" : 2,
"name" : "hdp05.thinkjoy.tt:27021",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 5957,
"optime" : {
"ts" : Timestamp(1534995039, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2018-08-23T03:30:39Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1534994548, 1),
"electionDate" : ISODate("2018-08-23T03:22:28Z"),
"configVersion" : 5,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"operationTime" : Timestamp(1534995039, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1534995039, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
3.2 使用MongoDB命令切换
--通过数据库命令切换
验证主从信息:
[root@hdp05 ~]# mongo --port 27021
TestRepSet:PRIMARY> rs.status()
--冻结即将成为主节点的从节点
[root@hdp05 ~]# mongo --port 27021
TestRepSet:PRIMARY> rs.freeze(120)
{
"ok" : 1,
"operationTime" : Timestamp(1534995549, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1534995549, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
TestRepSet:PRIMARY> rs.stepDown(120)
2018-08-23T14:24:50.897+0800 E QUERY [js] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '127.0.0.1:27021' :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
rs.stepDown@src/mongo/shell/utils.js:1433:12
@(shell):1:1
2018-08-23T14:24:50.899+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27021 failed
2018-08-23T14:24:50.900+0800 I NETWORK [js] reconnect 127.0.0.1:27021 ok
TestRepSet:SECONDARY>
TestRepSet:SECONDARY> rs.status()
{
"set" : "TestRepSet",
"date" : ISODate("2018-08-23T06:25:03.529Z"),
"myState" : 2,
"term" : NumberLong(6),
"syncingTo" : "hdp05.thinkjoy.tt:27019",
"syncSourceHost" : "hdp05.thinkjoy.tt:27019",
"syncSourceId" : 0,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1535005502, 1),
"t" : NumberLong(6)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1535005502, 1),
"t" : NumberLong(6)
},
"appliedOpTime" : {
"ts" : Timestamp(1535005502, 1),
"t" : NumberLong(6)
},
"durableOpTime" : {
"ts" : Timestamp(1535005502, 1),
"t" : NumberLong(6)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1535005472, 1),
"members" : [
{
"_id" : 0,
"name" : "hdp05.thinkjoy.tt:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 16128,
"optime" : {
"ts" : Timestamp(1535005502, 1),
"t" : NumberLong(6)
},
"optimeDurable" : {
"ts" : Timestamp(1535005502, 1),
"t" : NumberLong(6)
},
"optimeDate" : ISODate("2018-08-23T06:25:02Z"),
"optimeDurableDate" : ISODate("2018-08-23T06:25:02Z"),
"lastHeartbeat" : ISODate("2018-08-23T06:25:03.297Z"),
"lastHeartbeatRecv" : ISODate("2018-08-23T06:25:02.640Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1535005500, 1),
"electionDate" : ISODate("2018-08-23T06:25:00Z"),
"configVersion" : 5
},
{
"_id" : 1,
"name" : "hdp05.thinkjoy.tt:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 16128,
"optime" : {
"ts" : Timestamp(1535005482, 1),
"t" : NumberLong(5)
},
"optimeDurable" : {
"ts" : Timestamp(1535005482, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2018-08-23T06:24:42Z"),
"optimeDurableDate" : ISODate("2018-08-23T06:24:42Z"),
"lastHeartbeat" : ISODate("2018-08-23T06:25:03.297Z"),
"lastHeartbeatRecv" : ISODate("2018-08-23T06:25:03.479Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 5
},
{
"_id" : 2,
"name" : "hdp05.thinkjoy.tt:27021",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 16413,
"optime" : {
"ts" : Timestamp(1535005502, 1),
"t" : NumberLong(6)
},
"optimeDate" : ISODate("2018-08-23T06:25:02Z"),
"syncingTo" : "hdp05.thinkjoy.tt:27019",
"syncSourceHost" : "hdp05.thinkjoy.tt:27019",
"syncSourceId" : 0,
"infoMessage" : "syncing from: hdp05.thinkjoy.tt:27019",
"configVersion" : 5,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"operationTime" : Timestamp(1535005502, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1535005502, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
4、从节点读取数据
默认情况下,secondary节点时不允许读操作的,否则会遇到下面的报错:
TestRepSet:SECONDARY> show dbs
2018-08-23T15:41:31.667+0800 E QUERY [js] Error: listDatabases failed:{
"operationTime" : Timestamp(1535010084, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1535010084, 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:67:1
shellHelper.show@src/mongo/shell/utils.js:876:19
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
有时候为了降低primary节点的压力,需要使用secondary读写数据,这就需要用到MongoDB的slaveOkay参数,打开此参数后,就可以从secondary节点读写数据,如下:
TestRepSet:SECONDARY> rs.slaveOk();
TestRepSet:SECONDARY> show dbs
MyDB 1.672GB
admin 0.000GB
config 0.000GB
local 1.200GB
TestRepSet:SECONDARY>
5、更改oplog大小
主节点的操作记录成为oplog。它存储在一个特殊的数据库中,叫做local。oplog中的每个文档都代表主节点上执行的一个操作。oplog只记录改变数据库状态的操作,因为oplog只是作为主从节点保持数据同步的机制。
oplog的大小默认是剩余磁盘空间的5%。也可以在启动MongoDB时加上oplogSize参数指定大小。如果发现oplog大小满足不了时,可以使用下面的命令进行在线更改:
[root@hdp05 ~]# mongo --port 27019
TestRepSet:PRIMARY> use local
TestRepSet:PRIMARY> db.oplog.rs.stats().maxSize
NumberLong(1429872640)--显示出来的大小单位为字节
--设置oplog的大小为2048M
TestRepSet:PRIMARY> db.adminCommand({replSetResizeOplog: 1, size: 2048})
{
"ok" : 1,
"operationTime" : Timestamp(1534927347, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1534927347, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
TestRepSet:PRIMARY> db.oplog.rs.stats().maxSize
NumberLong("2147483648")