MongoDB搭建Replica Set Shard Cluster步驟

來源:互聯網
上載者:User

標籤:

    本文記錄從頭搭建一個MongoDB 複本集分區叢集的過程。

    我們要建立一個這樣子的分布式叢集:有兩個shard,每個shard都是一個replica set,各有兩個副本(實際產品應用中還應加上一個aribiter);有三個config server;有一個mongos。步驟如下:

1、replica set

replica set A
mkdir -p ./replset_shard1/node1
mkdir -p ./replset_shard1/node2
numactl --interleave=all mongod --port 20001 --dbpath ./replset_shard1/node1 --replSet set_a --oplogSize 1024 --logpath ./replset_shard1/node1/rs20001.log --fork
numactl --interleave=all mongod --port 20002 --dbpath ./replset_shard1/node2 --replSet set_a --oplogSize 1024 --logpath ./replset_shard1/node2/rs20002.log --fork
初始化:
rs.initiate({"_id" : "set_a", "members" : [{_id: 0, host: "xxxhost:20001"}, {_id: 1, host: "xxxhost: 20002"}]})

replica set B
mkdir -p ./replset_shard2/node1
mkdir -p ./replset_shard2/node2
numactl --interleave=all mongod --port 30001 --dbpath ./replset_shard2/node1 --replSet set_b --oplogSize 1024 --logpath ./replset_shard2/node1/rs30001.log --fork
numactl --interleave=all mongod --port 30002 --dbpath ./replset_shard2/node2 --replSet set_b --oplogSize 1024 --logpath ./replset_shard2/node2/rs30002.log --fork

初始化
rs.initiate({"_id" : "set_a", "members" : [{_id: 0, host: "xxxhost:30001"}, {_id: 1, host: "xxxhost: 30002"}]})

2、config server

mkdir -p ./data/configdb
mongod --configsvr --fork --logpath ./data/configdb/mongo17019.log --dbpath ./data/configdb1 --port 17019
mongod --configsvr --fork --logpath ./data/configdb/mongo27019.log --dbpath ./data/configdb2 --port 27019
mongod --configsvr --fork --logpath ./data/configdb/mongo37019.log --dbpath ./data/configdb3 --port 37019

3、mongos

mkdir -p ./data/mongosdb
mongos --configdb xxxhost:17019,xxxhost:27019,xxxhost:37019 --logpath ./mongosdb/mongos.log --fork --port 8100

啟動mongos時,config server的配置資訊不使用localhost、也不使用127.0.0.1,否則添加其它機器的shard會出現錯誤提示:
"can’t use localhost as a shard since all shards need to communicate. either use all shards and configdbs in localhost or all in actual IPs host: xxxxx isLocalHost"

4、replica set 添加到 shard cluster

test> use admin
switched to db admin
admin> db.runCommand({addShard: "set_a/xxxhost:20001"})
{ "shardAdded" : "set_a", "ok" : 1 }

admin> db.runCommand({addShard: "set_b/xxxhost:30001"})
{ "shardAdded" : "set_b", "ok" : 1 }

查看config.databases:
config> db.databases.find()
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "cswuyg", "partitioned" : false, "primary" : "set_a" }

查看shards:
config> db.shards.find()
{ "_id" : "set_a", "host" : "set_a/xxxhost:20001,xxxhost:20002" }
{ "_id" : "set_b", "host" : "set_b/xxxhost:30001,xxxhost:30002" }

5、對文檔使用shard功能

cswuyg> use admin
switched to db admin
admin> db.runCommand({"enablesharding": "cswuyg"})
{ "ok" : 1 }
admin> db.runCommand({"shardcollection": "cswuyg.a", "key": {"_id": 1}})
{ "collectionsharded" : "cswuyg.a", "ok" : 1 }

6、插入資料測試

