首页 / MONGODB / MongoDB分片集群
MongoDB分片集群
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MongoDB分片集群,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3890字,纯文字阅读大概需要6分钟。
内容图文
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
#CentOS安装mongo软件包 yum -y install mongodb mongodb-server
#开启配置服务器 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
#服务配置,将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
#创建数据库,如果建的数据库是空的那么将不会生成,所以紧接着在数据库里面建集合 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}});
#至此分片操作全部结束,接下来通过mongos向mongodb插入多条文档,然后通过printShardingStatus命令查看mongodb的数据分片情况主要看三点信息: ① shards: 我们清楚的看到已经别分为两个片了,shard0000和shard0001 ② databases: 这里有个partitioned字段表示是否分区,这里清楚的看到已经分区 ③ chunks: 集合
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)
#循环插入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登陆查看分片效果 [root@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登陆登陆查看分片效果 [root@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
#移除分片 mongos> db.runCommand({"removeshard":"192.168.3.101:27018"});
本文出自 “吴善强” 博客,请务必保留此出处http://shanqiangwu.blog.51cto.com/8067564/1653974
MongoDB分片集群
标签:mongodb分片集群
本文系统来源:http://shanqiangwu.blog.51cto.com/8067564/1653974
内容总结
以上是互联网集市为您收集整理的MongoDB分片集群全部内容,希望文章能够帮你解决MongoDB分片集群所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。