Reference http://scholar.blog.51cto.com/9985645/1673939
First, the environment
Mongodb-1 192.168.3.31 MONGOs
Mongodb-2 192.168.3.32 Shard1
Mongodb-3 192.168.3.33 Shard2
Mongodb-4 192.168.3.34 config server
Second, the configuration process Clean up the data (I'm here for other MongoDB experiments)
[Email protected] ~]#/etc/init.d/mongod stopstopping mongod: [OK][[email P Rotected] ~]# rm-rf/usr/local/mongodb/data/*
1.config Server configuration
#修改配置文件mongodb. Conf[[email protected] ~]# cat/usr/local/mongodb/conf/mongodb.conf #bind_ip =0.0.0.0#port= 27017configsvr=true #开启config Serverdbpath=/usr/local/mongodb/datalogpath=/usr/local/mongodb/log/mongod b.logpidfilepath=/usr/local/mongodb/log/mongodb.piddirectoryperdb=truelogappend=trueoplogsize=1000fork= Truerest=true#noprealloc=true#master=true
Start the service
[[email protected] ~]# /etc/init.d/mongodb startstarting mongod: [ ok ] #默认监听在27019, 28019 on [[email protected] ~]# netstat -tunlp |grep mongotcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 1493/mongod tcp 0 0 0.0.0.0:28019 0.0.0.0:* listen 1493/mongod
2 . MONGOs Configuration
[[email protected] ~]# mongos --configdb=192.168.3.34 --fork --logpath=/tmp/ mongos.log2015-08-07t08:22:56.955+0800 w sharding running with 1 config Server should be done only for testing purposes and is not recommended for productionabout to fork child process, waiting until server is ready for connections.forked process: 1644child Process started successfully, parent exiting[[email protected] ~]# netstat -tunlp |grep mongotcp 0 0 0.0.0.0:27017 0.0.0.0:* liSten 1644/mongos
3.shard1 Configuration
#修改配置文件mongodb. Conf[[email protected] ~]# cat/usr/local/mongodb/conf/mongodb.conf #bind_ip =0.0.0.0#port= 27017dbpath=/usr/local/mongodb/datalogpath=/usr/local/mongodb/log/mongodb.logpidfilepath=/usr/local/mongodb/ log/mongodb.piddirectoryperdb=truelogappend=trueoplogsize=1000fork=true#noprealloc=true#master=true#replset= Testrs0
Start the service
[[email protected] ~]# /etc/init.d/mongod startstarting mongod: [ OK ][[email protected] ~]# netstat -tunlp |grep montcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1729/mongod
4.SHARD2 Configuration
#修改配置文件mongodb. Conf[[email protected] ~]# cat/usr/local/mongodb/conf/mongodb.conf #bind_ip =0.0.0.0#port= 27017dbpath=/usr/local/mongodb/datalogpath=/usr/local/mongodb/log/mongodb.logpidfilepath=/usr/local/mongodb/ log/mongodb.piddirectoryperdb=truelogappend=trueoplogsize=1000fork=true#noprealloc=true#master=true#replset= Testrs0
Start the service
[[email protected] ~]# /etc/init.d/mongod startstarting mongod: [ OK ][[email protected] ~]# netstat -tunlp |grep montcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 2161/mongod
5 . Add the Shard node
on MONGOs
[[Email protected] ~]# mongo --host 192.168.3.31mongodb shell version: 3.0.5connecting to: 192.168.3.31:27017/testServer has startup warnings: mongos> sh.addshard ("192.168.3.32") #添加节点1 { "shardadded" : "shard0000", "OK" : 1 }mongos> Sh.addshard ("192.168.3.33") #添加节点2 { "shardadded" : "shard0001", "OK" : 1 }mongos> sh.status () #查看状态--- Sharding Status --- sharding version: {"_id" : 1, "Mincompatibleversion" : 5, "currentVersion" : 6, "Clusterid" : objectid ("55c3fa618addd3d263602205")} shards:{ "_id" : "shard0000", "Host" : "192.168.3.32:27017" }{ "_id" : "shard0001", "host" : "192.168.3.33:27017" } balancer:Currently enabled: yescurrently running: nofailed balancer rounds in last 5 attempts: 0migration results for the last 24 hours: no recent migrations databases:{ "_id" : "admin", "partitioned" : false, "PRIMARY" : "config" }
enable the Shard feature on the desired object
#对数据库启用sharding功能mongos > sh.enablesharding ("TestDB") { "OK" : 1 }mongos> Sh.status ()--- Sharding Status --- sharding version: {"_id"  : 1, "Mincompatibleversion" : 5, "CurrentVersion" : 6, "Clusterid" : ObjectId (" 55c3fa618addd3d263602205 ")} shards:{ " _id " : " shard0000 ", " host " : "192.168.3.32:27017" }{ "_id" : "shard0001", "host"  : "192.168.3.33:27017" } balancer:Currently enabled: yesCurrently running: nofailed balancer rounds in last 5 attempts: 0migration results for the last 24 hours: no recent migrations databases:{ "_id" : "admin", "partitioned" : false, "PRIMARY" : "Config" }{ "_id" : "test", "partitioned" : false, " Primary " : " shard0000 " }{ " _id " : " TestDB ", " partitioned " : true, "PRIMARY" : "shard0000" } #已生效 # Specify the collection and index Mongos> sh.shardcollection ("Testdb.students", {"Age": 1}) that require sharding { "collectionsharded" : "Testdb.students", "OK" : 1 }mongos> sh.status ()--- sharding Status --- sharding version: {"_id" : 1, "mincompatibleversion" : 5, "CurrentVersion" : 6, "Clusterid" : objectid ("55c3fa618addd3d263602205")} shards:{ "_id" : "shard0000", "host" : " 192.168.3.32:27017 " }{ " _id " : " shard0001 ", " host " : " 192.168.3.33:27017 " } balancer:currently enabled: yescurrently running: nofailed balancer rounds in Last 5 attempts: 0migration results for the last 24 hours: No recent migrations databases:{ "_id" : "admin", " Partitioned " : false, " primary " : " config " }{ " _id " : "Test", "partitioned" : false, "primary" : "shard0000" }{ "_id" : "TestDB", "partitioned" : true, "PRIMARY"  : "shard0000" }testdb.studentsshard key: { "Age" : 1 }chunks:shard00001 { "Age" : { "$minKey" : 1 } } -->> { "Age"  : { "$maxKey" : 1 } } on : shard0000 timestamp (1, 0)
The sharding feature is turned on, and we'll create the data manually to verify that the
is fragmented.
mongos> for (i=1;i<=100000;i++) db.students.insert ({name: "Student" +i,age: (i%120), Address: "China"}) Writeresult ({ "ninserted" &NBSP;:&NBSP;1&NBSP;}) Mongos> db.students.find (). Count () 100000mongos> sh.status ()--- Sharding Status --- sharding version : {"_id" : 1, "Mincompatibleversion" : 5, "CurrentVersion" : 6, "ClusterId" : objectid ("55c3fa618addd3d263602205")} shards:{ "_id" : "shard0000 ", " host : "192.168.3.32:27017" }{ "_id" : "shard0001", "host" : "192.168.3.33:27017" } balancer:Currently enabled: yescurrently running: nofailed balancer rounds in last 5 attempts: 0migration results for the last 24 hours: 1 : success databases:{ "_id" : "admin", "partitioned" : false, "PRIMARY" : "config" }{ "_id" : "test", "partitioned" : false, "PRIMARY" : "shard0000" }{ "_id" : "TestDB", " Partitioned " : true, " primary " : " shard0000 " }testdb.studentsshard key: { "Age" : 1 }chunks:shard00002 # 2 shards shard00011 #1个分片 { "age" : { "$minKey" : 1 } } -->> { "Age" : 2 } on : Shard0000 timestamp (2, 1) { ' age ' : 2 } -->> { ' age ' : 10 } on : shard0000 timestamp (1, 2) { "age" : 10 } -->> {&nBSP; " Age " : { " $maxKey " : 1 } } on : shard0001 timestamp (2, &NBSP;0)
This article is from the "ly36843" blog, please be sure to keep this source http://ly36843.blog.51cto.com/3120113/1682580
MongoDB Shard Cluster configuration