Mongodb集群搭建

共2臺服務(wù)器,每臺上面各一個路由,一個配置,一個分片,MongoDB版本3.4

太子河ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!

服務(wù)器1:10.2.4.214

服務(wù)器2:10.2.4.215

1.首先編寫2臺服務(wù)器的配置文件,并將配置文件中的路徑文件夾建立好

214的路由配置文件mongodb_rout.conf:

net:
???port:?5000
???ipv6:?true
systemLog:
???destination:?file
???path:?"/data/mongodb/formal_5000/log/route.log"
???logAppend:?true
processManagement:
???fork:?true
sharding:
???configDB:?rsConf/10.2.4.214:5100,10.2.4.215:5100
...

214的配置配置文件mongodb_cfg.conf:

net:
???port:?5100
???ipv6:?true
storage:
???dbPath:?"/data/mongodb/formal_5000/data/config"
???directoryPerDB:?true
systemLog:
???destination:?file
???path:?"/data/mongodb/formal_5000/log/config.log"
???logAppend:?true
storage:
???journal:
??????enabled:?true
processManagement:
???fork:?true
sharding:
???clusterRole:?configsvr
replication:
???replSetName:?rsConf
...

214的分片配置文件mongodb_s0.conf:

storage:
???dbPath:?"/data/mongodb/formal_5000/data/s0"
???directoryPerDB:?true
systemLog:
???destination:?file
???path:?"/data/mongodb/formal_5000/log/s0.log"
???logAppend:?true
net:
???port:?5010
???ipv6:?true
processManagement:
???fork:?true
storage:
???journal:
??????enabled:?true
replication:
???replSetName:?rsShardA
sharding:	
???clusterRole:?shardsvr
...

215的路由配置文件mongodb_rout.conf:

net:
???port:?5000
???ipv6:?true
systemLog:
???destination:?file
???path:?"/data/mongodb/formal_5000/log/route.log"
???logAppend:?true
processManagement:
???fork:?true
sharding:
???configDB:?rsConf/10.2.4.214:5100,10.2.4.215:5100
...

215的配置配置文件mongodb_cfg.conf:

net:
???port:?5100
???ipv6:?true
storage:
???dbPath:?"/data/mongodb/formal_5000/data/config"
???directoryPerDB:?true
systemLog:
???destination:?file
???path:?"/data/mongodb/formal_5000/log/config.log"
???logAppend:?true
storage:
???journal:
??????enabled:?true
processManagement:
???fork:?true
sharding:
???clusterRole:?configsvr
replication:
???replSetName:?rsConf
...

215的分片配置文件mongodb_s0.conf:

storage:
???dbPath:?"/data/mongodb/formal_5000/data/s0"
???directoryPerDB:?true
systemLog:
???destination:?file
???path:?"/data/mongodb/formal_5000/log/s0.log"
???logAppend:?true
net:
???port:?5010
???ipv6:?true
processManagement:
???fork:?true
storage:
???journal:
??????enabled:?true
replication:
???replSetName:?rsShardB
sharding:	
???clusterRole:?shardsvr
...

在2臺服務(wù)器上執(zhí)行下面命令建立文件夾,否則mongodb無法啟動

mkdir?-p?/data/mongodb/formal_5000/log/
mkdir?-p?/data/mongodb/formal_5000/data/config/
mkdir?-p?/data/mongodb/formal_5000/data/s0

2.啟動2臺配置服務(wù),并配置成副本集

#在2臺服務(wù)器上都啟動配置服務(wù)
numactl?--interleave=all??mongod?-f?/etc/mongodb/formal_5000/mongodb_cfg.conf
#進(jìn)入其中一臺服務(wù)進(jìn)行配置
mongo?--port?5100
config={_id:"rsConf",members:[
	{_id:0,host:"10.2.4.214:5100"},
	{_id:1,host:"10.2.4.215:5100"}
]}
rs.initiate(config)
#查看副本集配置是否成功
rs.status()

3.啟動2臺分片和路由服務(wù),并配置

#在2臺服務(wù)器上都啟動分片服務(wù)
numactl?--interleave=all??mongod?-f?/etc/mongodb/formal_5000/mongodb_s0.conf

#在2臺服務(wù)器上都啟動路由服務(wù)
numactl?--interleave=all??mongos?-f?/etc/mongodb/formal_5000/mongodb_rout.conf

#進(jìn)入214分片并配置分片成副本集的primary
mongo?--port?5010
config={_id:"rsShardA",members:[
	{_id:0,host:"10.2.4.214:5010"}
]}
rs.initiate(config)

