分片是mongoDB擴展的一種方式。分片分割一個collection并將不同的部分存儲在不同的機器上。當一個數據庫的collections相對于當前空間過大時,你需要增加一個新的機器。分片會自動的將collection數據分發到新的服務器上。
1. 連接到mongos可查看系統相關信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
configsvr> show dbs configsvr> use config configsvr> show collections onfigsvr> db.mongos.find() { "_id" : "racdb:28885" , "ping" :ISODate( "2016-03-21T09:23:05.106Z" ), "up" :NumberLong(1436), "waiting" : true , "mongoVersion" : "3.2.3" } { "_id" : "host8.localdomain:28885" , "ping" :ISODate( "2016-03-21T09:23:07.960Z" ), "up" :NumberLong(1427), "waiting" : true , "mongoVersion" : "3.2.3" } { "_id" : "host9.localdomain:28885" , "ping" :ISODate( "2016-03-21T09:23:03.521Z" ), "up" :NumberLong(1407), "waiting" : true , "mongoVersion" : "3.2.3" } configsvr> db.shards.find() { "_id" : "shard1" , "host" : "shard1/host8:28017,racdb:28017" } { "_id" : "shard2" , "host" : "shard2/host8:28018,racdb:28018" } configsvr> db.databases.find() { "_id" : "im_offline_msg" , "primary" : "shard1" , "partitioned" : true } { "_id" : "testdb" , "primary" : "shard2" , "partitioned" : true } { "_id" : "test" , "primary" : "shard1" , "partitioned" : true } { "_id" : "blogdb" , "primary" : "shard2" , "partitioned" : false } |
2. 對數據庫啟用分片
2.1 當前可連接到 mongos 查看數據庫或者集合的分片情況(沒有分片):
1
2
|
mongos> db.stats() mongos> db.tab.stats() |
2.2 對數據庫激活分片功能:
1
2
3
4
5
6
|
# mongo racdb:28885 mongos>sh.enableSharding( "test" ) #或者 # mongo racdb:28885 mongos> use admin mongos> db.runCommand( { enableSharding: "blogdb" } ) |
2.3 此時查看數據庫分區情況,partitioned變為 “true”。
1
2
3
4
5
6
7
|
configsvr> use config switched to db config configsvr> db.databases.find() { "_id" : "im_offline_msg" , "primary" : "shard1" , "partitioned" : true } { "_id" : "testdb" , "primary" : "shard2" , "partitioned" : true } { "_id" : "test" , "primary" : "shard1" , "partitioned" : true } { "_id" : "blogdb" , "primary" : "shard2" , "partitioned" : true } |
啟用數據庫分片并沒有將數據進行分開,還需要對 collection 進行分片。
3. 對集合啟用分片
啟用前,有幾個問題需要考慮的:
選擇哪個鍵列作為shard key 。(更多參考:Considerations for Selecting Shard Keys)
如果集合中已經存在數據,在選定作為shard key 的鍵列必須創建索引;如果集合為空,mongodb 將在激活集合分片(sh.shardCollection)時創建索引。
集合分片函數sh.shardCollection ,
sh.shardCollection(".",shard-key-pattern)
mongos>sh.shardCollection("test.tab", { "_id": "hashed"})
測試插入數據:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
--使用python命令 #創建python文件 $ vi batch_insert.py #-*- coding: UTF-8 -*- import pymongo client = pymongo.MongoClient( "racdb" , 28885) db = client.testdb #查看testdb數據庫中集合信息 print (db.collection_names()) #連接到my_collection集合 print (db.my_collection) #清空my_collection集合文檔信息 db.my_collection.remove() #顯示my_collection集合中文檔數目 print (db.my_collection.find(). count ()) #插入10000條文檔信息 for i in range(10000): db.my_collection. insert ({ "id" :i, "name" : "Licz" }) #顯示my_collection集合中文檔數目 print ( '插入完畢,當前文檔數目:' ) print (db.my_collection.find(). count ()) #執行插入 [mongod@racdb ~]$ python2.7.3batch_insert.py [u 'system.indexes' , u 'table1' ,u 'my_collection' ] Collection( Database (MongoClient(host=[ 'racdb:28885' ],document_class=dict, tz_aware= False , connect = True ), u 'testdb' ), u 'my_collection' ) 0 |
插入完畢,當前文檔數目:
1
2
3
4
5
|
10000 #或是用mongo shell插入測試數據 for (var i=1; i<=100000; i++) { db.cc. insert ({ "id" : i, "myName" : "cc" +i, "myDate" : new Date ()}); } |
啟用集合分片
1
2
3
4
5
6
7
8
9
|
mongos> show collections mongos> db.cc.find() mongos> db.cc.createIndex({ "id" : "hashed" }) mongos> db.cc.getIndexes() mongos>sh.shardCollection( "testdb.cc" , { "id" : "hashed" }) mongos> db.stats() mongos> db.cc.stats() --查看sharding 狀態 mongos> db.printShardingStatus(); |
以上內容是小編給大家介紹的MongoDB分片測試,希望對大家有所幫助!