MongoDB sharding模式实现(http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28266791&id=5758139 )
来源:互联网 发布:深圳乐易网络双休吗 编辑:程序博客网 时间:2024/06/05 20:48
环境准备
1.本例使用3台Linux主机,IP地址如下:
一、 点击(此处)折叠或打开
Server A: 192.168.106.101
Server B: 192.168.106.102
Server C: 192.168.106.103
2.根据需要,开启相应主机防火墙的相关端口。本次需要用到3台主机,所以开启这3台主机的以下端口:
点击(此处)折叠或打开
-A INPUT -m state –state NEW -m tcp -p tcp –dport 27017 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 27018 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 27019 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 20000 -j ACCEPT
二、搭建分布式集群
运用MongoDB的复制集(Replica Sets)+分片(Sharding)就能实现MongoDB分布式高可用的集群。
分片(sharding)能够增加更多的机器来应对不断增加的负载和数据,也不影响应用。
复制集(Replica Sets)能确保每个分片节点都具有自动备份、自动故障恢复能力。
1.集群的环境
架构图:
主机设计:
2.配置Replica Sets
创建数据目录:
#在serverA上mkdir -p /data/{shard1-1,shard2-1,config}
#在serverB上mkdir -p /data/{shard1-2,shard2-2,config}
#在serverC上mkdir -p /data/{shard1-3,shard2-3,config}
2.1配置shard1的Replica Sets:
#在serverA上启动shard1-1mongod --fork --shardsvr --port 27018 --dbpath /data/shard1-1/ --logpath /data/shard1-1/shard1-1.log --logappend --replSet shard1 --directoryperdb
#在serverB上启动shard1-2mongod --fork --shardsvr --port 27018 --dbpath /data/shard1-2/ --logpath /data/shard1-2/shard1-2.log --logappend --replSet shard1 --directoryperdb
#在serverC上启动shard1-3mongod --fork --shardsvr --port 27018 --dbpath /data/shard1-3/ --logpath /data/shard1-3/shard1-3.log --logappend --replSet shard1 --directoryperdb
用mongo连接其中一台主机的27018端口的mongod,初始化复制集shard1:
mongo –port 27018
点击(此处)折叠或打开
>config={_id:'shard1',members:[{_id:0,host:'192.168.106.101:27018',priority:2},{_id:1,host:'192.168.106.102:27018',priority:1},{_id:2,host:'192.168.106.103:27018',arbiterOnly:true}]}>rs.initiate(config)
2.2 配置shard2的Replica Sets
#在serverA上启动shard2-1mongod --fork --shardsvr --port 27019 --dbpath /data/shard2-1/ --logpath /data/shard2-1/shard2-1.log --logappend --replSet shard2 --directoryperdb
#在serverB上启动shard2-2mongod --fork --shardsvr --port 27019 --dbpath /data/shard2-2/ --logpath /data/shard2-2/shard2-2.log --logappend --replSet shard2 --directoryperdb
#在serverC上启动shard2-3mongod --fork --shardsvr --port 27019 --dbpath /data/shard2-3/ --logpath /data/shard2-3/shard2-3.log --logappend --replSet shard2 --directoryperdb
用mongo连接其中一台主机(建议在ServerB上)的27019端口的mongod,初始化复制集shard2:
mongo --port 27019>config={_id:'shard2',members:[{_id:0,host:'192.168.106.101:27019',arbiterOnly:true},{_id:1,host:'192.168.106.102:27019',priority:1},{_id:2,host:'192.168.106.103:27019',priority:2}]}>rs.initiate(config)
3.配置config server
#在3台主机中分别启动配置服务mongod --configsvr --port 20000 --dbpath /data/config/ --logpath /data/config/config.log --replSet docdetection --logappend --fork
用mongo连接其中一台主机的20000端口的mongod,初始化复制集docdetection:
mongo --port 20000configdb1={_id:'docdetection',members:[{_id:0,host:'192.168.106.101:20000',priority:3},{_id:1,host:'192.168.106.102:20000',priority:1},{_id:2,host:'192.168.106.103:20000',priority:2}]}>rs.initiate(configdb1)
4.配置router server
#在3台主机中分别运行mongos服务mongos --configdb docdetection/192.168.106.101:20000,192.168.106.102:20000,192.168.106.103:20000 --port 27017 --logpath /data/mongos.log --logappend --fork
5.配置分片(shard cluster)
#连接到其中一台机器的端口27017的mongos进程,并切换到admin数据库添加分片shard1和shard2:mongo --port 27017>use admin>db.runCommand({addshard:"shard1/192.168.106.101:27018,192.168.106.102:27018,192.168.106.103:27018"})>db.runCommand({addshard:"shard2/192.168.106.101:27019,192.168.106.102:27019,192.168.106.103:27019"})
#激活数据库(work)和集合(status)的分片功能:>db.runCommand({enablesharding:"work"})>db.runCommand({shardcollection:"work.status",key:{_id:1}})将准备好的mongodb的数据导入到mongodb中cd /usr/local/mongodb/binmongorestore -h localhost:27017 -d docdetection --dir /usr/local/mongodb/docdetection/
6.验证
#查看shard1的集合work.status和shard2的文档数几乎均分,且之和等于路由节点下查看的文档数:mongo --port 27018 work>db.status.count()./mongo --port 27019 work>db.status.count()./mongo work>db.status.count()#查看分片的状态:需要在mongs进程下访问:mongo --port 27017> printShardingStatus()
三、常见问题
1.【问题描述】执行初始化副本集时报错:
点击(此处)折叠或打开
> rs.initiate(config){ "ok" : 0, "errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: 192.168.106.102:27018 failed with No route to host, 192.168.106.103:27018 failed with No route to host", "code" : 74, "codeName" : "NodeNotFound"}
【解决方法】
开启防火墙的相关端口
点击(此处)折叠或打开
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 27018 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 27019 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 20000 -j ACCEPT
2.【问题描述】初始化副本集报错:
点击(此处)折叠或打开
> rs.initiate(config){ "ok" : 0, "errmsg" : "This node, 192.168.106.101:27019, with _id 0 is not electable under the new configuration version 1 for replica set shard2", "code" : 93, "codeName" : "InvalidReplicaSetConfig"}
【解决方法】
如果设置主机A为仲裁节点,那么不要在A主机上执行初始化操作,否则会报上述错误。
3.【问题描述】启动路由器mongos报错:
点击(此处)折叠或打开
./mongos --configdb 192.168.106.101:20000,192.168.106.102:20000,192.168.106.103:20000 --port 27017 --logpath /data/mongos.log --logappend --forkFailedToParse: mirrored config server connections are not supported; for config server replica sets be sure to use the replica set connection stringtry './mongos --help' for more information
【解决方法】
从Mongodb3.2之后,启动mongos时需使用副本集名称,否则会报上述错误。例如:
点击(此处)折叠或打开
./mongos –configdb 副本集名称/192.168.106.101:20000,192.168.106.102:20000,192.168.106.103:20000 --port 27017 --logpath /data/mongos.log --logappend --fork
- MongoDB sharding模式实现(http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28266791&id=5758139 )
- http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=22891521&id=2109284
- 函数模板(Function Template)及带默认参数的函数 http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=25520556&id
- ambari,hadoop本地源 部署http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26230811&id=4023821
- 进程,轻量级进程,内核线程,用户线程的区别关系http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=27025492&id=3441601
- 进程间通信--popen函数和pclose函数blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=25940216&id=3206312
- ORACLE中的物化视图(原文链接http://blog.chinaunix.net/space.php?uid=8329266&do=blog&id=2032099)
- 50个c/c++源代码网站(转自http://blog.chinaunix.net/space.php?uid=22830296&do=blog&id=1768388)
- http://blog.chinaunix.net/space.php?uid=22028566&do=blog&frmd=71047&view=me wanshilin
- vim tab总结 http://blog.chinaunix.net/space.php?uid=16444831&do=blog&cuid=1137430
- http://blog.chinaunix.net/uid-22666718-id-1771703.html
- http://blog.chinaunix.net/uid-20322254-id-145835.html
- http://blog.chinaunix.net/uid-25547034-id-3155778.html
- http://blog.chinaunix.net/uid-25750954-id-2956084.html
- http://blog.chinaunix.net/uid-22342564-id-3053393.html
- http://blog.chinaunix.net/uid-25082381-id-3242162.html
- http://blog.chinaunix.net/uid-25737580-id-3182286.html
- http://blog.chinaunix.net/uid-25835268-id-3055356.html--makefile
- bugdone使用
- Spring--4.bean管理(xml)
- js创建对象几种方式
- c++类指针
- 阿里发布的规约插件
- MongoDB sharding模式实现(http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28266791&id=5758139 )
- Altium Designer 16快捷键列表
- 设计模式【状态模式State Pattern】
- 监听、控制软键盘的弹出和收起
- Init进程笔记
- altera FPGA介紹
- Fiddler 手机抓包步骤
- android4.4编译失败,出现“virtual memory exhausted: Cannot allocate memory”
- 如何用命令开启相机