新聞中心
在Mongodb里面存在另一種集群,就是分片技術(shù),可以滿足MongoDB數(shù)據(jù)量大量增長的需求。

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、東營區(qū)ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的東營區(qū)網(wǎng)站制作公司
當(dāng)MongoDB存儲海量的數(shù)據(jù)時(shí),一臺機(jī)器可能不足以存儲數(shù)據(jù),也可能不足以提供可接受的讀寫吞吐量。這時(shí),我們就可以通過在多臺機(jī)器上分割數(shù)據(jù),使得數(shù)據(jù)庫系統(tǒng)能存儲和處理更多的數(shù)據(jù)。
環(huán)境準(zhǔn)備
Linux環(huán)境
| 主機(jī) | OS | 備注 |
|---|---|---|
| 192.168.32.13 | CentOS6.3 64位 | 普通PC |
| 192.168.71.43 | CentOS6.2 64位 | 服務(wù)器,NUMA CPU架構(gòu) |
MongoDB版本:mongodb-linux-x86_64-2.4.1,下載地址:www.mongodb.org/downloads.
MongoDB安裝:分別在兩臺機(jī)器上安裝好mongodb 2.4.1,安裝路徑都為/url/local/mongodb-2.4.1/
cd /usr/local/src/
wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.1.tgz
tar -zxvf mongodb-linux-x86_64-2.4.1.tgz
cp -r mongodb-linux-x86_64-2.4.1 /usr/local/mongodb-2.4.1
cd /usr/local/mongodb-2.4.1/bin/
ll
可以看到mongodb安裝成功有如下模塊:
**mongodb**啟動和關(guān)閉等在后面集群搭建中有詳細(xì)說明,在此不再贅述。
Sharding集群搭建
Mongodb一共有三種集群搭建的方式:Replica Set(副本集)、Sharding(切片)和Master-Slaver(主從)。下面要搭建的是Sharding,Sharding集群也是三種集群中最復(fù)雜的。
配置服務(wù)器啟動(192.168.32.13:10000):
1. ./bin/mongod --fork --dbpath data/config/ --logpath log/config.log –port 10000
路由服務(wù)器啟動(192.168.32.13:20000):
1. ./bin/mongos --port 20000 --configdb 192.168.32.13:10000 --logpath log/mongos.log --fork
注意1:配置–conigdb的時(shí)候ip地址不能填localhost或127.0.0.1否則添加分片時(shí)會返回如下錯(cuò)誤信息:
1. {
2. "ok" : 0,
3. "errmsg" : "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: 192.168.71.43:27017 isLocalHost:0"
4. }
啟動分片1(192.168.32.13:27019):
1. ./bin/mongod --dbpath data/shard3/ --logpath log/shard3.log --fork --port 27019
啟動分片2(192.168.32.13:27020):
1. ./bin/mongod --dbpath data/shard3/ --logpath log/shard3.log --fork --port 27020
啟動分片3(192.168.71.43:27017):
1. numactl --interleave=all ./bin/mongod --dbpath data/shard1/ --logpath log/shard1.log --fork --port 27017
啟動分片4(192.168.71.43:27018):
1. numactl --interleave=all ./bin/mongod --dbpath data/shard2/ --logpath log/shard2.log --fork --port 27018
說明:關(guān)于這里為什么加numactl –interleave=all,后面有詳細(xì)說明。
Note:在生產(chǎn)環(huán)境可以將啟動的配置信息寫入文件,然后啟動的時(shí)候指定配置文件,這樣便于管理:
1. vi conf/config.conf
2. bpath=data/config/
3. logpath=log/config.log
4. port=10000
5. fork=true
6. ./bin/mongod -f conf/config.conf
添加分片1(192.168.32.13:27019):
1. ./bin/mongo --port 20000
2. mongos> use admin
3. switched to db admin
4. mongos> db.runCommand({addshard:"192.168.32.13:27019",allowLocal:true })
注意2:同樣這里的192.168.32.13不能用localhost或127.0.0.1代替,并且當(dāng)路由進(jìn)程和分片在同一臺機(jī)器上要指定allowLocal為true,因?yàn)镸ongoDB盡量避免錯(cuò)誤的配置,將集群配置在本地,所以這個(gè)配置指明當(dāng)前僅僅是用于開發(fā)。
添加分片3(192.168.71.43:27017):
1. mongos> db.runCommand({addshard:"192.168.71.43:27017" })
類似的添加分片2,4。
分片添加成功返回類似下面的信息(當(dāng)前mongodb版本為2.4.1):
1. { "shardAdded" : "shard0000", "ok" : 1 }
刪除分片:如果要刪除分片的話可以removeshard命令:
1. mongos> use admin
2. switched to db admin
3. mongos> db.runCommand({"removeshard":"192.168.32.13:27020"})
4. {
5. "msg" : "draining started successfully",
6. "state" : "started",
7. "shard" : "shard0001",
8. "note" : "you need to drop or movePrimary these databases",
9. "dbsToMove" : [
10. "test3"
11. ],
12. "ok" : 1
13. }
移除分片需要一個(gè)過程,MongoDB會把移除的片上的數(shù)據(jù)(塊)挪到其他片上,移動中會顯示進(jìn)度:
1. mongos> db.runCommand({"removeshard":"192.168.32.13:27020"})
2. {
3. "msg" : "draining ongoing",
4. "state" : "ongoing",
5. "remaining" : {
6. "chunks" : NumberLong(0),
7. "dbs" : NumberLong(1)
8. },
9. "note" : "you need to drop or movePrimary these databases",
10. "dbsToMove" : [
11. "test3"
12. ],
13. "ok" : 1
14. }
注意:如果刪除的片是數(shù)據(jù)庫的大本營(基片),必須手動移動或刪除數(shù)據(jù)庫,用moveprimary命令,上面的示例中就提示192.168.32.13:27020是test3庫的大本營(primary),這個(gè)信息可以通過查看config.databases看到:
1. mongos> use config
2. switched to db config
3. mongos> db.databases.find()
4. { "_id" : "test3", "partitioned" : false, "primary" : "shard0001" }
這里shard0001就是192.168.32.13:27020,下面通過moveprimary命令移除test3:
1. mongos> use admin
2. switched to db admin
3. mongos> db.runCommand({"moveprimary":"test3","to":"192.168.32.13:27019"})
4. { "primary " : "shard0000:192.168.32.13:27019", "ok" : 1 }
這時(shí)再查看config.databases會發(fā)現(xiàn)test3的大本營變成了shard0000(192.168.32.13:27019)
這時(shí)再執(zhí)行removeshard命令則能成功移除分片了:
1. mongos> db.runCommand({"removeshard":"192.168.32.13:27020"})
2. {
3. "msg" : "removeshard completed successfully",
4. "state" : "completed",
5. "shard" : "shard0001",
6. "ok" : 1
7. }
管理分片
進(jìn)入mongos進(jìn)程config庫可以看到目前分片的情況:
1. ./bin/mongo –port 20000
2. use config
3. db.shards.find()
1. mongos> use config
2. switched to db config
3. mongos> db.shards.find()
4. { "_id" : "shard0000", "host" : "192.168.32.13:27019" }
5. { "_id" : "shard0001", "host" : "192.168.71.43:27017" }
6. { "_id" : "shard0002", "host" : "192.168.71.43:27018" }
注意3:如果配置過程中發(fā)生過上面注意1中出現(xiàn)的情況,即配置configdb的時(shí)候用了localhost或127.0.0.1,則運(yùn)行db.shards.find()可能會出現(xiàn)如下錯(cuò)誤:
1. mongos> db.shards.find()
2. error: {
3. "$err" : "could not initialize sharding on connection 192.168.32.13:10000 :: caused by :: mongos specified a different config database string : stored : localhost:10000 vs given : 192.168.32.13:10000",
4. "code" : 15907
5. }
解決方法是重新啟動config進(jìn)程。
查看分片后的數(shù)據(jù)庫:
1. ./bin/mongo –port 20000
2. use test
3. db.test.user.insert({“test”: “test”})
4. ……
5. use config
6. db.databases.find()
7. { "_id" : "admin", "partitioned" : false, "primary" : "config" }
8. { "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
9. { "_id" : "test2", "partitioned" : false, "primary" : "shard0000" }
10. { "_id" : "test3", "partitioned" : false, "primary" : "shard0001" }
“_id”,字符串。表示數(shù)據(jù)庫名。
“partioned”,布爾型。如果為true則表示開啟了分片功能。
“primary”,字符串,這個(gè)值與“_id”對應(yīng)表示這個(gè)數(shù)據(jù)庫的“大本營“在哪里,不論分片與否,數(shù)據(jù)庫總是會有個(gè)“大本營”,創(chuàng)建數(shù)據(jù)庫時(shí)會隨機(jī)選擇一個(gè)片,也就是說大本營是開始創(chuàng)建數(shù)據(jù)庫文件的位置。雖然分片的時(shí)候數(shù)據(jù)庫也會用到很多別的服務(wù)器,但是從這分片開始。
至此整個(gè)mongodb分片集群基本搭建完成,但是想讓分片正常、高效、穩(wěn)定的運(yùn)行還有很多工作要做,下一節(jié)將在此基礎(chǔ)上做一些簡單的測試。
網(wǎng)站欄目:部署MongoDB分片集群
文章地址:http://m.fisionsoft.com.cn/article/dhdjged.html


咨詢
建站咨詢
