標籤:mongodb分區叢集配置
參考http://scholar.blog.51cto.com/9985645/1673939
一、環境
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
二、配置過程清理資料(我這裡是之前做過mongodb的其它實驗)
[[email protected] ~]# /etc/init.d/mongod stopStopping mongod: [ OK ][[email protected] ~]# rm -rf /usr/local/mongodb/data/*
1.config server配置
#修改設定檔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/mongodb.logpidfilepath=/usr/local/mongodb/log/mongodb.piddirectoryperdb=truelogappend=trueoplogSize=1000fork=truerest=true#noprealloc=true#master=true
啟動服務
[[email protected] ~]# /etc/init.d/mongodb startStarting mongod: [ OK ]#預設監聽在27019,28019上[[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配置
[[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配置
#修改設定檔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
啟動服務
[[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配置
#修改設定檔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
啟動服務
[[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.在mongos上添加shard節點
[[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" }
對所需對象啟用分區功能
#對資料庫啟用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" } #已生效#指定需要分區的Collection及索引mongos> sh.shardCollection("testdb.students",{"age":1}){ "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)
分區功能已開啟,接下來我們手動建立資料來驗證是否會分區
mongos> for (i=1;i<=100000;i++) db.students.insert({name:"student"+i,age:(i%120),address:"China"})WriteResult({ "nInserted" : 1 })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個分區shard00011 #1個分區{ "age" : { "$minKey" : 1 } } -->> { "age" : 2 } on : shard0000 Timestamp(2, 1) { "age" : 2 } -->> { "age" : 10 } on : shard0000 Timestamp(1, 2) { "age" : 10 } -->> { "age" : { "$maxKey" : 1 } } on : shard0001 Timestamp(2, 0)
本文出自 “ly36843營運” 部落格,請務必保留此出處http://ly36843.blog.51cto.com/3120113/1682580
MongoDB分區叢集配置