Mongodb sharding Shard cluster OS centos6.5192.168.3.100server1configport=27017192.168.3.100server1mongosport=27 018192.168.3.101node1mongodport=27018192.168.3.102node2mongodport=27018
#CentOS安装mongo软件包yum-y Install MongoDB mongodb-server
#开启配置服务器mkdir /mongodmongod --dbpath=/mongod --logpath=/ mongod/config.log --port=27017 --fork# Routing Server Startup mkdir /mongosmongos --configdb= 192.168.3.100:27017 --logpath=/mongos/mongos.log --port=27018 --fork# starts the Mongod shard server, which is the add slice, Port: 27018,27018#node1 configuration mkdir /mongo1mongod --dbpath=/mongo1 --logpath=/mongo1/mongo1.log - -port=27018 --fork#node2 Configuration mkdir /mongo2mongod --dbpath=/mongo2 --logpath=/mongo2/ Mongo2.log --port=27018 --fork
#服务配置, 27019,27020 Mongod to MONGOs, add Shard is Addshard () mongo 192.168.3.100:27018/adminmongos> db.runcommand ({addshard: "192.168.3.101:27018"}); { "shardadded" : "shard0000", "OK" : 1 }mongos> db.runcommand ({ Addshard: "192.168.3.102:27018"}); { "shardadded" : "shard0001", "OK" : 1 } #查看数据库mongos > show dbs ; admin (empty) CONFIG0.1875GB
#创建数据库, if the database being built is empty then it will not be generated, so the collection is built in the database immediately mongos> use testdb mongos> db.testdocument01.insert ({"Name": "WSQ", "id": 1, "Address": "Gaugnzhou", "mobile": " 13838383830 "," Sex ":" M "});mongos> show tables;system.indexestestdocument01mongos> Db.testdocument01.find (); { "_id" : objectid ("55114e25f00c7c97c710566e"), "name" : "WSQ", "id"  : 1, "Address" : "Guangzhou", "mobile" : "13838383830", "Sex"  : "M"  } #开启分片功能需要进入admin集合当中操作, turn on the Shard function Mongos> use adminmongos> db.runcommand ({" Enablesharding ":" TestDB "}), #指定集合分片主键, specified here as Testdocument01.id field Mongos> db.runcommand ({" Shardcollection ": "testdb.testdocument01", "key": {"_id": 1}});
#至此分片操作全部结束, then insert multiple documents through MONGOs to MongoDB, And then through the Printshardingstatus command to view MongoDB data fragmentation situation mainly see three points of information:① shards: we clearly see has not been divided into two pieces, shard0000 and Shard0001② databases: Here's a partitioned field that indicates whether the partition is partitioned, so it's clear to see that the partitioned ③ chunks: collection is
Mongos> use testdb;mongos> db.printshardingstatus (); #查看分片信息--- sharding status --- sharding version: {"_id" : 1, "version" : 3, " Mincompatibleversion " : 3," CurrentVersion " : 4," Clusterid " : objectid (" 555f5b7d30c41ebb264764b1 ")} shards:{ " _id " : " shard0000 ", " host " : "192.168.3.101:27018" }{ "_id" : "shard0001", "host"  : "192.168.3.102:27018" } databases:{ "_id" : "admin", " Partitioned " : false, " primary " : " config " }{ " _id " : "TestDB", "partitioned" : true, "primary" : "shard0000"  } testdb.testdocument01shard key: { "_id" : 1 }chunks:shard00001{ "_id" : { "$minKey" : 1&nbSP;} } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 timestamp (1, 0)
#循环插入10000条文档mongos > for (var i=0;i<10000;i++) { Db.testdocument01.insert ({"Name": "WSQ" +i, "id": 2+i, "Address": "Guangzhou", "mobile": 13838383830+i, "Sex": "M"})};
#node1登陆查看分片效果 [[email protected]/]# MONGO 192.168.3.101:27018mongodb shell version:2.4.12connecting to: 127.0.0.1:27018/test> Show dbs;local0.078125gbtestdb0.203125gb> use testdb;switched to DB testdb> show tables; System.indexestestdocument01> Db.testdocument01.count () 5802#node2 Landing login View Shard effect [[email protected]/]# MONGO 192.168.3.102:27018mongodb Shell version:2.4.12connecting to:127.0.0.1:27018/test> show DBS; Local0.078125gbtestdb0.203125gb> use testdb;switched to DB testdb> show tables;system.indexestestdocument01> Db.testdocument01.count (4199)
#移除分片mongos > Db.runcommand ({"Removeshard": "192.168.3.101:27018"});
This article is from the "Wu Shanqiang" blog, make sure to keep this source http://shanqiangwu.blog.51cto.com/8067564/1653974
MongoDB Shard Cluster