mongoDB replica set configuration

startup mongodb with --replSet command option
example : 
1. startup one node:
mongod --replSet $setname[/$rmt_ip:$port]
2. startup another node :
mongod --replSet $setname[/$rmt_ip:$port]
3. run ininiate command within one of the nodes only : 
db.runCommand({replSetInitiate : <config_object>}) or
rs.initiate(<config_object>)
如果不带参数rs.initiate(),初始化包含$setname[/$rmt_ip:$port]的节点.

<config_object>完整格式:
{
  _id : <setname>,
  members: [
    {
      _id : <ordinal>,
      host : <hostname[:port]>,
      [, priority: <priority>]
      [, arbiterOnly : true]
      [, votes : <n>]
      [, hidden : true]
      [, slaveDelay : <n>]
      [, buildIndexes : <bool>]
      [, initialSync : {
           [state : 1|2,]
           [_id : <n>,]
           [name : <host>,]
           [optime : <date>]}]
    }
    , ...
  ],

  [settings: {
    [getLastErrorDefaults: <lasterrdefaults>]
    [, heartbeatSleep : <seconds>]
    [, heartbeatTimeout : <seconds>]
    [, heartbeatConnRetries : <n>]
  }]
}
参数说明:
强制参数:

    _id - the set name. This must match command line setting. Set names are usually alphanumeric and, in particular, cannot contain the '/' character.
    members - an array of servers in the set. For simpler configs, one can often simply set _id and host fields only – all the rest are optional.
          _id - each member has an _id ordinal, typically beginning with zero and numbered in increasing order. when a node is retired (removed from the config), its _id should not be reused.
          host - host name and optionally the port for the member

Member可选参数:

    arbiterOnly - if true, this member will participate in consensus (voting) but receive no data. Defaults to false.
    votes - number of votes this member gets in elections. Defaults to 1. Normally you do not need to set this parameter. Sometimes useful when the number of nodes are even or for biasing towards a particular data center.
    priority - priority a server has for potential election as primary. The highest priority member which is up will become primary. Default 1.0. Priority zero means server can never be primary (0 and 1 are the only priorities currently supported).
    hidden - when true, do not advertise the member's existence to clients in isMaster command responses. (v1.7+)
    slaveDelay - how far behind this slave's replication should be (in seconds). Defaults to 0 (as up-to-date as possible). Can be used to recover from human errors (accidentally dropping a database, etc.). This option can only be set on passive members. (v1.6.3+)
    buildIndexes - boolean, defaults to true. If the priority is 0, you can set buildIndexes to false to prevent indexes from being created on this member. This could be useful on a machine which is only used for backup as there is less overhead on writes if there are no secondary indexes. Note: the _id index is always created.
    initialSync (1.7.4+) - allows you to specify where this server should initially sync from. If not specified, the server will choose the first primary or secondary it finds that is healthy. The default should usually work fine, but you can change this by specifying any of the following options:
          state : 1 forces the server to clone from the primary, 2 from a secondary.
          _id : the _id of the member to clone from.
          name : the host to clone from.
          optime : finds a server that is at least this up-to-date to clone from. Can be a Date or Timestamp type.

Set可选参数:

The final optional argument, settings, can be used to set options on the set as a whole. Often one can leave out settings completely from the config as the defaults are reasonable.

    getLastErrorDefaults specifies defaults for the getlasterror command. If the client calls getLastError with no parameters, the default object specified here is used. (v1.6.2+)
    heartbeatSleep how frequently nodes should send a heartbeat to each other (default: 2 seconds, must be greater than 10 milliseconds).
    heartbeatTimeout indicates how long a node needs to fail to send data before we note a problem (default: 10 seconds, must be greater than 10 milliseconds).
    heartbeatConnRetries is how many times after heartbeatTimeout to try connecting again and getting a new heartbeat (default: 3 tries).

mongo shell replica set reference commands : 
reptest: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([secs])             step down as primary (momentarily) (disconnects)
        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()                    shorthand for db.getMongo().setSlaveOk()

        db.isMaster()                   check who is primary

        reconfiguration helpers disconnect from the database so the shell will display
        an error, even if the command succeeds.
        see also http://<mongod_host>:28017/_replSet for additional diagnostic info

或者使用命令:
db.runCommand({})
    * { isMaster : 1 }
    * { replSetGetStatus : 1 }
    * { replSetInitiate : <config> }
    * { replSetReconfig: <config> }
    * { replSetStepDown : <seconds> }
    * { replSetFreeze : <seconds> }

