生產(chǎn)MongoDB分片與集群方案.docx
生產(chǎn)MongoDB 分片與集群方案【編者的話(huà)】Mongo DB 是目前在IT行業(yè)非常流行的一種非關(guān)系型數(shù)據(jù)庫(kù)(NoSql),其靈活的數(shù)據(jù)存儲(chǔ)方式備受當(dāng)前IT從業(yè)人員的青睞。Mongo DB很好的實(shí)現(xiàn)了面向?qū)ο蟮乃枷?OO思想),在Mongo DB中 每一條記錄都是一個(gè)Document對(duì)象。本文介紹了一則生產(chǎn)環(huán)境下MongoDB實(shí)現(xiàn)分片與集群方案的操作實(shí)例。一、 mongodb分片與集群拓?fù)鋱D二、分片與集群的部署1.Mongodb的安裝分別在以上3臺(tái)服務(wù)器安裝好mongodb安裝方法見(jiàn)安裝腳本。2.Mongod 創(chuàng)建單個(gè)分片的副本集10.68.4.209建立數(shù)據(jù)文件夾和日志文件夾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啟動(dòng)mongodb/etc/init.d/mongodb_master start/etc/init.d/mongodb_slave start/etc/init.d/mongodb_arbiter start配置主、備、仲裁節(jié)點(diǎn)主節(jié)點(diǎn):#/usr/local/mongodb/bin/mongo10.68.4.209:10002MongoDBshellversion:2.4.9connectingto:10.68.4.209:10002/test>useadminswitchedtodbadmin>config=_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()#查看集群狀態(tài)10.68.4.29建立數(shù)據(jù)文件夾和日志文件夾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啟動(dòng)mongodb/etc/init.d/mongodb_masterstart/etc/init.d/mongodb_slavestart/etc/init.d/mongodb_arbiterstart配置主、備、仲裁節(jié)點(diǎn)主節(jié)點(diǎn):#/usr/local/mongodb/bin/mongo10.68.4.209:10002MongoDBshellversion:2.4.9connectingto:10.68.4.209:10002/test>useadminswitchedtodbadmin>config=_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()#查看集群狀態(tài)10.68.4.30建立數(shù)據(jù)文件夾和日志文件夾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啟動(dòng)mongodb/etc/init.d/mongodb_master start/etc/init.d/mongodb_slave start/etc/init.d/mongodb_arbiter start配置主、備、仲裁節(jié)點(diǎn)主節(jié)點(diǎn):#/usr/local/mongodb/bin/mongo10.68.4.209:10002MongoDBshellversion:2.4.9connectingto:10.68.4.209:10002/test>useadminswitchedtodbadmin>config=_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()#查看集群狀態(tài)2.Mongod 創(chuàng)建單個(gè)分片的配置服務(wù)器 創(chuàng)建配置目錄10.68.4.209 mkdir /data/config10.68.4.29 mkdir /data/config10.68.4.30 mkdir /data/config準(zhǔn)備配置服務(wù)器的配置文件3個(gè)服務(wù)器的配置服務(wù)器的配置文件一致#config.confdbpath=/data/configlogpath=/data/log/mongodb/config/mongodb.logpidfilepath=/var/run/mongo_config.piddirectoryperdb=truelogappend=trueport=10003fork=trueconfigsvr=true啟動(dòng)配置服務(wù)器/etc/init.d/mongodb_config start3.Mongod 創(chuàng)建并配置mongos和開(kāi)啟分片模式 創(chuàng)建日志目錄Mkdir -p /data/log/mongodb/mongos/ 準(zhǔn)備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 啟動(dòng)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()#查看分片狀態(tài)sh.status(verbose:true)sh.status()3.快速創(chuàng)建副本集和配置服務(wù)腳本上圖1.圖12.圖2 config.conf配置文件3.圖3附mongodb一鍵安裝腳本:http:/pan.baidu.com/s/1c0zvP7M附mongodb副本集和配置服務(wù)器一鍵配置腳本:http:/pan.baidu.com/s/1GuQ0A博文出處:http:/my.oschina.net/pwd/blog/411439【編輯推薦】為什么選擇使用NoSQL數(shù)據(jù)庫(kù)開(kāi)發(fā)如此困難? 軟件公司為何要放棄MongoDB? NoSQL詳解:如何找到對(duì)的技術(shù) 為什么需要選用NoSQL? 谷歌新款高性能NoSQL數(shù)據(jù)庫(kù) 支持HBase接口