一 基礎信息
--1.1 分片環境核心組件
shard 節點: 分片節點,存儲數據,可以是單個進程,也可以是 replica set;
config 節點: 配置節點,僅存儲集羣的元數據,在生產環境上一般配置 3個 config 節點;
monos 進程: 路由進程,本身不存儲數據,負責將應用發出的SQL分發到各個 shard 節點,
monos 進程消耗資源較少,可以部署在應用服務端,也可以部署在數據庫端。
--1.2 環境信息
主機名: redhatB.example.com
OS: Red Hat Enterprise Linux Server release 6.2 (Santiago)
mongo: db version v2.2.1 ( 軟件安裝略)
--1.3 各節點端口信息
shard1 5281
shard2 5282
Config Server 7281
mongos 7282
二:搭建步驟
--2.1 增加OS用戶
[root@redhatB ~]# groupadd shard [root@redhatB ~]# useradd -g shard shard [root@redhatB ~]# passwd shard [root@redhatB ]# mkdir -p /shard [root@redhatB ]# chown -R shard:shard /shard
--2.2 配置 shard1 節點
--2.2.1 創建目錄和配置文件
mkdir -p /shard/shard1 touch /shard/shard1/shard1_5281.conf #(包含以下內容) fork = true port = 5281 dbpath = /shard/shard1 logpath = /shard/shard1/shard1.log logappend = true journal = true
--2.2.2 啓動 shard1
mongod -f /shard/shard1/shard1_5281.conf
--2.3 配置 shard2 節點
--2.3.1 創建目錄和配置文件
mkdir -p /shard/shard2 touch /shard/shard2/shard2_5282.conf #(包含以下內容) fork = true port = 5282 dbpath = /shard/shard2 logpath = /shard/shard2/shard2.log logappend = true journal = true
--2.3.2 啓動 shard2
mongod -f /shard/shard2/shard2_5282.conf
--2.4 配置 Config Server
--2.4.1 創建目錄和配置文件
mkdir -p /shard/conf touch /shard/conf/conf_7281.conf #(包含以下內容) configsvr = true fork = true port = 7281 dbpath = /shard/conf logpath = /shard/conf/conf.log logappend = true
--2.4.2 啓動 Config Server
mongod -f /shard/conf/conf_7281.conf
--2.5 啓動 mongos 進程
mongos --configdb redhatB.example.com:7281 --fork --logpath /shard/mongos.log --chunkSize 1 --port 7282
備註:--chunkSize 選項設置 chunk 的大小,默認64M,這裏將它設置爲 1M,在接下來的實驗
更容易看到分片效果。
--2.6 增加分片結點到集羣
[shard@redhatB shard]$ mongo 127.0.0.1:7282 MongoDB shell version: 2.2.1 connecting to: 127.0.0.1:7282/test mongos> show dbs; config 0.046875GB mongos> sh.addShard( "redhatB.example.com:5281" ); { "shardAdded" : "shard0000", "ok" : 1 } mongos> sh.addShard( "redhatB.example.com:5282" ); { "shardAdded" : "shard0001", "ok" : 1 }
--2.7 查看集羣狀態
mongos> sh.status(); --- Sharding Status --- sharding version: { "_id" : 1, "version" : 3 } shards: { "_id" : "shard0000", "host" : "redhatB.example.com:5281" } { "_id" : "shard0001", "host" : "redhatB.example.com:5282" } databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" }
備註:到了這裏,單節點的 Sharded Cluster 搭建完成了,接下來驗證下。
三 測試
--3.1 開啓指定數據庫 Sharding 功能
[shard@redhatB shard]$ mongo 127.0.0.1:7282 MongoDB shell version: 2.2.1 connecting to: 127.0.0.1:7282/test mongos> sh.enableSharding("francs"); { "ok" : 1 }
--3.2 開啓指定集合分片
mongos> sh.shardCollection("francs.test_1", { "id": 1} ); { "collectionsharded" : "francs.test_1", "ok" : 1 }
--3.3 查看已分片集合的索引信息
mongos> db.test_1.getIndexes(); [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "francs.test_1", "name" : "_id_" }, { "v" : 1, "key" : { "id" : 1 }, "ns" : "francs.test_1", "name" : "id_1" } ]
備註:在集合上通過命令 shardCollection 開啓分片後, 在分片的字段上默認創建了索引。
--3.4 測試:插入數據
mongos> for( var i=1; i<50001; i++) db.test_1.save({id:i,name:'abc'})
--3.5 看集合 test_1 狀態
mongos> db.test_1.stats(); { "sharded" : true, "ns" : "francs.test_1", "count" : 50000, "numExtents" : 10, "size" : 2400128, "storageSize" : 5849088, "totalIndexSize" : 3049648, "indexSizes" : { "_id_" : 1635200, "id_1" : 1414448 }, "avgObjSize" : 48.00256, "nindexes" : 2, "nchunks" : 3, "shards" : { "shard0000" : { "ns" : "francs.test_1", "count" : 11827, "size" : 567760, "avgObjSize" : 48.005411346918066, "storageSize" : 3055616, "numExtents" : 5, "nindexes" : 2, "lastExtentSize" : 2359296, "paddingFactor" : 1, "systemFlags" : 1, "userFlags" : 0, "totalIndexSize" : 735840, "indexSizes" : { "_id_" : 392448, "id_1" : 343392 }, "ok" : 1 }, "shard0001" : { "ns" : "francs.test_1", "count" : 38173, "size" : 1832368, "avgObjSize" : 48.0016765776858, "storageSize" : 2793472, "numExtents" : 5, "nindexes" : 2, "lastExtentSize" : 2097152, "paddingFactor" : 1, "systemFlags" : 1, "userFlags" : 0, "totalIndexSize" : 2313808, "indexSizes" : { "_id_" : 1242752, "id_1" : 1071056 }, "ok" : 1 } }, "ok" : 1 }
備註:"shard0000" 存儲 11827條,"shard0001" 存儲 38173 條,集合已分片。