例如:
reptest:PRIMARY> db.runCommand({"replSetGetStatus" : 1})
{
        "set" : "reptest",
        "date" : ISODate("2011-01-12T02:38:30Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "172.16.3.33:5281",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 6595,
                        "optime" : {
                                "t" : 1294655286000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2011-01-10T10:28:06Z"),
                        "lastHeartbeat" : ISODate("2011-01-12T02:38:30Z")
                },
                {
                        "_id" : 1,
                        "name" : "db-172-16-3-39.sky-mobi.com.hz.gs:5281",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "optime" : {
                                "t" : 1294655286000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2011-01-10T10:28:06Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}

The myState field indicates the state of this server. Valid states are:
0     Starting up, phase 1
1     Primary
2     Secondary
3     Recovering
4     Fatal error
5     Starting up, phase 2
6     Unknown state
7     Arbiter
8     Down

The health field indicates the health of this server. Valid states are:
0     Server is down
1     Server is up

The errmsg field can contain informational messages, as shown above.

force a node to be primary at a given point in time,use below method : 
{ replSetStepDown : <seconds> }
Manually tell a member to step down as primary. Node will become eligible to be primary again after the specified number of seconds. (Presumably, another node will take over by then if it were eligible.)

{ replSetFreeze : <seconds> }
'Freeze' state of this member to the extent we can do that. What this really means is that this node will not attempt to become primary until the time period specified expires.
You can call again with {replSetFreeze:0} to unfreeze sooner. A process restart unfreezes the member also.
If the node is already primary, you need to use replSetStepdown instead.

# 修改replica set配置
> // shell v1.7.x:
> // example : give 1st set member 2 votes
> cfg = rs.conf()
> cfg.members[0].votes = 2
> rs.reconfig(cfg)

# 修改配置需要注意 
    * You must connect to the current primary.
    * A majority of members of the set must be up.

# 如两节点的replica环境如果将主节点冻结,在新的主节点修改配置后保存的时候可能会报错,原因是老的主节点未解冻.不能接收到消息.
如下:
reptest:PRIMARY> rs.reconfig(cfg)
Wed Jan 12 11:02:31 query failed : admin.$cmd { replSetReconfig: { _id: "reptest", version: 9, members: [ { _id: 0, host: "172.16.3.33:5281", votes: 2 }, { _id: 1, host: "172.16.3.39:5281", votes: 2.0 } ] } } to: 127.0.0.1:5281
shell got exception during reconfig: Error: error doing query: failed
in some circumstances, the primary steps down and closes connections on a reconfig
null
Wed Jan 12 11:02:31 trying reconnect to 127.0.0.1:5281
Wed Jan 12 11:02:31 reconnect 127.0.0.1:5281 ok
# 因此应该考虑先修改参数,再冻结

时间: 2024-09-22 10:23:11

mongoDB replica set configuration的相关文章

mongodb replica set 添加删除节点的2种方法_MongoDB

一,利用rs.reconfig,来添加,删除节点 1,添加节点 repmore:PRIMARY> config = {_id:"repmore",members:[{_id:0,host:'127.0.0.1:27017',priority :2},{_id:1,host:'127.0.0.1:27018',priority:1}]}; //添加节点 repmore:PRIMARY> rs.reconfig(config); //使配置生效 repmore:PRIMARY&

mongodb replica set 配置高性能多服务器详解_MongoDB

mongodb的多服务器配置,以前写过一篇文章,是master-slave模式的,请参考:详解mongodb 主从配置.master-slave模式,不能自动实现故障转移和恢复.所以推荐大家使用mongodb的replica set,来实现多服务器的高可用.给我的感觉是replica set好像自带了heartbeat功能,挺强大的. 一,三台服务器,1主,2从 服务器1:127.0.0.1:27017 服务器2:127.0.0.1:27018 服务器3:127.0.0.1:27019 1,创建

mongodb replica set 添加/删除节点方法

  replica set多服务器主从,添加,删除节点,肯定会经常遇到的.下面详细说明一下,添加,删除节点的2种方法. 一,利用rs.reconfig,来添加,删除节点 1,添加节点  代码如下   repmore:PRIMARY> config = {_id:"repmore",members:[{_id:0,host:'127.0.0.1:27017',priority :2},{_id:1,host:'127.0.0.1:27018',priority:1}]};   //

从MongoDB Replica Set HA 看分布式系统读写一致性问题

副本集基础 Replica Set是mongodb提供的一个去中心化的备份模式(同时mongodb还提供了主从部署和分片模式),每个mongod都可以是master,且副本集内会自动选举出一个primary,其他都暂时为seconary,primary挂掉后会自动选举出新的primary.副本集内所有mongod存储的都是数据全集,secondary节点会从primary同步数据操作以保证自己的数据up-to-date.副本集有自己的选举机制,相对是一种比较简单的选举,根据心跳.权重等因素选取一

MongoDB Replica Set使用几点总结

本文会涉及到MongoDB副本集的初始化,读写性能,scala driver,简单运维等内容. 副本集初始化 在各个节点上replica set进程, nohup numactl --interleave=all ./mongod --dbpath /home/mongodb/data/ --logpath /home/mongodb/mongodb-linux-x86_64-2.4.7/run.log --port 8017 --rest --journal --replSet smartq

mongodb数据库replica set shard 分片 高可用 集群

一,mongodb分片,常用的二种架构 1,每一个客户端系统上包含一个路由器mongos, 服务器的数量从十几台增长到几百台,mongos路由和mongod分片服务器之间建立了几百.有时候甚至是几千个连接,负载非常重.这意味着每当chunk平衡(MongoDB分片集群为了保持数据均匀分布所必须使用的平衡措施)发生的时候传送存储在配置数据库中的chunk位置信息都需要花费相当长的时间.这是因为每一个mongos路由都必须清楚地知道每一个chunk都存在于集群中的哪些位置. 2,路由器独立 路由独立

MongoDB之Replica Set+Sharding架构的例子

MongoDb Replica Set解决了容错和单点故障问题,但单台机器的存储和承受能力有限,Sharding就是为了海量存储和动态扩容而产生的.这才有了Replica Set+Sharding高可用架构.Sharding Cluster主要包括如下三部分: Shards:每个shard都是replica set,具有自动备份.容错.恢复能力,当然在开发环境你可配成单个mongodConfig Server:存储metadata,包括每个shard的基本信息和chunk信息,生产环境至少有3个

How Should I Backup MongoDB Databases?

Abstract: MongoDB replica set is composed of a group of MongoDB instances (processes), including one primary node and multiple secondary nodes. All the data on the MongoDB Driver (client) is written to the primary node, and the secondary node synchro

MongoDB Data Synchronization

MongoDB replica set (V3.0) synchronizes member status information through heartbeat information. Each node periodically sends heartbeat information, such as the replica set status information shown in the rs.status() method, to other members in the r