Sharding is a distributed process, but the load of MongoDB is different from other loads, and in the whole MongoDB distributed cluster, there are the following roles:
Mongos:mongos is a routing server that will distribute data to its own managed Mongod cluster based on the "tablet key" set by the administrator, and is responsible for interacting directly with the client
Config: Data and slice correspondence and corresponding configuration information are saved on "config server"
MongoDB: The place where the data is stored, if you do not make shards, we will directly connect to MongoDB
The information is probably as follows:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/44/92/wKiom1Ph9B3gh67UAAEow0c6rGw678.jpg "title=" Qq20140806171424.jpg "alt=" Wkiom1ph9b3gh67uaaeow0c6rgw678.jpg "/>
Port Description:
MONGODB1 27017
MONGODB2 27018
Config 27027
MONGOs 40000
Step One
Create MONGODB1 and MONGODB2 db directories and log directories, and start MONGODB1 and MONGODB2
mkdir-p/opt/mongodb/data/s1
Mkdir-p/OPT/MONGODB/DATA/S2
Mkdir-p/opt/mongodb/log/
./mongod--port 27017--dbpath/opt/mongodb/data/s1--fork--logpath/opt/mongodb/log/s1.log
./mongod--port 27018--dbpath/opt/mongodb/data/s2--fork--logpath/opt/mongodb/log/s2.log
Step Two
Create the config server db directory and start config server
mkdir-p/opt/mongodb/data/config
./mongod--port 27027--dbpath/opt/mongodb/data/config--fork--logpath/opt/mongodb/log/config.log
Step Three
Start MONGOs Server
./mongos--port 40000--configdb localhost:27027--fork--logpath/opt/mongodb/log/mongos.log
Step Four
Service configuration
Connect the MONGOs server, and then give the 27017,27018 Mongod to MONGOs, adding shards that is addshard ()
Mongos> Db.runcommand ({"Addshard": "127.0.0.1:27017", allowlocal:true})
Mongos> Db.runcommand ({"Addshard": "127.0.0.1:27018", allowlocal:true})
The slice has been clustered, but MONGOs doesn't know how to slice the data and set the tablet key in MongoDB to fix the problem
Mongos> Db.runcommand ({"enablesharding": "Weisong"}) shards the Xiaoxin library
Set the key to the Name field inside the user table in the Weisong library
Mongos> Db.runcommand ({"Shardcollection": "Weisong.user", "key": {"name": 1}})
Step Five
Verify
Inserting 1W data into the data
Mongos> for (Var i=0;i<10000;i++) {
... db.user.insert ({"Name": "Jack" +i, "age": i})
...}
View Insert Results
Mongos> Db.printshardingstatus ()
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/44/96/wKioL1Ph_MKgXnQ7AALZoiUbUkw074.jpg "title=" Qq20140806171424.jpg "alt=" Wkiol1ph_mkgxnq7aalzoiubukw074.jpg "/>
In the return message we have to pay attention to the following four places
①shards we can clearly see that the cluster is divided into 2 slices, 27017 and 27018, respectively.
②databases we can clearly see that we've partitioned the Weisong library.
③ we saw a ratio of two shards to 2:1.
④ we will find that the name value is divided into 3 regions
Infinity Small-->NAME0, name0-->name999, name999--> Infinity
Reference documents
http://gong1208.iteye.com/blog/1622078
Http://www.cnblogs.com/huangxincheng/archive/2012/03/07/2383284.html
This article is from the "Loyalty" blog, make sure to keep this source http://xinsir.blog.51cto.com/5038915/1536529