-
集群配置:
-
副本集配置(在每个 shard的主节点上进行配置及初始化,否则会初始化失败 ):
-
shard1的副本集配置(主节点、副本节点、仲裁节点):
bin]$ ./mongo 172.16.16.124:40001 MongoDB shell version: 3.0.7 connecting to: 172.16.16.124:40001/test > use admin switched to db admin > config = { _id:"shard1", members:[ {_id:0,host:"172.16.16.124:40001"}, {_id:1,host:"172.16.16.125:40001"}, {_id:2,host:"172.16.16.126:40001",arbiterOnly:true}] } #以下为输出 { "_id" : "shard1", "members" : [ { "_id" : 0, "host" : "172.16.16.124:40001" }, { "_id" : 1, "host" : "172.16.16.125:40001" }, { "_id" : 2, "host" : "172.16.16.126:40001", "arbiterOnly" : true } ] } > rs.initiate(config); #初始化配置 { "ok" : 1 }
-
shard2的副本集配置(主节点、副本节点、仲裁节点):
bin]$ ./mongo 172.16.16.125:40002 MongoDB shell version: 3.0.7 connecting to: 172.16.16.125:40002/test > use admin switched to db admin > config = { _id:"shard2", members:[ {_id:0,host:"172.16.16.125:40002"}, {_id:1,host:"172.16.16.126:40002"}, {_id:2,host:"172.16.16.131:40002",arbiterOnly:true}] } #以下为输出 { "_id" : "shard2", "members" : [ { "_id" : 0, "host" : "172.16.16.125:40002" }, { "_id" : 1, "host" : "172.16.16.126:40002" }, { "_id" : 2, "host" : "172.16.16.131:40002", "arbiterOnly" : true } ] } > rs.initiate(config); #初始化配置 { "ok" : 1 }
-
shard3的副本集配置(主节点、副本节点、仲裁节点):
bin]$ ./mongo 172.16.16.126:40003 MongoDB shell version: 3.0.7 connecting to: 172.16.16.126:40003/test > use admin switched to db admin > config = { _id:"shard3", members:[ {_id:0,host:"172.16.16.126:40003"}, {_id:1,host:"172.16.16.131:40003"}, {_id:2,host:"172.16.16.124:40003",arbiterOnly:true}] } #以下为输出 { "_id" : "shard3", "members" : [ { "_id" : 0, "host" : "172.16.16.126:40003" }, { "_id" : 1, "host" : "172.16.16.131:40003" }, { "_id" : 2, "host" : "172.16.16.124:40003", "arbiterOnly" : true } ] } > rs.initiate(config); #初始化配置 { "ok" : 1 }
-
shard4的副本集配置(主节点、副本节点、仲裁节点):
bin]$ ./mongo 172.16.16.131:40004 MongoDB shell version: 3.0.7 connecting to: 172.16.16.131:40004/test > use admin switched to db admin > config = { _id:"shard4", members:[ {_id:0,host:"172.16.16.131:40004"}, {_id:1,host:"172.16.16.124:40004"}, {_id:2,host:"172.16.16.125:40004",arbiterOnly:true}] } #以下为输出 { "_id" : "shard4", "members" : [ { "_id" : 0, "host" : "172.16.16.131:40004" }, { "_id" : 1, "host" : "172.16.16.124:40004" }, { "_id" : 2, "host" : "172.16.16.125:40004", "arbiterOnly" : true } ] } > rs.initiate(config); #初始化配置 { "ok" : 1 }
-
分片配置
bin]$ ./mongo 172.16.16.124:50001 mongos> use admin switched to db admin mongos> db.runCommand({addshard:"shard1/172.16.16.124:40001,172.16.16.125:40001,172.16.16.126:40001"}); { "shardAdded" : "shard1", "ok" : 1 } mongos>db.runCommand({addshard:"shard2/172.16.16.125:40002,172.16.16.126:40002,172.16.16.131:40002"}); { "shardAdded" : "shard2", "ok" : 1 } mongos>db.runCommand({addshard:"shard3/172.16.16.126:40003,172.16.16.131:40003,172.16.16.124:40003"}); { "shardAdded" : "shard3", "ok" : 1 } mongos>db.runCommand({addshard:"shard4/172.16.16.131:40004,172.16.16.124:40004,172.16.16.125:40004"}); { "shardAdded" : "shard4", "ok" : 1 }
-
查看配置是否生效(仲裁不被列出 ):
mongos> db.runCommand( { listshards : 1 } ); { "shards" : [ { "_id" : "shard1", "host" : "shard1/172.16.16.124:40001,172.16.16.125:40001" }, { "_id" : "shard2", "host" : "shard2/172.16.16.125:40002,172.16.16.126:40002" }, { "_id" : "shard3", "host" : "shard3/172.16.16.126:40003,172.16.16.131:40003" }, { "_id" : "shard4", "host" : "shard4/172.16.16.124:40004,172.16.16.131:40004" } ], "ok" : 1 }
以上就完成了MongoDB shard+replica模式的集群搭建,接下来做业务测试。
-
集群测试
默认情况下,库和集合没有自动分片的,若有数据写入,只会往一个shard中存储,做个测试验证:
bin]$ ./mongo 172.16.16.131:50001 MongoDB shell version: 3.0.7 connecting to: 172.16.16.131:50001/test mongos> use ljaidb switched to db ljaidb mongos> for (var i=1;i<=10000;i++) db.ljaitable.save({"name":"ljai","age":27,"addr":"fuzhou"}) WriteResult({ "nInserted" : 1 }) mongos> db.ljaitable.stats() { "sharded" : false, "primary" : "shard1", "ns" : "ljaidb.ljaitable", "count" : 10000, "size" : 670000, "avgObjSize" : 67, "storageSize" : 49152, "capped" : false, "wiredTiger" : { "metadata" : { "formatVersion" : 1 } mongos> db.printShardingStatus() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5625fc29e3c17fdff8517b73") } shards: { "_id" : "shard1", "host" : "shard1/172.16.16.124:40001,172.16.16.125:40001" } { "_id" : "shard2", "host" : "shard2/172.16.16.125:40002,172.16.16.126:40002" } { "_id" : "shard3", "host" : "shard3/172.16.16.126:40003,172.16.16.131:40003" } { "_id" : "shard4", "host" : "shard4/172.16.16.124:40004,172.16.16.131:40004" } balancer: Currently enabled: yes Currently running: yes Balancer lock taken at Tue Oct 20 2015 21:01:26 GMT+0800 (CST) by DataServer-04:50001:1445330413:1804289383:Balancer:846930886 Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "test", "partitioned" : false, "primary" : "shard1" } { "_id" : "ljaidb", "partitioned" : false, "primary" : "shard1" }
可以看出ljaidb库并没有分片,且数据都在shard1上,登录其他shard1上查看:
bin]$ ./mongo 172.16.16.124:40001 MongoDB shell version: 3.0.7 connecting to: 172.16.16.124:40001/test shard1:PRIMARY> show dbs ljaidb 0.000GB local 0.000GB shard1:PRIMARY> use ljaidb switched to db ljaidb shard1:PRIMARY> show tables ljaitable shard1:PRIMARY> db.ljaitable.find().count() 10000
验证shard2、shard3、shard4上都没有ljaidb这个库:
bin]$ ./mongo 172.16.16.125:40002 MongoDB shell version: 3.0.7 connecting to: 172.16.16.125:40002/test shard2:PRIMARY> show dbs local 0.000GB
-
指定数据库和集合进行分片:
为了让某个数据库与集合自动分片生效,对数据库(lymdb)及数据库(lymtable)中的表进行分片配置:
bin]$ ./mongo 172.16.16.124:50001 MongoDB shell version: 3.0.7 connecting to: 172.16.16.124:50001/test mongos> use admin switched to db admin mongos> db.runCommand( { enablesharding :"lymdb"}); { "ok" : 1 } mongos> db.runCommand( { shardcollection : "lymdb.lymtable",key : {_id: 1} } ) { "collectionsharded" : "lymdb.lymtable", "ok" : 1 }
java连接代码:
1 import java.util.ArrayList; 2 import java.util.List; 3 4 import com.mongodb.BasicDBObject; 5 import com.mongodb.DB; 6 import com.mongodb.DBCollection; 7 import com.mongodb.DBObject; 8 import com.mongodb.MongoClient; 9 import com.mongodb.ServerAddress; 10 11 public class TestMongoDBShards { 12 13 public static void main(String[] args) { 14 try { 15 List<ServerAddress> addresses = new ArrayList<ServerAddress>(); 16 ServerAddress address1 = new ServerAddress("172.16.16.124" , 50001); 17 ServerAddress address2 = new ServerAddress("172.16.16.125" , 50001); 18 ServerAddress address3 = new ServerAddress("172.16.16.126" , 50001); 19 ServerAddress address4 = new ServerAddress("172.16.16.131" , 50001); 20 addresses.add(address1); 21 addresses.add(address2); 22 addresses.add(address3); 23 24 MongoClient client = new MongoClient(addresses); 25 DB db = client.getDB( "lymdb" ); 26 DBCollection coll = db.getCollection( "lymtable" ); 27 28 // BasicDBObject object = new BasicDBObject(); 29 // object.append( "id" , 1); 30 31 // DBObject dbObject = coll.findOne(object); 32 33 for(int i=1;i<=1000000;i++) { 34 DBObject saveData=new BasicDBObject(); 35 saveData.put("id", i); 36 saveData.put("userName", "baiwan" + i); 37 saveData.put("age", "26"); 38 saveData.put("gender", "m"); 39 40 41 coll.save(saveData); 42 } 43 44 45 46 47 // System. out .println(dbObject); 48 49 } catch (Exception e) { 50 e.printStackTrace(); 51 } 52 // TODO Auto-generated method stub 53 54 } 55 56 }
python连接代码:
1 #encoding=UTF-8 2 import datetime 3 4 ISOTIMEFORMAT = '%Y-%m-%d %X' 5 6 from pymongo import MongoClient 7 conn = MongoClient("172.16.16.124",50001) 8 db = conn.funodb 9 def dateDiffInSeconds(date1,date2): 10 timedelta = date2 - date1 11 return timedelta.days*24*3600 +timedelta.seconds 12 db.funotable.drop() 13 date1 = datetime.datetime.now() 14 for i in range(0,1000000): db.funotable.insert({"name":"ljai","age":i,"addr":"fuzhou"}) 15 c = db.funotable.find().count() 16 print("count is ",c) 17 date2 = datetime.datetime.now() 18 print(date1) 19 print(date2) 20 print("消耗:",dateDiffInSeconds(date1,date2),"seconds") 21 conn.close()
-
测试是否自动分片:
mongos> db.lymtable.getShardDistribution() Shard shard1 at shard1/172.16.16.124:40001,172.16.16.125:40001 data : 96.46MiB docs : 1216064 chunks : 4 estimated data per chunk : 24.11MiB estimated docs per chunk : 304016 Shard shard2 at shard2/172.16.16.125:40002,172.16.16.126:40002 data : 44.9MiB docs : 565289 chunks : 4 estimated data per chunk : 11.22MiB estimated docs per chunk : 141322 Shard shard3 at shard3/172.16.16.126:40003,172.16.16.131:40003 data : 99.39MiB docs : 1259979 chunks : 4 estimated data per chunk : 24.84MiB estimated docs per chunk : 314994 Shard shard4 at shard4/172.16.16.124:40004,172.16.16.131:40004 data : 76.46MiB docs : 958668 chunks : 4 estimated data per chunk : 19.11MiB estimated docs per chunk : 239667 Totals data : 317.22MiB docs : 4000000 chunks : 16 Shard shard1 contains 30.4% data, 30.4% docs in cluster, avg obj size on shard : 83B Shard shard2 contains 14.15% data, 14.13% docs in cluster, avg obj size on shard : 83B Shard shard3 contains 31.33% data, 31.49% docs in cluster, avg obj size on shard : 82B Shard shard4 contains 24.1% data, 23.96% docs in cluster, avg obj size on shard : 83B
可以看出,插入400万条数据,都有分布各个shard上,但是不够均匀,需要进一步研究分片的配置。
MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-10/124455.htm