var a = 10000
for (var i = 0; i < 1000000; ++i){
db.a.save({"b": i})
}
集合自動均衡後chunks的分布效果:
config> db.chunks.find()
{ "_id" : "cswuyg.a-_id_MinKey", "lastmod" : Timestamp(2, 0), "lastmodEpoch" : ObjectId("54f54f0a59b0d8e1cbf0784e"), "ns" : "cswuyg.a", "min" : { "_id" : { "$minKey" : 1 } }, "max" : { "_id" : ObjectId("54f477859a27767875b03801") }, "shard" : "set_b" }
{ "_id" : "cswuyg.a-_id_ObjectId(‘54f477859a27767875b03801‘)", "lastmod" : Timestamp(3, 0), "lastmodEpoch" : ObjectId("54f54f0a59b0d8e1cbf0784e"), "ns" : "cswuyg.a", "min" : { "_id" : ObjectId("54f477859a27767875b03801") }, "max" : { "_id" : ObjectId("54f5507a86d364ad1c3f125f") }, "shard" : "set_b" }
{ "_id" : "cswuyg.a-_id_ObjectId(‘54f5507a86d364ad1c3f125f‘)", "lastmod" : Timestamp(4, 1), "lastmodEpoch" : ObjectId("54f54f0a59b0d8e1cbf0784e"), "ns" : "cswuyg.a", "min" : { "_id" : ObjectId("54f5507a86d364ad1c3f125f") }, "max" : { "_id" : ObjectId("54f551fe86d364ad1c44a844") }, "shard" : "set_a" }
{ "_id" : "cswuyg.a-_id_ObjectId(‘54f551fe86d364ad1c44a844‘)", "lastmod" : Timestamp(3, 2), "lastmodEpoch" : ObjectId("54f54f0a59b0d8e1cbf0784e"), "ns" : "cswuyg.a", "min" : { "_id" : ObjectId("54f551fe86d364ad1c44a844") }, "max" : { "_id" : ObjectId("54f552f086d364ad1c4aee1f") }, "shard" : "set_a" }
{ "_id" : "cswuyg.a-_id_ObjectId(‘54f552f086d364ad1c4aee1f‘)", "lastmod" : Timestamp(4, 0), "lastmodEpoch" : ObjectId("54f54f0a59b0d8e1cbf0784e"), "ns" : "cswuyg.a", "min" : { "_id" : ObjectId("54f552f086d364ad1c4aee1f") }, "max" : { "_id" : { "$maxKey" : 1 } }, "shard" : "set_b" }

7、為複本集set_a添加新的副本

啟動新副本執行個體
mkdir -p ./replset_shard1/node3
numactl --interleave=all mongod --port 20003 --dbpath ./replset_shard1/node3 --replSet set_a --oplogSize 1024 --logpath ./replset_shard1/node3/rs20003.log --fork
新副本執行個體加入到複本集
進入到primary執行個體執行:
test> rs.add("xxxhost:20003")
{ "ok" : 1 }
加入之後的新副本執行個體需要時間初始化同步資料,大資料量資料初始化過程可能很長,對服務會有較大影響。而且如果同步初始化過程耗時太長時,且導致了oplog空間被寫滿一輪,則又要再次觸發同步初始化,這種情況下可以採用其它方式來實現添加副本:拷貝primary執行個體的磁碟檔案到新目錄然後以副本啟動,然後加入到replica set,這樣子就不需要有同步初始化過程。

參考:

http://docs.mongodb.org/manual/tutorial/convert-standalone-to-replica-set/http://docs.mongodb.org/manual/tutorial/restore-replica-set-from-backup/

8、其它

如果要把一個分區叢集轉為複本集叢集,需要dump出資料,然後restore回去;
如果要把一個分區叢集轉為複本集分區叢集,參考:
http://docs.mongodb.org/manual/tutorial/convert-standalone-to-replica-set/

如果是多機器的叢集,不要在replica中使用localhost、127.0.0.1,否則導致無法使用多機器部署。

 

本文所在:http://www.cnblogs.com/cswuyg/p/4356637.html

MongoDB搭建Replica Set Shard Cluster步驟

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.