Windows环境下Mongo副本集群搭建

总结一下windows 64 mongoDB 集群

 

至少要求三个mongo ,本次搞了四个
,后面又在另外一个实例中增加一个,步骤1:
新建文件夹:data用的 
E:/diange/MongoDB/data/b/r1 
E:/diange/MongoDB/data/b/r2
E:/diange/MongoDB/data/b/r3 
E:/diange/MongoDB/data/b/r4
新建文件夹:给logs用的
E:/diange/MongoDB/logs/b
2、这里需要打开4个cmd窗口分别 cd到同一个实例下启动mongo 
mongod  --dbpath E:/diange/MongoDB/data/b/r1 --port 27108 --replSet myrepl  --logappend --logpath E:/diange/MongoDB/logs/b/r1.log --oplogSize 64
mongod  --dbpath E:/diange/MongoDB/data/b/r2 --port 27109 --replSet myrepl  --logappend --logpath E:/diange/MongoDB/logs/b/r2.log --oplogSize 64
mongod  --dbpath E:/diange/MongoDB/data/b/r3 --port 27110 --replSet myrepl  --logappend --logpath E:/diange/MongoDB/logs/b/r3.log --oplogSize 64
mongod  --dbpath E:/diange/MongoDB/data/b/r4 --port 27111 --replSet myrepl  --logappend --logpath E:/diange/MongoDB/logs/b/r4.log --oplogSize 64

3、新打开一个cmd窗口,cd到mongo文件夹bin目录下,输入命令,链接任意一个启动的mongo
命令如下:mongo 172.16.90.29:27108 

4、然后输入命令设置集群配置
var myrepl={  
    _id:'myrepl',  
    members:[  
        {_id:0, host:'172.16.90.29:27108'},  
        {_id:1, host:'172.16.90.29:27109'}, 
        {_id:2, host:'172.16.90.29:27110'},
        {_id:3, host:'172.16.90.29:27111'}]  
} 
rs.initiate(myrepl); 
5、验证集群配置
输入命令rs.status();
查看集群状态
结果如下:
myrepl:PRIMARY> rs.status();
{
        "set" : "myrepl",
        "date" : ISODate("2015-05-19T10:14:29Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "172.16.90.29:27108",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 322,
                        "optime" : Timestamp(1432030448, 1),
                        "optimeDate" : ISODate("2015-05-19T10:14:08Z"),
                        "lastHeartbeat" : ISODate("2015-05-19T10:14:29Z"),
                        "lastHeartbeatRecv" : ISODate("2015-05-19T10:14:28
                        "pingMs" : 0,
                        "syncingTo" : "172.16.90.29:27110"
                },
                {
                        "_id" : 1,
                        "name" : "172.16.90.29:27109",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 433,
                        "optime" : Timestamp(1432030448, 1),
                        "optimeDate" : ISODate("2015-05-19T10:14:08Z"),
                        "lastHeartbeat" : ISODate("2015-05-19T10:14:28Z"),
                        "lastHeartbeatRecv" : ISODate("2015-05-19T10:14:28
                        "pingMs" : 0,
                        "syncingTo" : "172.16.90.29:27110"
                },
                {
                        "_id" : 2,
                        "name" : "172.16.90.29:27110",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 435,
                        "optime" : Timestamp(1432030448, 1),
                        "optimeDate" : ISODate("2015-05-19T10:14:08Z"),
                        "electionTime" : Timestamp(1432030115, 1),
                        "electionDate" : ISODate("2015-05-19T10:08:35Z"),
                        "self" : true
                },
                {
                        "_id" : 3,
                        "name" : "172.16.90.29:27111",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 21,
                        "optime" : Timestamp(1432030448, 1),
                        "optimeDate" : ISODate("2015-05-19T10:14:08Z"),
                        "lastHeartbeat" : ISODate("2015-05-19T10:14:28Z"),
                        "lastHeartbeatRecv" : ISODate("2015-05-19T10:14:29
                        "pingMs" : 0,
                        "syncingTo" : "172.16.90.29:27110"
                }
        ],
        "ok" : 1
}

6、如果需要新增节点的话,也可以cd 到新的实例下或者在相同的实例下,重复上述步骤如下:
新开cmd窗口
cd 到  e:/zxdg/mongo/bin
mongod  --dbpath E:/点歌/MongoDB/data/b/r5 --port 27112 --replSet myrepl  --logappend --logpath E:/diange/MongoDB/logs/b/r4.log --oplogSize 64
链接任意的一个mongo, mongo 172.16.90.29:27108
链接成功后,rs.status();
查一下哪个节点是主节点,然后,退出当前节点的连接,然后连接主节点输入一下内容
重新设置集群参数 
var myrepl={  
    _id:'myrepl',  
    members:[  
        {_id:0, host:'172.16.90.29:27108'},  
        {_id:1, host:'172.16.90.29:27109'}, 
        {_id:2, host:'172.16.90.29:27110'},
        {_id:3, host:'172.16.90.29:27111'},
        {_id:4, host:'172.16.90.29:27112'}]  
} 
rs.reconfig(myrepl); 

然后验证就可以了
注意:代码里连mongo时,如果链接没有try,primary节点关闭的话,会报异常,关闭secondary
节点不会有影响
所以,还是要在获取mongo连接时,try catch一下,这样不管你关哪个节点,程序都能正常执行 

有很多命令可以用,上网搜一下即可

 linux环境的集群方式,参照:

http://blog.csdn.net/luonanqin/article/details/8497860

 

博客写的很详细

你可能感兴趣的:(windows)