mongodb复制集+分片生产环境实践
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mongodb复制集+分片生产环境实践,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7117字,纯文字阅读大概需要11分钟。
内容图文
![mongodb复制集+分片生产环境实践](/upload/InfoBanner/zyjiaocheng/490/1142d71730d94751b2f11b138213e9cb.jpg)
node1机器上操作配置复制集
[mongodb@node1 config]$ mongo --port 10001 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10001/test > use admin switched to db admin
> config = { _id:"shard1_zxl", members:[ ... ... {_id:0,host:"192.168.42.41:10001"}, ... ... {_id:1,host:"192.168.42.42:10001"}, ... ... {_id:2,host:"192.168.42.43:10001",arbiterOnly:true} ... ... ] ... ... }
{ "_id" : "shard1_zxl", "members" : [ { "_id" : 0, "host" : "192.168.42.41:10001" }, { "_id" : 1, "host" : "192.168.42.42:10001" }, { "_id" : 2, "host" : "192.168.42.43:10001", "arbiterOnly" : true } ] }
> rs.initiate(con config connect( connectionURLTheSame( constructor > rs.initiate(config) { "ok" : 1 }
node2机器上操作配置复制集
[mongodb@node2 config]$ mongo --port 10002 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10002/test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user > use admin switched to db admin
> config = { _id:"shard2_zxl", members:[ ... ... {_id:0,host:"192.168.42.42:10002"}, ... ... {_id:1,host:"192.168.42.43:10002"}, ... ... {_id:2,host:"192.168.42.41:10002",arbiterOnly:true} ... ... ] ... ... } { "_id" : "shard2_zxl", "members" : [ { "_id" : 0, "host" : "192.168.42.42:10002" }, { "_id" : 1, "host" : "192.168.42.43:10002" }, { "_id" : 2, "host" : "192.168.42.41:10002", "arbiterOnly" : true } ] }
> rs.initiate(config) { "ok" : 1 }
node3机器上操作配置复制集
[mongodb@node3 config]$ mongo --port 10003 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10003/test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user > use admin switched to db admin
> config = {_id:"shard3_zxl", members:[ ... ... {_id:0,host:"192.168.42.43:10003"}, ... ... {_id:1,host:"192.168.42.41:10003"}, ... ... {_id:2,host:"192.168.42.42:10003",arbiterOnly:true} ... ... ] ... ... } { "_id" : "shard3_zxl", "members" : [ { "_id" : 0, "host" : "192.168.42.43:10003" }, { "_id" : 1, "host" : "192.168.42.41:10003" }, { "_id" : 2, "host" : "192.168.42.42:10003", "arbiterOnly" : true } ] }
> rs.initiate(config) { "ok" : 1 }
注:以上是配置rs复制集,相关命令如:rs.status(),查看各个复制集的状况
启动三台机器上的configsvr和mongos节点
[mongodb@node1 logs]$ mongod -f /data/config/configsvr.conf about to fork child process, waiting until server is ready for connections. forked process: 6317 child process started successfully, parent exiting
[mongodb@node1 logs]$ mongos -f /data/config/mongos.conf about to fork child process, waiting until server is ready for connections. forked process: 6345 child process started successfully, parent exiting
查看端口是否启动:
[mongodb@elk_node1 ~]$ ss -atunlp|grep mong tcp LISTEN 0 128 *:10001 *:* users:(("mongod",2630,6)) tcp LISTEN 0 128 *:10002 *:* users:(("mongod",2654,6)) tcp LISTEN 0 128 *:10003 *:* users:(("mongod",2678,6)) tcp LISTEN 0 128 *:10004 *:* users:(("mongod",3053,6)) tcp LISTEN 0 128 *:10005 *:* users:(("mongos",3157,21))
配置shard分片
在node1机器上配置shard分片
[mongodb@node1 config]$ mongo --port 10005 MongoDB shell version: 3.2.3 connecting to: 127.0.0.1:10005/test mongos> use admin switched to db admin
mongos> db.runCommand({addshard:"shard1_zxl/192.168.42.41:10001,192.168.42.42:10001,192.168.42.43:10001"}); { "shardAdded" : "shard1_zxl", "ok" : 1 }
mongos> db.runCommand({addshard:"shard2_zxl/192.168.42.41:10002,192.168.42.42:10002,192.168.42.43:10002"}); { "shardAdded" : "shard2_zxl", "ok" : 1 }
mongos> db.runCommand({addshard:"shard3_zxl/192.168.42.41:10003,192.168.42.42:10003,192.168.42.43:10003"}); { "shardAdded" : "shard3_zxl", "ok" : 1 }
查看shard信息
mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("56de6f4176b47beaa9c75e9d") } shards: { "_id" : "shard1_zxl", "host" : "shard1_zxl/192.168.42.41:10001,192.168.42.42:10001" } { "_id" : "shard2_zxl", "host" : "shard2_zxl/192.168.42.42:10002,192.168.42.43:10002" } { "_id" : "shard3_zxl", "host" : "shard3_zxl/192.168.42.41:10003,192.168.42.43:10003" } active mongoses: "3.2.3" : 3 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases:
查看分片状态
mongos> db.runCommand( {listshards : 1 } ) { "shards" : [ { "_id" : "shard1_zxl", "host" : "shard1_zxl/192.168.42.41:10001,192.168.42.42:10001" }, { "_id" : "shard2_zxl", "host" : "shard2_zxl/192.168.42.42:10002,192.168.42.43:10002" }, { "_id" : "shard3_zxl", "host" : "shard3_zxl/192.168.42.41:10003,192.168.42.43:10003" } ], "ok" : 1 }
启用shard分片的库名字为‘zxl‘,即为库
mongos> sh.enableSharding("zxl") { "ok" : 1 }
设置集合的名字以及字段,默认自动建立索引,zxl库,haha集合
mongos> sh.shardCollection("zxl.haha",{age: 1, name: 1}) { "collectionsharded" : "zxl.haha", "ok" : 1 }
模拟在haha集合中插入10000数据
mongos> for (i=1;i<=10000;i++) db.haha.insert({name: "user"+i, age: (i%150)}) WriteResult({ "nInserted" : 1 })
可以使用上面mongos> sh.status()命令查看各个shard分片情况,以上就是复制集和shard分片搭建完成。
结果如下:
mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("56de6f4176b47beaa9c75e9d") } shards: { "_id" : "shard1_zxl", "host" : "shard1_zxl/192.168.42.41:10001,192.168.42.42:10001" } { "_id" : "shard2_zxl", "host" : "shard2_zxl/192.168.42.42:10002,192.168.42.43:10002" } { "_id" : "shard3_zxl", "host" : "shard3_zxl/192.168.42.41:10003,192.168.42.43:10003" } active mongoses: "3.2.3" : 3 balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: 2 : Success databases: { "_id" : "zxl", "primary" : "shard3_zxl", "partitioned" : true } zxl.haha
shard key: { "age" : 1, "name" : 1 } unique: false balancing: true chunks: shard1_zxl1 shard2_zxl1 shard3_zxl1 { "age" : { "$minKey" : 1 }, "name" : { "$minKey" : 1 } } -->> { "age" : 2, "name" : "user2" } on : shard1_zxl Timestamp(2, 0) { "age" : 2, "name" : "user2" } -->> { "age" : 22, "name" : "user22" } on : shard2_zxl Timestamp(3, 0) { "age" : 22, "name" : "user22" } -->> { "age" : { "$maxKey" : 1 }, "name" : { "$maxKey" : 1 } } on : shard3_zxl Timestamp(3, 1)
以上就是mongodb3.2复制集和shard分片搭建就此完成.有什么疑问可以给我留言,看到第一时间解决。
mongodb复制集+分片生产环境实践
标签:mongodb 复制集 分片
本文系统来源:http://limaomao.blog.51cto.com/12623570/1961957
内容总结
以上是互联网集市为您收集整理的mongodb复制集+分片生产环境实践全部内容,希望文章能够帮你解决mongodb复制集+分片生产环境实践所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。