好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

MongoDB分片测试

分片 是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 分片测试,希望对大家有所帮助!

查看更多关于MongoDB分片测试的详细内容...

  阅读:23次