#進(jìn)入215分片并配置分片成副本集的primary
mongo?--port?5010
config={_id:"rsShardB",members:[
	{_id:0,host:"10.2.4.215:5010"}
]}
rs.initiate(config)

#進(jìn)入路由并配置分片
mongo?--port?5000
use?admin
db.runCommand({addshard:"rsShardA/10.2.4.214:5010",name:"shard_0",maxSize:0})
db.runCommand({addshard:"rsShardB/10.2.4.215:5010",name:"shard_1",maxSize:0})
#查看分片配置情況
sh.status()

4.設(shè)置monitor_center可分片

use?monitor_center
use?admin
db.runCommand({enablesharding:"monitor_center"})

5.使用Studio-3T連接路由、配置和分片建立用戶(這里教程省略)

6.關(guān)閉數(shù)據(jù)庫

#先關(guān)路由,進(jìn)入2臺服務(wù)器的路由
mongo?--port?5000
use?admin
db.shutdownServer()
#再關(guān)分片,進(jìn)入2臺服務(wù)器的分片
mongo?--port?5010
use?admin
db.shutdownServer({force:true})
#最后關(guān)配置,進(jìn)入2臺服務(wù)器的配置
mongo?--port?5100
use?admin
db.shutdownServer()

7.將keyfile加入到2臺服務(wù)器的路由、配置、分片的配置文件中的最后一行里,如下所示:

214的路由配置文件mongodb_rout.conf:

net:
???port:?5000
???ipv6:?true
systemLog:
???destination:?file
???path:?"/data/mongodb/formal_5000/log/route.log"
???logAppend:?true
processManagement:
???fork:?true
sharding:
???configDB:?rsConf/10.2.4.214:5100,10.2.4.215:5100
security:
???keyFile:?"/data/mongodb/formal_5000/key/mongodb_key"
...

其他配置文件類似,都需要添加keyfile

#生成keyFile
mkdir?-p?/data/mongodb/formal_5000/key/
cd?/data/mongodb/formal_5000/key
echo?-e?"formal?mongodb?keyFile"?>?mongodb_key
chmod?600?/data/mongodb/formal_5000/key/mongodb_key

8.最后依次啟動配置、分片、路由

numactl?--interleave=all??mongod?-f?/etc/mongodb/formal_5000/mongodb_cfg.conf
numactl?--interleave=all??mongod?-f?/etc/mongodb/formal_5000/mongodb_s0.conf
numactl?--interleave=all??mongos?-f?/etc/mongodb/formal_5000/mongodb_rout.conf

PS:

1.平衡器

sh.setBalancerState(true)?#啟動平衡器
sh.setBalancerState(false)?#關(guān)閉平衡器
sh.getBalancerState()?#查看平衡器的狀態(tài)
sh.isBalancerRunning()?#查看平衡器當(dāng)前是否在運行

#設(shè)置平衡器啟動時間范圍,首先確保平衡器是啟動狀態(tài)
db.settings.update({?_id:?"balancer"?},{?$set:?{?activeWindow?:?{?start?:?"21:00",?stop?:?"08:00"?}?}?},{?upsert:?true?})
#取消平衡器啟動時間范圍
db.settings.update({?_id?:?"balancer"?},?{?$unset?:?{?activeWindow?:?true?}?})

2.建表

use?monitor_center
db.createCollection("origdata_20171024")?#建表
db.origdata_20171024.createIndex({?mac:?1,?time:?1},{?background:?true})?#建索引
use?admin
db.runCommand({shardcollection:"monitor_center.origdata_20171024",key:{mac:1,time:1}})?#集合是否允許分片
db.runCommand({moveChunk:"monitor_center.origdata_20171024",bounds:[{mac:MinKey,time:MinKey},{mac:MaxKey,time:MaxKey}],to:"shard_1"})?#將集合手動移到分片1

3.表的遷移

sh.enableBalancing("monitor_center.origdata_20171024")?#開啟該集合的數(shù)據(jù)平衡
sh.disableBalancing("monitor_center.origdata_20171024")?#關(guān)閉該集合的數(shù)據(jù)平衡
db.getSiblingDB("config").collections.findOne({_id?:?"monitor_center.origdata_20171024"}).noBalance?#檢查該集合是否啟動了數(shù)據(jù)平衡

標(biāo)題名稱:Mongodb集群搭建
網(wǎng)站URL:http://muchs.cn/article48/gjgjhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版靜態(tài)網(wǎng)站、企業(yè)建站、網(wǎng)站維護(hù)手機(jī)網(wǎng)站建設(shè)、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司