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)
config 0.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登陆查看分片效果
[iyunv@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
testdb 0.203125GB
> use testdb;
switched to db testdb
> show tables;
system.indexes
testdocument01
> db.testdocument01.count()
5802
#node2登陆登陆查看分片效果
[iyunv@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
testdb 0.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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com