首页 > 代码库 > [MongoDB] 机器换IP之后的设置

[MongoDB] 机器换IP之后的设置

组里用的几台机器换了网段,MongoDB需要做重新配置。

查看Replica Set的状态如下:

?
1
2
3
4
5
6
rs.status()
{
        "startupStatus" : 1,
        "ok" : 0,
        "errmsg" : "loading local.system.replset config (LOADINGCONFIG)"
}

查看Replica Set的config如下,仍然是之前的IP:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
rs.conf()
{
        "_id" : "nj-mongo",
        "version" : 1,
        "members" : [
                {
                        "_id" : 1,
                        "host" : "192.168.131.15:27017"
                },
                {
                        "_id" : 2,
                        "host" : "192.168.131.16:27017"
                },
                {
                        "_id" : 3,
                        "host" : "192.168.131.13:27017",
                        "arbiterOnly" : true
                }
        ]
}

 

重新配置:

?
1
2
3
4
5
6
7
8
9
10
config = {
    "_id" : "nj-mongo",
    "version" : 1,
    "members" : [
        {"_id" : 1,"host" : "10.64.203.235:27017"},
        {"_id" : 2,"host" : "10.64.203.234:27017"},
        {"_id" : 3,"host" : "10.64.203.237:27017","arbiterOnly" : true}
    ]
}
rs.reconfig(config, {force: true})

这时会显示:

?
1
2
3
4
{
        "msg" : "will try this config momentarily, try running rs.conf() again in a few seconds",
        "ok" : 1
}

过一会再查看状态:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
rs.status()
{
        "set" : "nj-mongo",
        "date" : ISODate("2014-05-09T09:26:33Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 1,
                        "name" : "10.64.203.235:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 4724,
                        "optime" : Timestamp(1397201109, 4),
                        "optimeDate" : ISODate("2014-04-11T07:25:09Z"),
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "10.64.203.234:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 543,
                        "optime" : Timestamp(1397201109, 4),
                        "optimeDate" : ISODate("2014-04-11T07:25:09Z"),
                        "lastHeartbeat" : ISODate("2014-05-09T09:26:32Z"),
                        "lastHeartbeatRecv" : ISODate("2014-05-09T09:26:33Z"),
                        "pingMs" : 0,
                        "syncingTo" : "10.64.203.235:27017"
                },
                {
                        "_id" : 3,
                        "name" : "10.64.203.237:27017",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 543,
                        "lastHeartbeat" : ISODate("2014-05-09T09:26:32Z"),
                        "lastHeartbeatRecv" : ISODate("2014-05-09T09:26:33Z"),
                        "pingMs" : 0
                }
        ],
        "ok" : 1
}