MongoDB叢集之分片
MongoDB分片
分片(sharding)是將資料拆分,將其分散在不同的機器上的過程。MongoDB支援自動分片
片鍵(shard key)設定分片時,需要從集合裡面選一個鍵,用該鍵作為資料拆分的依據,此鍵就叫片鍵
分片一般有以下三部分組成:
片 可以是普通的mongod程式,也可以是副本集。
但是即使一片內有多臺伺服器,也只能有一個主伺服器,其他的伺服器儲存相同的資料
Mongos
就是mongodb的路由器程式,它路由所有請求,然後將結果聚合。它不儲存儲存資料或配置資訊
它本身並不儲存資料或配置資訊(但會快取配置伺服器資訊)
配置伺服器
儲存叢集的配置資訊:資料和片的對應關係
Mongos不會永久存放資料,所以需要個地方存放分片配置
環境規劃
|
Dbpath |
logpath |
port |
備註 |
1 |
/data/mongodb/conf |
/data/mongodb/logs/conf.log |
10000 |
配置伺服器 |
2 |
—— |
/data/mongodb/logs/mongos.log |
20000 |
mongos |
3 |
/data/mongodb/shard1 |
/data/mongodb/logs/shard1.log |
30000 |
分片1 |
4 |
/data/mongodb/shard2 |
/data/mongodb/logs/shard2.log |
40000 |
分片2 |
建立db|logpath目錄
~]# mkdir /data/mongodb/conf /data/mongodb/shard{1,2}
~]# mkdir /data/mongodb/logs/
啟動配置服務
#配置伺服器要最先啟動,因為mongos會用它其上的配置資訊
~]# mongod --dbpath /data/mongodb/conf --logpath /data/mongodb/logs/conf.log --logappend --fork --port 10000
about to fork child process, waiting until server is ready for connections.
forked process: 14234
child process started successfully, parent exiting
啟動路由服務
#mongos程式不需要資料目錄
~]# mongos --port 20000 --configdb 192.168.211.217:10000 --logpath /data/mongodb/logs/mongos.log --logappend --fork
2015-01-15T17:21:13.758+0800 warning: running with 1 config server should be done only for testing purposes and is not recommended for production
about to fork child process, waiting until server is ready for connections.
forked process: 15361
child process started successfully, parent exiting
啟動分片服務
~]#mongod --fork --dbpath /data/mongodb/shard1 --logpath /data/mongodb/logs/shard1.log --logappend -port 30000
about to fork child process, waiting until server is ready for connections.
forked process: 15426
child process started successfully, parent exiting
~]#mongod --fork --dbpath /data/mongodb/shard2 --logpath /data/mongodb/logs/shard2.log --logappend -port 40000
about to fork child process, waiting until server is ready for connections.
forked process: 15515
child process started successfully, parent exiting
新增分片
#檢視mongos的埠
~]# netstat -ntpl|grep mongos
tcp 0 0 0.0.0.0:20000 0.0.0.0:* LISTEN 15361/mongos
#連線剛才啟動的mongos(192.168.211.217:20000)
~]# /usr/local/mongodb/bin/mongo --port 20000
MongoDB shell version: 2.6.6
connecting to: 127.0.0.1:20000/test
#確認是連線下mongos了
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"192.168.211.217:30000",allowLocal:true })
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({addshard:"192.168.211.217:40000",allowLocal:true })
{ "shardAdded" : "shard0001", "ok" : 1 }
注:allowLocal:true當路由程式和分片在同一臺機器上要指定allowLocal為true
#進入mongos程式config庫可以看到目前分片的情況
mongos> use config
switched to db config
mongos> db.shards.find()
{ "_id" : "shard0000", "host" : "192.168.211.217:30000" }
{ "_id" : "shard0001", "host" : "192.168.211.217:40000" }
切分資料
預設的是不會將儲存的每條資料進行分片處理,需要在資料庫和集合的粒度上都開啟分片功能
#資料庫級別開啟分片
#開啟test庫的分片功能
~]# /usr/local/mongodb/bin/mongo --port 20000
MongoDB shell version: 2.6.6
connecting to: 127.0.0.1:20000/test
mongos> use admin
switched to db admin
mongos> db.runCommand({"enablesharding":"test"})
{ "ok" : 1 }
#連線test庫長生測試資料emp集合
mongos> use test
switched to db test
。。。。。。。
mongos> db.emp.find();
{ "_id" : ObjectId("54b78bb75cb5db3323e5e57b"), "empno" : 7369, "ENAME" : "SMITH", "JOB" : "CLERK", "MGR" : 7902, "SAL" : 800 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e57c"), "empno" : 7499, "ENAME" : "ALLEN", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1600 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e57d"), "empno" : 7521, "ENAME" : "WARD", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1250 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e57e"), "empno" : 7566, "ENAME" : "JONES", "JOB" : "MANAGER", "MGR" : 7839, "SAL" : 2975 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e57f"), "empno" : 7654, "ENAME" : "MARTIN", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1250 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e580"), "empno" : 7698, "ENAME" : "BLAKE", "JOB" : "MANAGER", "MGR" : 7839, "SAL" : 2850 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e581"), "empno" : 7782, "ENAME" : "CLARK", "JOB" : "MANAGER", "MGR" : 7839, "SAL" : 2450 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e582"), "empno" : 7788, "ENAME" : "SCOTT", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e583"), "empno" : 7788, "ENAME" : "SCOTT", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e584"), "empno" : 7844, "ENAME" : "TURNER", "JOB" : "SALESMAN", "MGR" : 7698, "SAL" : 1500 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e585"), "empno" : 7876, "ENAME" : "ADAMS", "JOB" : "CLERK", "MGR" : 7788, "SAL" : 1100 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e586"), "empno" : 7900, "ENAME" : "JAMES", "JOB" : "CLERK", "MGR" : 7698, "SAL" : 950 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e587"), "empno" : 7902, "ENAME" : "FORD", "JOB" : "ANALYST", "MGR" : 7566, "SAL" : 3000 }
{ "_id" : ObjectId("54b78bb95cb5db3323e5e588"), "empno" : 7934, "ENAME" : "MILLER", "JOB" : "CLERK", "MGR" : 7782, "SAL" : 1300 }
#對集合進行分片(shardcollection)
mongos> use admin
switched to db admin
mongos> db.runCommand({"shardcollection":"test.emp","key":{"_id":1}})
{ "collectionsharded" : "test.emp", "ok" : 1 }
mongos> use config
switched to db config
mongos> db.databases.find()
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
#“_id”,字串。表示資料庫名。
“ partioned”,布林型。如果為true則表示開啟了分片功
“primary”,字串。這個值與“_id”對應表示這個資料庫的“大本營“在哪裡
mongos> db.collections.find()
{ "_id" : "test.emp", "lastmod" : ISODate("2015-01-15T09:48:49.919Z"), "dropped" : false, "key" : { "_id" : 1 }, "unique" : false, "lastmodEpoch" : ObjectId("54b78d0167f270682893cda6") }
mongos> db.chunks.find()
{ "_id" : "test.emp-_id_MinKey", "lastmod" : Timestamp(1, 0), "lastmodEpoch" : ObjectId("54b78d0167f270682893cda6"), "ns" : "test.emp", "min" : { "_id" : { "$minKey" : 1 } }, "max" : { "_id" : { "$maxKey" : 1 } }, "shard" : "shard0000" }
#這樣emp集合就按照“_id”分片了,再新增資料,就會依據“_id”片鍵分散到各個片上
檢視分片情況
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 4,
"minCompatibleVersion" : 4,
"currentVersion" : 5,
"clusterId" : ObjectId("54b7865f648316c44268333c")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.211.217:30000" }
{ "_id" : "shard0001", "host" : "192.168.211.217:40000" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
test.emp
shard key: { "_id" : 1 }
chunks:
shard0000 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27000195/viewspace-1403758/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mongodb 分片叢集建立分片集合MongoDB
- MongoDB分片叢集新增分片(自用)MongoDB
- MongoDB 分片叢集搭建MongoDB
- 搭建MongoDB分片叢集MongoDB
- mongodb 分片叢集設定MongoDB
- MongoDB分片叢集常用操作MongoDB
- mongodb副本叢集和分片叢集佈署MongoDB
- MongoDB分片叢集chunk的概念MongoDB
- 高可用mongodb叢集(分片+副本)MongoDB
- MongoDB Sharding(二) -- 搭建分片叢集MongoDB
- Mongodb分散式叢集副本集+分片MongoDB分散式
- mongodb的分散式叢集(3、分片)MongoDB分散式
- 搭建高可用MongoDB叢集(四):分片MongoDB
- 分散式文件儲存資料庫之MongoDB分片叢集分散式資料庫MongoDB
- linux下Mongodb叢集搭建:分片+副本集LinuxMongoDB
- 【MongoDB】分片(sharding)+副本集(replSet)叢集搭建MongoDB
- Mongodb副本集+分片叢集環境部署記錄MongoDB
- MongoDB健壯叢集——用副本集做分片MongoDB
- 搭建 MongoDB分片(sharding) / 分割槽 / 叢集環境MongoDB
- MongoDB分片儲存的叢集架構實現MongoDB架構
- MongoDB叢集設定集合分片生效及檢視集合分片情況MongoDB
- 分片叢集元件元件
- 部署分片叢集
- MongoDB 分片叢集均衡器導致的效能下降MongoDB
- Mongodb主從複製/ 副本集/分片叢集介紹MongoDB
- mongoDB研究筆記:分片叢集的工作機制MongoDB筆記
- Nagios監控mongodb分片叢集服務實戰iOSMongoDB
- MongoDB 4.2分片叢集搭建及與3.4分片叢集搭建時的一些異同MongoDB
- Redis叢集 - cluster叢集、資料分片Redis
- MongoDB Driver:使用正確的姿勢連線分片叢集MongoDB
- MongoDB分片叢集節點狀態stateStr:RECOVERING解決MongoDB
- mongodb之shard分片MongoDB
- 【Mongodb】分片加複製集MongoDB
- Elasticsearch高階之-叢集搭建,資料分片Elasticsearch
- mongoDB研究筆記:分片叢集部署MongoDB筆記
- 【Mongodb】分片複製集環境新增新的分片MongoDB
- MongoDB叢集同步MongoDB
- 分片叢集平衡器Balancer