這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)MongoDB中怎么實現(xiàn)分片與集群,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的循化網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
一、 mongodb分片與集群拓?fù)鋱D
二、分片與集群的部署
1.Mongodb的安裝
分別在以上3臺服務(wù)器安裝好mongodb
安裝方法見安裝腳本。
2.Mongod 創(chuàng)建單個分片的副本集
10.68.4.209
①建立數(shù)據(jù)文件夾和日志文件夾
mdkir /data/{master,slave,arbiter}
mkdir /data/log/mongodb/{master,slave,arbiter} -p
②建立配置文件
#master.conf dbpath=/data/master logpath=/data/log/mongodb/master/mongodb.log pidfilepath=/var/run/mongo_master.pid #directoryperdb=true logappend=true replSet=policydb port=10002 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 #slave.conf dbpath=/data/slave logpath=/data/log/mongodb/slave/mongodb.log pidfilepath=/var/run/mongo_slave.pid #directoryperdb=true logappend=true replSet=policydb port=10001 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 #arbiter.conf dbpath=/data/arbiter logpath=/data/log/mongodb/arbiter/mongodb.log pidfilepath=/var/run/mongo_arbiter.pid #directoryperdb=true logappend=true replSet=policydb port=10000 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200
③啟動mongodb
/etc/init.d/mongodb_master start
/etc/init.d/mongodb_slave start
/etc/init.d/mongodb_arbiter start
④配置主、備、仲裁節(jié)點
主節(jié)點:
# /usr/local/mongodb/bin/mongo 10.68.4.209:10002 MongoDB shell version: 2.4.9 connecting to: 10.68.4.209:10002/test > use admin switched to db admin >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" : true } ] } rs.initiate(config) #初始化 rs.status() #查看集群狀態(tài)
10.68.4.29
①建立數(shù)據(jù)文件夾和日志文件夾
mdkir /data/{master,slave,arbiter}
mkdir /data/log/mongodb/{master,slave,arbiter} -p
②建立配置文件
#master.conf dbpath=/data/master logpath=/data/log/mongodb/master/mongodb.log pidfilepath=/var/run/mongo_master.pid #directoryperdb=true logappend=true replSet=policydb2 port=10002 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 #slave.conf dbpath=/data/slave logpath=/data/log/mongodb/slave/mongodb.log pidfilepath=/var/run/mongo_slave.pid #directoryperdb=true logappend=true replSet=policydb2 port=10001 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 #arbiter.conf dbpath=/data/arbiter logpath=/data/log/mongodb/arbiter/mongodb.log pidfilepath=/var/run/mongo_arbiter.pid #directoryperdb=true logappend=true replSet=policydb2 port=10000 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 ③啟動mongodb /etc/init.d/mongodb_master start /etc/init.d/mongodb_slave start /etc/init.d/mongodb_arbiter start
④配置主、備、仲裁節(jié)點
主節(jié)點:
# /usr/local/mongodb/bin/mongo 10.68.4.209:10002 MongoDB shell version: 2.4.9 connecting to: 10.68.4.209:10002/test > use admin switched to db admin > 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" : true } ] } rs.initiate(config) #初始化 rs.status() #查看集群狀態(tài)
10.68.4.30
①建立數(shù)據(jù)文件夾和日志文件夾
mdkir /data/{master,slave,arbiter}
mkdir /data/log/mongodb/{master,slave,arbiter} -p
②建立配置文件
#master.conf dbpath=/data/master logpath=/data/log/mongodb/master/mongodb.log pidfilepath=/var/run/mongo_master.pid #directoryperdb=true logappend=true replSet=policydb3 port=10002 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 #slave.conf dbpath=/data/slave logpath=/data/log/mongodb/slave/mongodb.log pidfilepath=/var/run/mongo_slave.pid #directoryperdb=true logappend=true replSet=policydb3 port=10001 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200 #arbiter.conf dbpath=/data/arbiter logpath=/data/log/mongodb/arbiter/mongodb.log pidfilepath=/var/run/mongo_arbiter.pid #directoryperdb=true logappend=true replSet=policydb3 port=10000 oplogSize=10000 fork=true noprealloc=true profile=1 slowms=200
③啟動mongodb
/etc/init.d/mongodb_master start
/etc/init.d/mongodb_slave start
/etc/init.d/mongodb_arbiter start
④配置主、備、仲裁節(jié)點
主節(jié)點:
# /usr/local/mongodb/bin/mongo 10.68.4.209:10002 MongoDB shell version: 2.4.9 connecting to: 10.68.4.209:10002/test > use admin switched to db admin > 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" : true } ] } rs.initiate(config) #初始化 rs.status() #查看集群狀態(tài)
2.Mongod 創(chuàng)建單個分片的配置服務(wù)器
① 創(chuàng)建配置目錄
10.68.4.209 mkdir /data/config
10.68.4.29 mkdir /data/config
10.68.4.30 mkdir /data/config
②準(zhǔn)備配置服務(wù)器的配置文件
3個服務(wù)器的配置服務(wù)器的配置文件一致
#config.conf dbpath=/data/config logpath=/data/log/mongodb/config/mongodb.log pidfilepath=/var/run/mongo_config.pid directoryperdb=true logappend=true port=10003 fork=true configsvr=true
③啟動配置服務(wù)器
/etc/init.d/mongodb_config start
3.Mongod 創(chuàng)建并配置mongos和開啟分片模式
① 創(chuàng)建日志目錄
Mkdir -p /data/log/mongodb/mongos/
② 準(zhǔn)備mongos的配置文件
#mongos.conf logpath=/data/log/mongodb/mongos/mongodb.log pidfilepath=/var/run/mongo_mongos.pid logappend=true port=10004 fork=true configdb=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() #查看分片狀態(tài) sh.status({verbose:true}) sh.status()
3.快速創(chuàng)建副本集和配置服務(wù)腳本
上圖
1.圖1
2.圖2 config.conf配置文件
3.圖3
上述就是小編為大家分享的MongoDB中怎么實現(xiàn)分片與集群了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。