rgar12 发表于 2015-5-25 10:37:08

MongoDB分片集群

Mongodb Sharding分片集群
OS                      CentOS6.5
192.168.3.100       server1   configport=27017   
192.168.3.100       server1   mongosport=27018
192.168.3.101       node1       mongodport=27018
192.168.3.102       node2       mongodport=27018


1
2
#CentOS安装mongo软件包
yum -y install mongodb mongodb-server





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#开启配置服务器
mkdir /mongod
mongod --dbpath=/mongod --logpath=/mongod/config.log --port=27017 --fork

#路由服务器启动
mkdir /mongos
mongos --configdb=192.168.3.100:27017 --logpath=/mongos/mongos.log --port=27018 --fork

#启动mongod分片服务器,也就是添加片,端口为:27018,27018
#node1配置
mkdir /mongo1
mongod --dbpath=/mongo1 --logpath=/mongo1/mongo1.log --port=27018 --fork

#node2配置
mkdir /mongo2
mongod --dbpath=/mongo2 --logpath=/mongo2/mongo2.log --port=27018 --fork





1
2
3
4
5
6
7
8
9
10
11
#服务配置,将27019,27020的mongod交给mongos,添加分片也就是addshard()
mongo 192.168.3.100:27018/admin
mongos> 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





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#创建数据库,如果建的数据库是空的那么将不会生成,所以紧接着在数据库里面建集合
mongos> use testdb
mongos> db.testdocument01.insert({"name":"wsq","id":1,"address":"gaugnzhou","mobile":"13838383830","sex":"M"});
mongos> show tables;      
system.indexes
testdocument01
mongos> db.testdocument01.find();
{ "_id" : ObjectId("55114e25f00c7c97c710566e"), "name" : "wsq", "id" : 1, "address" : "guangzhou", "mobile" : "13838383830", "sex" : "M" }

#开启分片功能需要进入admin集合当中操作,开启分片功能
mongos> use admin      
mongos> db.runCommand({"enablesharding":"testdb"});

#指定集合分片主键,这里指定为testdocument01.id字段
mongos> db.runCommand({"shardcollection":"testdb.testdocument01","key":{"_id":1}});





1
2
3
4
#至此分片操作全部结束,接下来通过mongos向mongodb插入多条文档,然后通过printShardingStatus命令查看mongodb的数据分片情况主要看三点信息:
  ① shards: 我们清楚的看到已经别分为两个片了,shard0000和shard0001
  ② databases: 这里有个partitioned字段表示是否分区,这里清楚的看到已经分区
  ③ chunks: 集合





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.testdocument01
            shard key: { "_id" : 1 }
            chunks:
                shard0000   1
            { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)





1
2
#循环插入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"})};





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#node1登陆查看分片效果
# mongo 192.168.3.101:27018
MongoDB shell version: 2.4.12
connecting to: 127.0.0.1:27018/test
> show dbs;                              
local   0.078125GB
testdb0.203125GB
> use testdb;                        
switched to db testdb
> show tables;                        
system.indexes
testdocument01
> db.testdocument01.count()
5802
#node2登陆登陆查看分片效果
# mongo 192.168.3.102:27018
MongoDB shell version: 2.4.12
connecting to: 127.0.0.1:27018/test
> show dbs;                              
local   0.078125GB
testdb0.203125GB
> use testdb;                        
switched to db testdb
> show tables;                        
system.indexes
testdocument01
> db.testdocument01.count()
4199





1
2
#移除分片
mongos> db.runCommand({"removeshard":"192.168.3.101:27018"});



页: [1]
查看完整版本: MongoDB分片集群