ITPub博客

首页 > 数据库 > NoSQL > Mongodb中replicaset的搭建以及部署

Mongodb中replicaset的搭建以及部署

原创 NoSQL 作者:mchdba 时间:2014-03-06 11:41:44 0 删除 编辑

-- mongodb replicaset的搭建
1 download the install package
wget  http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.3.tgz (地址不一定对,自己去官网下在最新的版本)

2 uncompress the package
tar -xvf mongodb-linux-x86_64-2.0.2.tgz
mv mongodb-linux-x86_64-2.0.2 /usr/local/mongodb37017

tar -xvf mongodb-linux-x86_64-2.0.2.tgz
mv mongodb-linux-x86_64-2.0.2 /usr/local/mongodb37018


tar -xvf mongodb-linux-x86_64-2.0.2.tgz
mv mongodb-linux-x86_64-2.0.2 /usr/local/mongodb37019

tar -xvf mongodb-linux-x86_64-2.0.2.tgz
mv mongodb-linux-x86_64-2.0.2 /usr/local/mongodb37020

3 config the 4 conf
[root@mongo mongodb]# vim /etc/mongodb/37017.conf
dbpath = /db/db1
port = 37017
rest = true
fork = true
logpath = /etc/mongodb/mongodb17.log
logappend = true
replSet = sso-resti
#keyFile = /home/mongo/keyfile
auth = true

4 Start primary server
/usr/local/mongodb37017/bin/mongod  -f /etc/mongodb/37017.conf 

5 Start second server
/usr/local/mongodb37018/bin/mongod  -f /etc/mongodb/37018.conf
/usr/local/mongodb37019/bin/mongod  -f /etc/mongodb/37019.conf
/usr/local/mongodb37020/bin/mongod  -f /etc/mongodb/37020.conf

 


6 config 在primary上面的mongo shell控制台上面run如下command, 建立primary-second 关系

config = {_id: 'sso-resti', members: [
        {_id: 0, host: '127.0.0.1:37017'},
        {_id: 1, host: '127.0.0.1:37018'},
  {_id: 2, host: '127.0.0.1:37019'},
        {_id: 3, host: '127.0.0.1:37020'}
    ]};
    rs.initiate(config);
    rs.status();

报错:
Fri May  3 16:04:23 [rsSync] replSet initial sync pending
Fri May  3 16:04:23 [rsSync] replSet syncing to: 127.0.0.1:37017
Fri May  3 16:04:23 [rsSync] replauthenticate: no user in local.system.users to use for authentication
repl:  
Fri May  3 16:04:23 [rsSync] replSet initial sync couldn't connect to 127.0.0.1:37017
 
1 创建一个key文件,里面是随机字符串,chmod 600 key,然后在四个conf文件里都开启keyFile参数,指向这个key文件

2 你也可以将admin库文件拷贝到其他的数据目录试试,停机copy

我试了第一个方案,搞定

但是又报新的错误: 
"errmsg" : "syncThread: 13257 cmdline oplogsize (100) different than existing (14179) see:http://dochub.mongodb.org/core/increase-oplog"
注释掉 oplogSize=100参数

延迟比较optimeDate就行了

7 成功状态
PRIMARY> rs.status();
{
        "set" : "sso-resti",
        "date" : ISODate("2013-05-03T08:40:46Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "127.0.0.1:37017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "optime" : {
                                "t" : 1367567307000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-05-03T07:48:27Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "127.0.0.1:37018",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1231,
                        "optime" : {
                                "t" : 1367567307000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-05-03T07:48:27Z"),
                        "lastHeartbeat" : ISODate("2013-05-03T08:40:45Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 2,
                        "name" : "127.0.0.1:37019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1052,
                        "optime" : {
                                "t" : 1367567307000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-05-03T07:48:27Z"),
                        "lastHeartbeat" : ISODate("2013-05-03T08:40:44Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 3,
                        "name" : "127.0.0.1:37020",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1062,
                        "optime" : {
                                "t" : 1367567307000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2013-05-03T07:48:27Z"),
                        "lastHeartbeat" : ISODate("2013-05-03T08:40:45Z"),
                        "pingMs" : 0
                }
        ],
        "ok" : 1
}

 

8 insert data and test

PRIMARY> use test;
switched to db test
PRIMARY> db.tim.save({"":1});
PRIMARY> db.tim.save({"a":2});
PRIMARY> db.tim.find();
{ "_id" : ObjectId("51837ab1c77389f37f74792d"), "" : 1 }
{ "_id" : ObjectId("51837ac0c77389f37f74792e"), "a" : 2 }
PRIMARY>

SECONDARY> show collections;
Fri May  3 16:54:09 uncaught exception: error: { "$err" : "not master and slaveok=false", "code" : 13435 }
SECONDARY> 
SECONDARY> db.getMongo().setSlaveOk()
not master and slaveok=false
SECONDARY> db.tim.find();
{ "_id" : ObjectId("51837ab1c77389f37f74792d"), "" : 1 }
{ "_id" : ObjectId("51837ac0c77389f37f74792e"), "a" : 2 }
SECONDARY> 

 

primary库和secondary库上数据一致了,OK.

 

PS: 感谢网友Mr.Sharp的大力协助。没有他我不能如此顺利部署完毕。

 

[2013-08-02 续补]

以上的例子是没有设立primary与secondary的切换监控服务器arbiter的,补充一个初始化的时候设立arbiter监控的配置,如下:

 config = {_id: 'rpl', members: [
        {_id: 0, host: '10.100.10.19:27017'},
        {_id: 1, host: '10.100.10.19:27018'},
  {_id: 2, host: '10.100.10.19:27019', arbiterOnly: true},
    ]};
 rs.initiate(config);
 rs.status();

{
        "set" : "rpl",
        "date" : ISODate("2013-08-02T02:57:09Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "10.100.10.19:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 66247,
                        "optime" : {
                                "t" : 1375349303000,
                                "i" : 2995
                        },
                        "optimeDate" : ISODate("2013-08-01T09:28:23Z"),
                        "lastHeartbeat" : ISODate("2013-08-02T02:57:08Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 1,
                        "name" : "10.100.10.19:27018",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "optime" : {
                                "t" : 1375349303000,
                                "i" : 2995
                        },
                        "optimeDate" : ISODate("2013-08-01T09:28:23Z"),
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "10.100.10.19:27019",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 66247,
                        "optime" : {
                                "t" : 0,
                                "i" : 0
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2013-08-02T02:57:08Z"),
                        "pingMs" : 0
                }
        ],
        "ok" : 1
}
PRIMARY>

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26230597/viewspace-1102132/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
Happy is the man who is living by his hobby.

注册时间:2011-09-05

  • 博文量
    147
  • 访问量
    3749963