1.查看集群配置屬性;
test:PRIMARY> rs.conf()
{
"_id" : "test",
"version" : 3,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "192.168.80.25:27011",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "redisvspgvsmysql:27012",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "redisvspgvsmysql:27013",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5f51115825f13fdb0adb54be")
}
}
2.修改集群的參數:
2.1 conf=rs.conf()
2.2 conf.members;
2.3 conf.members[2].slaveDelay=10(延遲10S)同時必須修改conf.members[2].priority=0(在3版本需要rs.remove,rs.add之后才可以,好像4版本直接可以,沒有4版本的數據庫)
3的版本:
rs.remove("redisvspgvsmysql:27013")
rs.add({host: "redisvspgvsmysql:27013","priority":0,"slaveDelay":10});
在3版本測試好像W的測試沒有成功,就是不會堵塞。
基本命令:
副本集操作--------轉自 春困秋乏夏打盹
官方文檔:https://docs.mongodb.com/v3.2/reference/method/js-replication/
1 rs.add()
{
_id: <int>,
host: <string>, // required
arbiterOnly: <boolean>,
buildIndexes: <boolean>,
hidden: <boolean>,
priority: <number>,
tags: <document>,
slaveDelay: <int>,
votes: <number>
}
Add a Secondary to a New Replica Set //添加一個新節點到新的副本集
rs.add( { host: "mongodbd4.example.net:27017" } )
rs.add( "mongodbd4.example.net:27017" )
Add a Secondary to an Existing Replica Set//添加一個新節點到已經存在的副本集
rs.add( { host: "mongodbd4.example.net:27017", priority: 0, votes: 0 } )
rs.status()
Reconfigure the replica set to update the votes and priority of the new member //配置副本集的投票屬性
var cfg = rs.conf();
cfg.members[n].priority = 1; // Substitute the correct array index for the new member
cfg.members[n].votes = 1; // Substitute the correct array index for the new member
rs.reconfig(cfg)
WARNING //使用rs.reconfig()命令后,當前的主節點會down掉並發生選舉
The rs.reconfig() shell method can force the current primary to step down, which causes an election. When the primary steps down,
the mongod closes all client connections. While this typically takes 10-20 seconds, try to make these changes during scheduled maintenance periods.
Avoid reconfiguring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.
Add a Priority 0 Member to a Replica Set //加入新節點,優先級為0
rs.add( { host: "mongodbd4.example.net:27017", priority: 0 } )
Add an Arbiter to a Replica Set //加入仲裁節點
rs.add( { host: "10.15.7.114:28003", arbiterOnly: true } )
添加備份節點
hidden(成員用於支持專用功能):這樣設置后此機器在讀寫中都不可見,並且不會被選舉為Primary,但是可以投票,一般用於備份數據
MyMongo:PRIMARY> rs.remove("10.15.7.114:28005"); #刪除原來的第二個仲裁節點
MyMongo:PRIMARY> rs.add({host:"10.15.7.114:28005","priority":0,"hidden":true}) #設置為備份節點
添加延遲節點
Delayed(成員用於支持專用功能):可以指定一個時間延遲從primary節點同步數據。主要用於處理誤刪除數據馬上同步到從節點導致的不一致問題
MyMongo:PRIMARY> rs.add({host:"10.15.7.114:28005","priority":0,"hidden":true,"slaveDelay":60})
Secondary-Only:不能成為primary節點,只能作為secondary副本節點,防止一些性能不高的節點成為主節點。
Non-Voting:沒有選舉權的secondary節點,純粹的備份數據節點
2 rs.addArb()
rs.addArb(host) Adds a new arbiter to an existing replica set
WARNING//一般來說,避免在一個副本集中部署多個仲裁節點
In general, avoid deploying more than one arbiter per replica set
3 rs.conf()
//查看副本集配置信息
Returns a document that contains the current replica set configuration.
rs.config()
rs.config() is an alias of rs.conf().
4 rs.freeze()
rs.freeze(seconds)
//是當前副本集在指定時間內不能成為主節點(阻止選舉)
Makes the current replica set member ineligible to become primary for the period specified
5 rs.help()
//返回副本集的一些函數
Returns a basic help text for all of the replication related shell functions.
MyMongo:PRIMARY> rs.help()
rs.status() { replSetGetStatus : 1 } checks repl set status
rs.initiate() { replSetInitiate : null } initiates set with default settings
rs.initiate(cfg) { replSetInitiate : cfg } initiates set with configuration cfg
rs.conf() get the current configuration object from local.system.replset
rs.reconfig(cfg) updates the configuration of a running replica set with cfg (disconnects)
rs.add(hostportstr) add a new member to the set with default attributes (disconnects)
rs.add(membercfgobj) add a new member to the set with extra attributes (disconnects)
rs.addArb(hostportstr) add a new member which is arbiterOnly:true (disconnects)
rs.stepDown([stepdownSecs, catchUpSecs]) step down as primary (disconnects)
rs.syncFrom(hostportstr) make a secondary sync from the given member
rs.freeze(secs) make a node ineligible to become primary for the time specified
rs.remove(hostportstr) remove a host from the replica set (disconnects)
rs.slaveOk() allow queries on secondary nodes
rs.printReplicationInfo() check oplog size and time range
rs.printSlaveReplicationInfo() check replica set members and replication lag
db.isMaster() check who is primary
reconfiguration helpers disconnect from the database so the shell will display
an error, even if the command succeeds.
6 rs.initiate()
rs.initiate(configuration) //初始化副本集
Initiates a replica set. Optionally, the method can take an argument in the form of a document that holds the configuration of a replica set.
Eg
rs.initiate(
{
_id: "myReplSet",
version: 1,
members: [
{ _id: 0, host : "mongodb0.example.net:27017" },
{ _id: 1, host : "mongodb1.example.net:27017" },
{ _id: 2, host : "mongodb2.example.net:27017" }
]
}
)
7 rs.printReplicationInfo()
//打印副本集的信息,這里為oplog日志
The following example is a sample output from the rs.printReplicationInfo() method run on the primary:
MyMongo:PRIMARY> rs.printReplicationInfo()
configured oplog size: 1024MB
log length start to end: 14939secs (4.15hrs)
oplog first event time: Thu Oct 11 2018 03:45:19 GMT+0800 (CST)
oplog last event time: Thu Oct 11 2018 07:54:18 GMT+0800 (CST)
now: Thu Oct 11 2018 07:54:23 GMT+0800 (CST)
MyMongo:PRIMARY> db.getReplicationInfo
//在slave上運行,命令為db.printSlaveReplicationInfo()
If run on a slave of a master-slave replication, the method calls db.printSlaveReplicationInfo().
8 rs.printSlaveReplicationInfo()
//返回slave與primary的延時
Returns a formatted report of the status of a replica set from the perspective of the secondary member of the set.
MyMongo:SECONDARY> rs.printSlaveReplicationInfo()
source: 10.15.7.114:28002
syncedTo: Thu Oct 11 2018 08:00:08 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
9 rs.reconfig()
rs.reconfig(configuration, force) //{ force: true }
//重新配置已經存在的副本集,重寫副本集配置文件,必須在primary節點上運行
Reconfigures an existing replica set, overwriting the existing replica set configuration. To run the method, you must connect to the primary of the replica set.
//重新配置副本集,必須先rs.conf(),在運行新的副本集配置,然后rs.reconfig()
To reconfigure an existing replica set, first retrieve the current configuration with rs.conf(), modify the configuration
document as needed, and then pass the modified document to rs.reconfig().
WARNING//避免在同一個副本集中,出現不同mongodb的版本
Avoid reconfiguring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.
//在執行命令rs.reconfig(),primary庫會set down,進行重新選舉
{ force: true }
WARNING//在執行rs.reconfig()加上force=ture參數,可導致提交的寫操作回滾,要小心使用
Using rs.reconfig() with { force: true } can lead to rollback of committed writes. Exercise caution when using this option
Member Priority and Votes
Changed in version 3.2.
Members with priority greater than 0 cannot have 0 votes.
Non-voting members must have priority of 0.
##修改1為主節點,進行重新選舉
cfg = rs.conf();
cfg.members[1].priority = 2;
rs.reconfig(cfg);
10 rs.remove()
rs.remove(hostname) //移除副本集成員
MyMongo:PRIMARY> rs.remove("10.15.7.114:28003");
11 rs.slaveOk()
MyMongo:SECONDARY> db.getMongo().setSlaveOk()
//允許在secondry節點上進行讀取操作
This allows the current connection to allow read operations to run on secondary members.
12 rs.status()
//返回副本集的當前狀態信息
This output reflects the current status of the replica set, using data derived from the heartbeat packets sent by the other members of the replica set.
13 rs.stepDown()
rs.stepDown(stepDownSecs, secondaryCatchUpPeriodSecs)
//此操作將會把primary節點變為secondry,並落后新成為primary節點%秒
Triggers the primary of the replica set to become a secondary. This in turn triggers an election for primary.
The method steps down the primary for a specified number of seconds; during this period, the stepdown member is ineligible from becoming primary.
主節點降為secondary
MyMongo:PRIMARY> use admin
MyMongo:PRIMARY> rs.stepDown(60)#單位為 秒
secondaryCatchUpPeriodSecs //Optional. The number of seconds that mongod will wait for an electable secondary to catch up to the primary.
14 rs.syncFrom()
//臨時指定成員要同步的目標
which allows administrators to temporarily override the default sync target for the current member.
Specify the name of the member you want to replicate from in the form of [hostname]:[port].
rs.syncFrom("myHost:27017");
PRIMARY> rs.status().members[1].MyMongo