MongoDB sharding test

  • 2020-06-01 11:14:25
  • OfStack

Sharding is one way of extending mongoDB. Shards split 1 collection and store different parts on different machines. When the collections of a database is too large for the current space, you need to add a new machine. Sharding will automatically distribute collection data to the new server.

1. Connect to mongos to view information related to the system


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. Enable sharding for the database

2.1 sharding of database or collection (without sharding) can be currently connected to mongos:


mongos> db.stats()
mongos> db.tab.stats()

2.2 sharding function for database activation:


# mongo racdb:28885
mongos>sh.enableSharding("test")
# or 
# mongo racdb:28885
mongos> use admin
mongos> db.runCommand( { enableSharding:"blogdb"} )

2.3 when you look at the database partitioning situation, partitioned becomes "true".


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 }

Enabling database sharding does not separate the data; you also need to shard collection.

3. Enable sharding for collections

There are several issues to consider before starting:

Select which key column to use as shard key. (more information: Considerations for Selecting Shard Keys)

If data already exists in the collection, the key column selected as shard key must be indexed; If the collection is empty, mongodb creates an index when the collection sharding (sh.shardCollection) is activated.

Set sharding function sh.shardCollection,

sh.shardCollection(".",shard-key-pattern)

mongos > sh.shardCollection("test.tab", { "_id": "hashed"})

Test insert data:


-- use python The command 
# create python file 
$ vi batch_insert.py
#-*- coding: UTF-8 -*-
import pymongo
client = pymongo.MongoClient("racdb", 28885)
db = client.testdb
# To view testdb Collection information in the database 
print (db.collection_names())
# Connect to the my_collection A collection of 
print (db.my_collection)
# empty my_collection Collection document information 
db.my_collection.remove()
# According to my_collection The number of documents in the collection 
print (db.my_collection.find().count())
# insert 10000 Bar document information 
for i in range(10000):
db.my_collection.insert({"id":i,"name":"Licz"})
# According to my_collection The number of documents in the collection 
print (' After insertion, the current number of documents: ')
print (db.my_collection.find().count())
# An insert 
[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

After insertion, the current number of documents:


10000
# Or use mongo shell Insert test data 
for (var i=1; i<=100000; i++) {
db.cc.insert({"id": i,"myName" : "cc"+i, "myDate" : new Date()});
}

Enable collection sharding


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()
-- To view sharding  state 
mongos> db.printShardingStatus();

The above content is the MongoDB sharding test introduced to you by this site, I hope to help you!


Related articles: