资源描述
生产MongoDB 分片与集群方案【编者的话】Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。本文介绍了一则生产环境下MongoDB实现分片与集群方案的操作实例。一、 mongodb分片与集群拓扑图二、分片与集群的部署1.Mongodb的安装分别在以上3台服务器安装好mongodb安装方法见安装脚本。2.Mongod 创建单个分片的副本集10.68.4.209建立数据文件夹和日志文件夹mdkir /data/master,slave,arbitermkdir /data/log/mongodb/master,slave,arbiter -p建立配置文件#master.confdbpath=/data/masterlogpath=/data/log/mongodb/master/mongodb.logpidfilepath=/var/run/mongo_master.pid#directoryperdb=truelogappend=truereplSet=policydbport=10002oplogSize=10000fork=truenoprealloc=trueprofile=1slowms=200#slave.confdbpath=/data/slavelogpath=/data/log/mongodb/slave/mongodb.logpidfilepath=/var/run/mongo_slave.pid#directoryperdb=truelogappend=truereplSet=policydbport=10001oplogSize=10000fork=truenoprealloc=trueprofile=1slowms=200#arbiter.confdbpath=/data/arbiterlogpath=/data/log/mongodb/arbiter/mongodb.logpidfilepath=/var/run/mongo_arbiter.pid#directoryperdb=truelogappend=truereplSet=policydbport=10000oplogSize=10000fork=truenoprealloc=trueprofile=1slowms=200启动mongodb/etc/init.d/mongodb_master start/etc/init.d/mongodb_slave start/etc/init.d/mongodb_arbiter start配置主、备、仲裁节点主节点:#/usr/local/mongodb/bin/mongo10.68.4.209:10002MongoDBshellversion:2.4.9connectingto:10.68.4.209:10002/testuseadminswitchedtodbadminconfig=_id:policydb,members:_id:0,host:10.68.4.209:10002,priority:2,_id:1,host:10.68.4.209:10001,priority:1,._id:2,host:10.68.4.209:10000,arbiterOnly:true;_id:policydb,members:_id:0,host:10.68.4.209:10002,priority:2,_id:1,host:10.68.4.209:10001,priority:1,_id:2,host:10.68.4.209:10000,arbiterOnly:truers.initiate(config)#初始化rs.status()#查看集群状态10.68.4.29建立数据文件夹和日志文件夹mdkir /data/master,slave,arbitermkdir /data/log/mongodb/master,slave,arbiter -p建立配置文件#master.confdbpath=/data/masterlogpath=/data/log/mongodb/master/mongodb.logpidfilepath=/var/run/mongo_master.pid#directoryperdb=truelogappend=truereplSet=policydb2port=10002oplogSize=10000fork=truenoprealloc=trueprofile=1slowms=200#slave.confdbpath=/data/slavelogpath=/data/log/mongodb/slave/mongodb.logpidfilepath=/var/run/mongo_slave.pid#directoryperdb=truelogappend=truereplSet=policydb2port=10001oplogSize=10000fork=truenoprealloc=trueprofile=1slowms=200#arbiter.confdbpath=/data/arbiterlogpath=/data/log/mongodb/arbiter/mongodb.logpidfilepath=/var/run/mongo_arbiter.pid#directoryperdb=truelogappend=truereplSet=policydb2port=10000oplogSize=10000fork=truenoprealloc=trueprofile=1slowms=200启动mongodb/etc/init.d/mongodb_masterstart/etc/init.d/mongodb_slavestart/etc/init.d/mongodb_arbiterstart配置主、备、仲裁节点主节点:#/usr/local/mongodb/bin/mongo10.68.4.209:10002MongoDBshellversion:2.4.9connectingto:10.68.4.209:10002/testuseadminswitchedtodbadminconfig=_id:policydb2,members:_id:0,host:10.68.4.29:10002,priority:2,_id:1,host:10.68.4.29:10001,priority:1,._id:2,host:10.68.4.209:10000,arbiterOnly:true;_id:policydb,members:_id:0,host:10.68.4.29:10002,priority:2,_id:1,host:10.68.4.29:10001,priority:1,_id:2,host:10.68.4.29:10000,arbiterOnly:truers.initiate(config)#初始化rs.status()#查看集群状态10.68.4.30建立数据文件夹和日志文件夹mdkir /data/master,slave,arbitermkdir /data/log/mongodb/master,slave,arbiter -p建立配置文件#master.confdbpath=/data/masterlogpath=/data/log/mongodb/master/mongodb.logpidfilepath=/var/run/mongo_master.pid#directoryperdb=truelogappend=truereplSet=policydb3port=10002oplogSize=10000fork=truenoprealloc=trueprofile=1slowms=200#slave.confdbpath=/data/slavelogpath=/data/log/mongodb/slave/mongodb.logpidfilepath=/var/run/mongo_slave.pid#directoryperdb=truelogappend=truereplSet=policydb3port=10001oplogSize=10000fork=truenoprealloc=trueprofile=1slowms=200#arbiter.confdbpath=/data/arbiterlogpath=/data/log/mongodb/arbiter/mongodb.logpidfilepath=/var/run/mongo_arbiter.pid#directoryperdb=truelogappend=truereplSet=policydb3port=10000oplogSize=10000fork=truenoprealloc=trueprofile=1slowms=200启动mongodb/etc/init.d/mongodb_master start/etc/init.d/mongodb_slave start/etc/init.d/mongodb_arbiter start配置主、备、仲裁节点主节点:#/usr/local/mongodb/bin/mongo10.68.4.209:10002MongoDBshellversion:2.4.9connectingto:10.68.4.209:10002/testuseadminswitchedtodbadminconfig=_id:policydb3,members:_id:0,host:10.68.4.30:10002,priority:2,_id:1,host:10.68.4.30:10001,priority:1,._id:2,host:10.68.4.30:10000,arbiterOnly:true;_id:policydb,members:_id:0,host:10.68.4.30:10002,priority:2,_id:1,host:10.68.4.30:10001,priority:1,_id:2,host:10.68.4.30:10000,arbiterOnly:truers.initiate(config)#初始化rs.status()#查看集群状态2.Mongod 创建单个分片的配置服务器 创建配置目录10.68.4.209 mkdir /data/config10.68.4.29 mkdir /data/config10.68.4.30 mkdir /data/config准备配置服务器的配置文件3个服务器的配置服务器的配置文件一致#config.confdbpath=/data/configlogpath=/data/log/mongodb/config/mongodb.logpidfilepath=/var/run/mongo_config.piddirectoryperdb=truelogappend=trueport=10003fork=trueconfigsvr=true启动配置服务器/etc/init.d/mongodb_config start3.Mongod 创建并配置mongos和开启分片模式 创建日志目录Mkdir -p /data/log/mongodb/mongos/ 准备mongos的配置文件#mongos.conflogpath=/data/log/mongodb/mongos/mongodb.logpidfilepath=/var/run/mongo_mongos.pidlogappend=trueport=10004fork=trueconfigdb=10.68.4.209:10003,10.68.4.29:10003,10.68.4.30:1000 启动mongos/etc/init.d/mongodb_mongos start 配置分片sh.addShard(policydb/10.68.4.209:10002)sh.addShard(policydb2/10.68.4.29:10002)sh.addShard(policydb3/10.68.4.30:10002)sh.enableSharding(policydb)db.runCommand(shardcollection:policydb.fullPolicyTextInfo_history,key:key:1)db.printShardingStatus()#查看分片状态sh.status(verbose:true)sh.status()3.快速创建副本集和配置服务脚本上图1.图12.图2 config.conf配置文件3.图3附mongodb一键安装脚本:http:/pan.baidu.com/s/1c0zvP7M附mongodb副本集和配置服务器一键配置脚本:http:/pan.baidu.com/s/1GuQ0A博文出处:http:/my.oschina.net/pwd/blog/411439【编辑推荐】为什么选择使用NoSQL数据库开发如此困难? 软件公司为何要放弃MongoDB? NoSQL详解:如何找到对的技术 为什么需要选用NoSQL? 谷歌新款高性能NoSQL数据库 支持HBase接口
展开阅读全文