mongodb複製+分片

lsq_008發表於2016-02-23
1.規劃
 
角色    主機    埠    資料檔案路徑    日誌檔案路徑       
Router    localhost    9001    /home/master/data/router1    /home/master/log/router1/router1.log       
    localhost    9002    /home/master/data/router2    /home/master/log/router2/router2.log       
Config Server    localhost    10001    /home/master/data/config1    /home/master/log/config1/config1.log       
    localhost    10002    /home/master/data/config2    /home/master/log/config2/config2.log       
    localhost    10003    /home/master/data/config3    /home/master/log/config3/config3.log       
複製集1(power1)    localhost    20001    /home/master/data/power1_01    /home/master/log/power1_01/power1_01.log       
    localhost    20002    /home/master/data/power1_02    /home/master/log/power1_02/power1_02.log       
    localhost    20003    /home/master/data/power1_03    /home/master/log/power1_03/power1_03.log       
複製集2(power2)    localhost    30001    /home/master/data/power2_01    /home/master/log/power2_01/power2_01.log       
    localhost    30002    /home/master/data/power2_02    /home/master/log/power2_02/power2_02.log       
    localhost    30003    /home/master/data/power2_03    /home/master/log/power2_03/power2_03.log   

2.建立相關目錄
mkdir /home/oracle/data/router1
mkdir /home/oracle/data/router2
mkdir /home/oracle/data/config1
mkdir /home/oracle/data/config2
mkdir /home/oracle/data/config3
mkdir /home/oracle/data/power1_01
mkdir /home/oracle/data/power1_02
mkdir /home/oracle/data/power1_03
mkdir /home/oracle/data/power2_01
mkdir /home/oracle/data/power2_02
mkdir /home/oracle/data/power2_03

mkdir -p /home/oracle/log/router1
mkdir -p /home/oracle/log/router2
mkdir -p /home/oracle/log/config1
mkdir -p /home/oracle/log/config2
mkdir -p /home/oracle/log/config3
mkdir -p /home/oracle/log/power1_01
mkdir -p /home/oracle/log/power1_02
mkdir -p /home/oracle/log/power1_03
mkdir -p /home/oracle/log/power2_01
mkdir -p /home/oracle/log/power2_02
mkdir -p /home/oracle/log/power2_03

3. 部署config server叢集
mongod --configsvr --replSet configset --port 10001 --dbpath /home/oracle/data/config1 --logpath /home/oracle/log/config1/config1.log --fork
mongod --configsvr --replSet configset --port 10002 --dbpath /home/oracle/data/config2 --logpath /home/oracle/log/config2/config2.log --fork
mongod --configsvr --replSet configset --port 10003 --dbpath /home/oracle/data/config3 --logpath /home/oracle/log/config3/config3.log --fork

--初始化叢集:
rs.initiate( {
   _id: "configset",
   configsvr: true,
   members: [
      { _id: 0, host: "master:10001" },
      { _id: 1, host: "master:10002" },
      { _id: 2, host: "master:10003" }
   ]
} )

4.啟動mongos例項
mongos --configdb configset/master:10001,master:10002,master:10003 --port 9001 --logpath /home/oracle/log/router1/router1.log --fork
mongos --configdb configset/master:10001,master:10002,master:10003 --port 9002 --logpath /home/oracle/log/router2/router2.log --fork

5.建立mongodb複製集
---啟動mongodb
mongod --replSet "power1" --dbpath /home/oracle/data/power1_01 --port 20001 --logpath /home/oracle/log/power1_01/power1_01.log --fork
mongod --replSet "power1" --dbpath /home/oracle/data/power1_02 --port 20002 --logpath /home/oracle/log/power1_02/power1_02.log --fork
mongod --replSet "power1" --dbpath /home/oracle/data/power1_03 --port 20003 --logpath /home/oracle/log/power1_03/power1_03.log --fork

mongod --replSet "power2" --dbpath /home/oracle/data/power2_01 --port 30001 --logpath /home/oracle/log/power2_01/power2_01.log --fork
mongod --replSet "power2" --dbpath /home/oracle/data/power2_02 --port 30002 --logpath /home/oracle/log/power2_02/power2_02.log --fork
mongod --replSet "power2" --dbpath /home/oracle/data/power2_03 --port 30003 --logpath /home/oracle/log/power2_03/power2_03.log --fork

mongo master:20001/admin
rs.initiate()
rs.conf()
rs.add("master:20002")
rs.add("master:20003")

rs.status()

mongo master:30001/admin
rs.initiate()
rs.conf()
rs.add("master:30002")
rs.add("master:30003")

6.將複製集加入到分片叢集中
mongo master:9001/admin
sh.addShard("power1/master:20001")
sh.addShard("power2/master:30001")

7.開啟資料庫test分片功能
mongo master:9001/adminn
sh.enableSharding("test")

8.開啟collection的分片功能
1)決定使用什麼作為片鍵。片鍵的選擇會影響分片的效能。
2)如果一個collection已經包含資料,則必須在片鍵的欄位上使用ensureIndex()命令建立一個索引。如果collection是空的,則MongoDB將在sh.shardCollection()階段建立一個索引。
3)通過在mongo shell中執行sh.shardCollection()方法開啟一個collection的分片功能。這個方法的使用語法如下:
db.shardCollection(“<database>.<collection>”, shard-key-pattern)

sh.shardCollection("records.people", { "zipcode": 1, "name": 1 } )
sh.shardCollection("people.addresses", { "state": 1, "_id": 1 } )
sh.shardCollection("assets.chairs", { "type": 1, "_id": 1 } )

sh.shardCollection("test.alerts", { "_id": "hashed" } )

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

相關文章