mongodb複製集搭建

liiinuuux發表於2015-04-03
環境
主機 埠4444
[root@db11 mongo]# hostname
db11
[root@db11 mongo]# pwd
/opt/mongo
[root@db11 mongo]# ls -l
total 124
drwxr-xr-x. 2 root root  4096 Mar 10 19:37 bin
drwxr-xr-x. 3 root root  4096 Mar 12 16:27 data
-rw-r--r--. 1 root root 34520 Feb 28 01:43 GNU-AGPL-3.0
-rw-r--r--. 1 root root   104 Mar 11 09:52 mongo.conf
-rw-r--r--. 1 root root 37896 Mar 16 14:52 mongo.log
-rw-------. 1 root root  1602 Mar 16 14:51 nohup.out
-rw-r--r--. 1 root root  1359 Feb 28 01:43 README
-rw-r--r--. 1 root root 22660 Feb 28 01:43 THIRD-PARTY-NOTICES

備機 埠從4000開始
兩個複製節點,一個arbiter
[root@db10 mongo]# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:C2:46:86 
          inet addr:1.1.1.10  Bcast:1.1.1.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fec2:4686/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:117884 errors:0 dropped:0 overruns:0 frame:0
          TX packets:50708 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:162996837 (155.4 MiB)  TX bytes:3730840 (3.5 MiB)

eth0:1    Link encap:Ethernet  HWaddr 08:00:27:C2:46:86 
          inet addr:1.1.1.100  Bcast:1.1.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:2    Link encap:Ethernet  HWaddr 08:00:27:C2:46:86 
          inet addr:1.1.1.101  Bcast:1.1.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:3    Link encap:Ethernet  HWaddr 08:00:27:C2:46:86 
          inet addr:1.1.1.102  Bcast:1.1.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
[root@db10 mongo]# hostname
db10
[root@db10 mongo]# ls -l
total 28
drwxr-xr-x. 2 root root 4096 Mar 16 13:50 bin
drwxr-xr-x. 2 root root 4096 Mar 16 14:07 data1
drwxr-xr-x. 2 root root 4096 Mar 16 14:07 data2

drwxr-xr-x. 2 root root 4096 Mar 16 13:50 dir
-rw-r--r--. 1 root root  104 Mar 16 13:50 mongo1.conf
-rw-r--r--. 1 root root  104 Mar 16 14:13 mongo2.conf
-rw-r--r--. 1 root root  104 Mar 16 14:14 mongo_arbiter.conf



步驟
1 在每個成員的配置檔案裡新增replica set name,並且啟動所有成員。
replSet = rs0

arbiter成員設定
replSet = rs0
journal = false

2 連線主庫,初始化複製集。
> rs.initiate()
{
        "info2" : "no configuration explicitly specified -- making one",
        "me" : "db11:4444",
        "ok" : 1
}
rs0:OTHER>

3 檢視當前replica配置。
執行後,提示符程式設計“rs0:PRIMARY>”
rs0:OTHER> rs.conf()
{
        "_id" : "rs0",
        "version" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "db11:4444",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : 0,
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatTimeoutSecs" : 10,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                }
        }
}
rs0:PRIMARY>

4 新增兩個備機和一個arbiter
rs0:PRIMARY> rs.add('1.1.1.101:4001');
{ "ok" : 1 }
rs0:PRIMARY> rs.add('1.1.1.102:4002');
{ "ok" : 1 }
rs0:PRIMARY> rs.addArb('1.1.1.100:4000')
{ "ok" : 1 }

5 讓備庫就緒
(以後每次啟動備庫都需要執行)
rs0:SECONDARY> rs.slaveOk()

6 檢視配置結果
rs0:PRIMARY> rs.conf()
{
        "_id" : "rs0",
        "version" : 5,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "db11:4444",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : 0,
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "1.1.1.101:4001",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : 0,
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "1.1.1.102:4002",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : 0,
                        "votes" : 1
                },
                {
                        "_id" : 3,
                        "host" : "1.1.1.100:4000",
                        "arbiterOnly" : true,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : 0,
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatTimeoutSecs" : 10,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                }
        }
}


7 驗證
主庫
rs0:PRIMARY> use test
switched to db test
rs0:PRIMARY> db.createCollection('obj');
{ "ok" : 1 }
rs0:PRIMARY> db.obj.insert({name: "a", value: 100});
WriteResult({ "nInserted" : 1 })
備庫
rs0:SECONDARY> use test
switched to db test
rs0:SECONDARY> db.obj.findOne();
{
        "_id" : ObjectId("55068b6b391bb83a8c51b140"),
        "name" : "a",
        "value" : 100
}
備庫整體是隻讀的,建其它資料庫也不行
rs0:SECONDARY> use ls
switched to db ls
rs0:SECONDARY> db.createCollection('xx')
{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26239116/viewspace-1485427/,如需轉載,請註明出處,否則將追究法律責任。

相關文章