溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MongoDB中怎么實(shí)現(xiàn)分片與集群

發(fā)布時(shí)間:2021-08-04 14:15:23 來(lái)源:億速云 閱讀:150 作者:Leah 欄目:數(shù)據(jù)庫(kù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)MongoDB中怎么實(shí)現(xiàn)分片與集群,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

一、 mongodb分片與集群拓?fù)鋱D

MongoDB中怎么實(shí)現(xiàn)分片與集群

二、分片與集群的部署

1.Mongodb的安裝

分別在以上3臺(tái)服務(wù)器安裝好mongodb

安裝方法見(jiàn)安裝腳本。

2.Mongod 創(chuàng)建單個(gè)分片的副本集

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

③啟動(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/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  ③啟動(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/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

③啟動(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/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)建單個(gè)分片的配置服務(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個(gè)服務(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

③啟動(dòng)配置服務(wù)器

/etc/init.d/mongodb_config start

3.Mongod 創(chuàng)建并配置mongos和開(kāi)啟分片模式

① 創(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

③ 啟動(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.圖1

MongoDB中怎么實(shí)現(xiàn)分片與集群

2.圖2 config.conf配置文件

MongoDB中怎么實(shí)現(xiàn)分片與集群

3.圖3

MongoDB中怎么實(shí)現(xiàn)分片與集群

上述就是小編為大家分享的MongoDB中怎么實(shí)現(xiàn)分片與集群了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI