Mongodb中replicaset的搭建以及部署

mchdba發表於2014-03-06

-- mongodb replicaset的搭建
1 download the install package
wget   (地址不一定對,自己去官網下在最新的版本)

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:"
註釋掉 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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章