服務(wù)器75 | 服務(wù)器84 | 服務(wù)器86 |
---|---|---|
mongos | mongos | mongos |
config server | config server | config server |
shard server1 主節(jié)點(diǎn) | shard server1 副節(jié)點(diǎn) | shard server1 仲裁 |
shard server2 仲裁 | shard server2 主節(jié)點(diǎn) | shard server2 副節(jié)點(diǎn) |
shard server3 副節(jié)點(diǎn) | shard server3 仲裁 | shard server3 主節(jié)點(diǎn) |
端口分配:
mongos:20000 config:21000 shard1:27001 shard2:27002 shard3:27003
集群搭建
1、安裝mongodb
#解壓 tar -xzvf mongodb-linux-x86_64-3.4.6.tgz -C /usr/local/ #改名 mv mongodb-linux-x86_64-3.4.6 mongodb
分別在每臺(tái)機(jī)器建立conf、mongos、config、shard1、shard2、shard3六個(gè)目錄,因?yàn)閙ongos不存儲(chǔ)數(shù)據(jù),只需要建立日志文件目錄即可。
mkdir -p /usr/local/mongodb/conf mkdir -p /usr/local/mongodb/mongos/log mkdir -p /usr/local/mongodb/config/data mkdir -p /usr/local/mongodb/config/log mkdir -p /usr/local/mongodb/shard1/data mkdir -p /usr/local/mongodb/shard1/log mkdir -p /usr/local/mongodb/shard2/data mkdir -p /usr/local/mongodb/shard2/log mkdir -p /usr/local/mongodb/shard3/data mkdir -p /usr/local/mongodb/shard3/log
配置環(huán)境變量
vim /etc/profile # 內(nèi)容 export MONGODB_HOME=/usr/local/mongodb export PATH=$MONGODB_HOME/bin:$PATH # 使立即生效 source /etc/profile
2、config server配置服務(wù)器
mongodb3.4以后要求配置服務(wù)器也創(chuàng)建副本集,不然集群搭建不成功。
添加配置文件
vi /usr/local/mongodb/conf/config.conf ## 配置文件內(nèi)容 pidfilepath = /usr/local/mongodb/config/log/configsrv.pid dbpath = /usr/local/mongodb/config/data logpath = /usr/local/mongodb/config/log/congigsrv.log logappend = true bind_ip = 0.0.0.0 port = 21000 fork = true #declare this is a config db of a cluster; configsvr = true #副本集名稱 replSet=configs #設(shè)置最大連接數(shù) maxConns=20000
啟動(dòng)三臺(tái)服務(wù)器的config server
mongod -f /usr/local/mongodb/conf/config.conf
登錄任意一臺(tái)配置服務(wù)器,初始化配置副本集
#連接 mongo --port 21000 #config變量 config = { ... _id : "configs", ... members : [ ... {_id : 0, host : "192.168.0.75:21000" }, ... {_id : 1, host : "192.168.0.84:21000" }, ... {_id : 2, host : "192.168.0.86:21000" } ... ] ... } #初始化副本集 rs.initiate(config)
其中,”_id” : “configs”應(yīng)與配置文件中配置的 replicaction.replSetName 一致,”members” 中的 “host” 為三個(gè)節(jié)點(diǎn)的 ip 和 port
3、配置分片副本集(三臺(tái)機(jī)器)
設(shè)置第一個(gè)分片副本集
配置文件
vi /usr/local/mongodb/conf/shard1.conf #配置文件內(nèi)容 #——————————————– pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid dbpath = /usr/local/mongodb/shard1/data logpath = /usr/local/mongodb/shard1/log/shard1.log logappend = true bind_ip = 0.0.0.0 port = 27001 fork = true #打開web監(jiān)控 httpinterface=true rest=true #副本集名稱 replSet=shard1 #declare this is a shard db of a cluster; shardsvr = true #設(shè)置最大連接數(shù) maxConns=20000
啟動(dòng)三臺(tái)服務(wù)器的shard1 server
mongod -f /usr/local/mongodb/conf/shard1.conf
登陸任意一臺(tái)服務(wù)器,初始化副本集
mongo --port 27001 #使用admin數(shù)據(jù)庫(kù) use admin #定義副本集配置,第三個(gè)節(jié)點(diǎn)的 "arbiterOnly":true 代表其為仲裁節(jié)點(diǎn)。 config = { ... _id : "shard1", ... members : [ ... {_id : 0, host : "192.168.0.75:27001" }, ... {_id : 1, host : "192.168.0.84:27001" }, ... {_id : 2, host : "192.168.0.86:27001” , arbiterOnly: true } ... ] ... } #初始化副本集配置 rs.initiate(config);
設(shè)置第二個(gè)分片副本集
配置文件
vi /usr/local/mongodb/conf/shard2.conf #配置文件內(nèi)容 #——————————————– pidfilepath = /usr/local/mongodb/shard2/log/shard2.pid dbpath = /usr/local/mongodb/shard2/data logpath = /usr/local/mongodb/shard2/log/shard2.log logappend = true bind_ip = 0.0.0.0 port = 27002 fork = true #打開web監(jiān)控 httpinterface=true rest=true #副本集名稱 replSet=shard2 #declare this is a shard db of a cluster; shardsvr = true #設(shè)置最大連接數(shù) maxConns=20000
啟動(dòng)三臺(tái)服務(wù)器的shard2 server
mongod -f /usr/local/mongodb/conf/shard2.conf
登陸任意一臺(tái)服務(wù)器,初始化副本集
mongo --port 27002 #使用admin數(shù)據(jù)庫(kù) use admin #定義副本集配置 config = { ... _id : "shard2", ... members : [ ... {_id : 0, host : "192.168.0.75:27002" , arbiterOnly: true }, ... {_id : 1, host : "192.168.0.84:27002" }, ... {_id : 2, host : "192.168.0.86:27002" } ... ] ... } #初始化副本集配置 rs.initiate(config);
設(shè)置第三個(gè)分片副本集
配置文件
vi /usr/local/mongodb/conf/shard3.conf #配置文件內(nèi)容 #——————————————– pidfilepath = /usr/local/mongodb/shard3/log/shard3.pid dbpath = /usr/local/mongodb/shard3/data logpath = /usr/local/mongodb/shard3/log/shard3.log logappend = true bind_ip = 0.0.0.0 port = 27003 fork = true #打開web監(jiān)控 httpinterface=true rest=true #副本集名稱 replSet=shard3 #declare this is a shard db of a cluster; shardsvr = true #設(shè)置最大連接數(shù) maxConns=20000
啟動(dòng)三臺(tái)服務(wù)器的shard3 server
mongod -f /usr/local/mongodb/conf/shard3.conf
登陸任意一臺(tái)服務(wù)器,初始化副本集
mongo --port 27003 #使用admin數(shù)據(jù)庫(kù) use admin #定義副本集配置 config = { ... _id : "shard3", ... members : [ ... {_id : 0, host : "192.168.0.75:27003" }, ... {_id : 1, host : "192.168.0.84:27003" , arbiterOnly: true}, ... {_id : 2, host : "192.168.0.86:27003" } ... ] ... } #初始化副本集配置 rs.initiate(config);
4、配置路由服務(wù)器 mongos
先啟動(dòng)配置服務(wù)器和分片服務(wù)器,后啟動(dòng)路由實(shí)例啟動(dòng)路由實(shí)例:(三臺(tái)機(jī)器)
vi /usr/local/mongodb/conf/mongos.conf #內(nèi)容 pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid logpath = /usr/local/mongodb/mongos/log/mongos.log logappend = true bind_ip = 0.0.0.0 port = 20000 fork = true #監(jiān)聽的配置服務(wù)器,只能有1個(gè)或者3個(gè) configs為配置服務(wù)器的副本集名字 configdb = configs/192.168.0.75:21000,192.168.0.84:21000,192.168.0.86:21000 #設(shè)置最大連接數(shù) maxConns=20000
啟動(dòng)三臺(tái)服務(wù)器的mongos server
mongod -f /usr/local/mongodb/conf/mongos.conf
5、啟用分片
目前搭建了mongodb配置服務(wù)器、路由服務(wù)器,各個(gè)分片服務(wù)器,不過(guò)應(yīng)用程序連接到mongos路由服務(wù)器并不能使用分片機(jī)制,還需要在程序里設(shè)置分片配置,讓分片生效。
登陸任意一臺(tái)mongos
mongo --port 20000 #使用admin數(shù)據(jù)庫(kù) user admin #串聯(lián)路由服務(wù)器與分配副本集 sh.addShard("shard1/192.168.0.75:27001,192.168.0.84:27001,192.168.0.86:27001") sh.addShard("shard2/192.168.0.75:27002,192.168.0.84:27002,192.168.0.86:27002") sh.addShard("shard3/192.168.0.75:27003,192.168.0.84:27003,192.168.0.86:27003") #查看集群狀態(tài) sh.status()
6、測(cè)試
目前配置服務(wù)、路由服務(wù)、分片服務(wù)、副本集服務(wù)都已經(jīng)串聯(lián)起來(lái)了,但我們的目的是希望插入數(shù)據(jù),數(shù)據(jù)能夠自動(dòng)分片。連接在mongos上,準(zhǔn)備讓指定的數(shù)據(jù)庫(kù)、指定的集合分片生效。
#指定testdb分片生效 db.runCommand( { enablesharding :"testdb"}); #指定數(shù)據(jù)庫(kù)里需要分片的集合和片鍵 db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
我們?cè)O(shè)置testdb的 table1 表需要分片,根據(jù) id 自動(dòng)分片到 shard1 ,shard2,shard3 上面去。要這樣設(shè)置是因?yàn)椴皇撬衜ongodb 的數(shù)據(jù)庫(kù)和表 都需要分片!
測(cè)試分片配置結(jié)果
mongo 127.0.0.1:20000 #使用testdb use testdb; #插入測(cè)試數(shù)據(jù) for (var i = 1; i = 100000; i++) db.table1.save({id:i,"test1":"testval1"}); #查看分片情況如下,部分無(wú)關(guān)信息省掉了 db.table1.stats(); { "sharded" : true, "ns" : "testdb.table1", "count" : 100000, "numExtents" : 13, "size" : 5600000, "storageSize" : 22372352, "totalIndexSize" : 6213760, "indexSizes" : { "_id_" : 3335808, "id_1" : 2877952 }, "avgObjSize" : 56, "nindexes" : 2, "nchunks" : 3, "shards" : { "shard1" : { "ns" : "testdb.table1", "count" : 42183, "size" : 0, ... "ok" : 1 }, "shard2" : { "ns" : "testdb.table1", "count" : 38937, "size" : 2180472, ... "ok" : 1 }, "shard3" : { "ns" : "testdb.table1", "count" :18880, "size" : 3419528, ... "ok" : 1 } }, "ok" : 1 }
可以看到數(shù)據(jù)分到3個(gè)分片,各自分片數(shù)量為: shard1 “count” : 42183,shard2 “count” : 38937,shard3 “count” : 18880。已經(jīng)成功了!
后期運(yùn)維
啟動(dòng)關(guān)閉
mongodb的啟動(dòng)順序是,先啟動(dòng)配置服務(wù)器,在啟動(dòng)分片,最后啟動(dòng)mongos.
mongod -f /usr/local/mongodb/conf/config.conf mongod -f /usr/local/mongodb/conf/shard1.conf mongod -f /usr/local/mongodb/conf/shard2.conf mongod -f /usr/local/mongodb/conf/shard3.conf mongod -f /usr/local/mongodb/conf/mongos.conf
關(guān)閉時(shí),直接killall殺掉所有進(jìn)程
killall mongod killall mongos
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
參考:
標(biāo)簽:無(wú)錫 泰安 遼陽(yáng) 雞西 興安盟 廈門 玉林 自貢
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mongodb3.4集群搭建實(shí)戰(zhàn)之高可用的分片+副本集》,本文關(guān)鍵詞 mongodb3.4,集群,搭建,實(shí)戰(zhàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。