MongoDB Shard Overview Shards
In MongoDB there is another cluster, that is, the Shard technology, can meet the requirements of a large number of MONGODB data volume growth.
When MongoDB stores massive amounts of data, a machine may not be enough to store data, or it may not be sufficient to provide acceptable read and write throughput. At this point, we can divide the data on multiple machines so that the database system can store and process more data.
Why use Shards
Copy all write operations to the master node
Deferred sensitive data is queried on the master node
A single replica set is limited to 12 nodes
When the volume of requests is large, there is insufficient memory.
Insufficient Local Disk
Vertical expansion is expensive
MongoDB Shard
Shows the use of shard cluster structure distribution in MongoDB:
650) this.width=650; "src=" Http://www.runoob.com/wp-content/uploads/2013/12/sharding.png "style=" border:0px; Margin:0px;padding:0px;height:auto; "alt=" Sharding.png "/>
There are three main components as described below:
Shard:
Used to store actual blocks of data, a Shard server role in the actual production environment can be assumed by several machines, one replica set, to prevent a single point of failure of the host
Config Server:
The Mongod instance, which stores the entire clustermetadata, including chunk information.
Query Routers:
Front-end routing, which the client accesses, and makes the entire cluster look like a single database, the front-end application can be used transparently.
Shard Instance
192.168.1.100:mongos
192.168.1.110:config Server
192.168.1.101:shard1
192.168.1.102:shard2
The MongoDB database is installed on all four of the above hosts.
I. Configuring CONFIG server
To edit a configuration file mongod.conf:
Vim/etc/mongod.conf
Add the following and enable the Config Server service:
Configsvr=true
To restart the MongoDB service:
Service Mongod Restart
Note: The default listener is 27019 ports. You can also start the Mongod process using the following command.
# Mongod--configsvr--dbpath <path>--port <port>
At this point, the default port is changed from 27017 to 27019:
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8F/26/wKioL1jU5xDSUa-9AABB4cvrN-8860.jpg-wh_500x0-wm_ 3-wmp_4-s_1114483490.jpg "title=" 2017-03-24_172929.jpg "alt=" Wkiol1ju5xdsua-9aabb4cvrn-8860.jpg-wh_50 "/>
Two. Configuring MONGOs instances
Install MONGOs:
Yum Install mongodb-org-mongos-2.6.1-1.x86_64.rpm
Start MONGOs:
MONGOs--configdb=192.168.1.110--fork--logpath=/var/log/mongodb/mongos.log
Note: MONGOs is a lightweight application that can run on the same node as other services, and at startup, it is necessary to specify the access address of each config server for the MONGOs instance;
By default, MONGOs listens on port 27017, and the MONGOs instance can be started using the following command.
# mongos--configdb <config server hostnames (ip| Hostname) >:P ort)
You can also edit the configuration file directly:
1, note dbpath directive;
2, add configdb instruction, and specify the address of config server;
Then use the following command to start the MONGOs instance:
# mongos-f/etc/mongod.conf
Log in to MONGOs and configure the Shard node:
MONGO--host 192.168.1.100mongos> sh.addshard ("192.168.1.101") {"shardadded": "shard0000", "OK": 1}mongos> sh.ad Dshard ("192.168.1.102") {"shardadded": "shard0001", "OK": 1}
View Shard Status:
mongos> sh.status ()---sharding status---sharding version: {"_id": 1, "Version": 4, "Mincompatibleversion": 4, "cur Rentversion ": 5," Clusterid ": ObjectId (" 58d4bd8a102ad4bdad74aa1d ")} shards:{" _id ":" shard0000 "," host ":" 192.168.1. " 101:27017 "} {" _id ":" shard0001 "," host ":" 192.168.1.102:27017 "} databases:{" _id ":" admin "," partitioned ": Fals E, "PRIMARY": "Config"}
To start the sharding function:
Mongos> sh.enablesharding ("TestDB"); {"OK": 1}
Note: MongoDB's shard feature is implemented at the collection level, but to start Shard on collection, it also needs to be enabled on its associated database beforehand. When the Shard feature is enabled on the database, MongoDB assigns it a Master shard.
The Enable procedure needs to be implemented on MONGOs instances, you can use the Sh.enablesharding () method, or you can use the enablesharding command of Db.runcommand (), which uses the following format, respectively:
Sh.enablesharding ("<database>") Db.runcommand ({enablesharding: <database>})
At this point, look again at the Shard status:
mongos> sh.status ()---sharding status---sharding version: {"_id": 1, "Version": 4, "Mincompatibleversion": 4, "cur Rentversion ": 5," Clusterid ": ObjectId (" 58d4bd8a102ad4bdad74aa1d ")} shards:{" _id ":" shard0000 "," host ":" 192.168.1. " 101:27017 "} {" _id ":" shard0001 "," host ":" 192.168.1.102:27017 "} databases:{" _id ":" TestDB "," Partitioned ": Tru E, "PRIMARY": "shard0000"}
Test:
Sharding on the collection:
Mongos> sh.shardcollection ("Testdb.student", {"Age": 1}) {"collectionsharded": "Testdb.student", "OK": 1}
Insert data:
Mongos> for (i=1;i<=100000;i++) Db.student.insert ({name: "Student" +i,age: (i%120), classes: "Class+ (I%10)"}); Writeresult ({"ninserted": 1})
At this point we re-view the sharding status as follows:
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8F/28/wKiom1jU7KLzU_tMAAEpDByx8Kk967.jpg-wh_500x0-wm_ 3-wmp_4-s_1262078485.jpg "title=" status. jpg "alt=" wkiom1ju7klzu_tmaaepdbyx8kk967.jpg-wh_50 "/>
MongoDB Shard (sharding) feature implementation