小强哥博客

小强哥,小强哥博客,技术大咖

MongDB副本集(一),简略配置

准备三台服务器,确保三台服务器网络畅通。

第一步安装mongo,

三台服务器上分别安装mongo数据库,在centos7上参考 http://xiaoqiangge.com/aritcle/1505974551849.html 安装mongo数据库。

第二步配置副本,

config={
    "_id":"spock",
    "members":[
        {"_id":0,"host":"127.0.0.1:27017"},
        {"_id":1,"host":"127.0.0.1:27018"},
        {"_id":2,"host":"127.0.0.1:27019"},
    ]
}

config为一个json对象,其中第一个_id为副本集的名称,这里设置为spock,members为副本集成员对象,_id为副本集的节点ID,从0开始,host为副本集的服务地址。

第三步初始化副本集,在任意一台服务器执行副本集初始化代码,如下,

rs.initiate(config)

第四步启动mongo副本集

在三台服务器上分别执行mongo启动命令,格式如下,

mongod --port 21077 --replSet spock &
mongod --port 21078 --replSet spock &
mongod --port 21079 --replSet spock &

--port指定mongo服务端口号,--replSe指定副本集的名称。

接着就可以成功的使用副本集了。后面内容介绍了几个常的副本集命令。

查看副本集配置

命令如下:

rs.config()

返回内容:

spock:PRIMARY> rs.config()
{
        "_id" : "spock",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "127.0.0.1:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "127.0.0.1:27018",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "127.0.0.1:27019",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : 60000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("59c3c9a6254334856d6780db")
        }
}

查看副本集状态

命令如下:

rs.status()

返回内容:

spock:PRIMARY> rs.status()
{
        "set" : "spock",
        "date" : ISODate("2017-09-22T08:31:26.612Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1506069086, 1),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1506069086, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1506069086, 1),
                        "t" : NumberLong(2)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "127.0.0.1:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 471,
                        "optime" : {
                                "ts" : Timestamp(1506069086, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-09-22T08:31:26Z"),
                        "electionTime" : Timestamp(1506068845, 1),
                        "electionDate" : ISODate("2017-09-22T08:27:25Z"),
                        "configVersion" : 1,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "127.0.0.1:27018",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 244,
                        "optime" : {
                                "ts" : Timestamp(1506069076, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1506069076, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-09-22T08:31:16Z"),
                        "optimeDurableDate" : ISODate("2017-09-22T08:31:16Z"),
                        "lastHeartbeat" : ISODate("2017-09-22T08:31:25.274Z"),
                        "lastHeartbeatRecv" : ISODate("2017-09-22T08:31:24.631Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "127.0.0.1:27017",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "127.0.0.1:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 193,
                        "optime" : {
                                "ts" : Timestamp(1506069076, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1506069076, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2017-09-22T08:31:16Z"),
                        "optimeDurableDate" : ISODate("2017-09-22T08:31:16Z"),
                        "lastHeartbeat" : ISODate("2017-09-22T08:31:25.309Z"),
                        "lastHeartbeatRecv" : ISODate("2017-09-22T08:31:26.407Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "127.0.0.1:27018",
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}