This article is a summary of the common operations of the MongoDB replica set, and it also introduces the principle behind the operation and points for attention.
Combined with the previous article: MongoDB replica set building, you can be familiar with the construction and management of MongoDB in a short time.
The following operations are mainly divided into two sections:
1. Modify Node Status
Mainly include:
1> demote primary node to secondary node
2> Freezing secondary nodes
3> forcing secondary nodes into maintenance mode
2. Modify the configuration of the replica set
1> adding nodes
2> Deleting a node
3> Setting the secondary node to a deferred backup node
4> to set the secondary node as a hidden node
5> replacing the current replica set member
6> setting the priority of replica set nodes
7> preventing secondary nodes from being promoted to primary nodes
8> How to set a secondary node without voting rights
9> disabling chainingallowed
10> explicitly specifying a replication source for a secondary node
11> prohibit secondary nodes from creating indexes
Start by looking at all the operations supported by the MongoDB replica set
> Rs.help () rs.status () {replsetgetstatus:1} checks Repl set status Rs.initi Ate () {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) u Pdates the configuration of a running replica set with CFG (disconnects) Rs.add (HOSTPORTSTR) ad D A new member to the set with default attributes (disconnects) Rs.add (membercfgobj) add a new M Ember 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) R S.syncfrom (HOSTPORTSTR) Make a secondary sync from the given member Rs.freeze (secs) Make a node I Neligible to become primary for the time specified Rs.remove (HOSTPORTSTR) remove a host from the R Eplica set (disconnects) Rs.slaveok () Allow queries on secondary nodes RS.PRINTREPL Icationinfo () Check oplog size and time range Rs.printslavereplicationinfo () Check Replic A set members and replication lags Db.ismaster () Check who is primary reconfiguration Helpers disconnect from the database so the shell would display an error, even if the command succeeds.
Modify Node State
Demote the primary node to the secondary node
Myapp:primary> Rs.stepdown ()
This command will downgrade primary to the secondary node and maintain 60s, which could require a re-election if no new primary are elected in the period.
You can also specify the time manually
Myapp:primary> Rs.stepdown (30)
After the command is executed, the original secondary node3:27017 is upgraded to primary.
Its log output is: View Code
Original primary node3:27018 reduced to secondary View Code
Freeze secondary nodes
If you need to perform maintenance on primary, but do not want to elect other secondary nodes as primary nodes during the maintenance period, you can execute freeze commands on each secondary node, forcing them to always be in the secondary node state.
Myapp:secondary> Rs.freeze (100)
Note: Can only be performed on the secondary node
Myapp:primary> Rs.freeze (+)
{
"OK": 0,
"errmsg": "Cannot freeze node when PRIMARY or running for ELECTI On. State:primary ",
" code ":"
codename ":" Notsecondary "
}
If you want to unfreeze the secondary node, simply perform
Myapp:secondary> Rs.freeze ()
Forcing secondary nodes into maintenance mode
When the secondary node enters maintenance mode, its state is converted to "RECOVERING", in which the client does not send a read request to it, and it cannot be used as a source of replication.
There are two triggering modes for entering maintenance mode:
1. Automatic triggering
For example, perform compression on secondary
2. Manual Trigger
Myapp:secondary> Db.admincommand ({"Replsetmaintenance": true})
Modifying the configuration of a replica set
Adding nodes
Myapp:primary> Rs.add ("node3:27017")
Myapp:primary> Rs.add ({_id:3, Host: "node3:27017", priority:0, hidden:true})
You can also configure the file by the way
> cfg={
"_id": 3,
"host": "node3:27017",
"arbiteronly": false,
"buildindexes": True,
"hidden ": True,
" priority ": 0,
" tags ": {
},
" Slavedelay ": Numberlong (0),
" votes ": 1
}
> RS . Add (CFG)