摘要:搭建分片分區(qū)集群環(huán)境安裝三臺(tái)機(jī)器關(guān)閉防火墻主節(jié)點(diǎn)副節(jié)點(diǎn)仲裁仲裁主節(jié)點(diǎn)副節(jié)點(diǎn)副節(jié)點(diǎn)仲裁主節(jié)點(diǎn)端口分配下載并且安裝所有版本二進(jìn)制文件自行下載改名分別在每臺(tái)機(jī)器建立六個(gè)目錄,因?yàn)椴淮鎯?chǔ)數(shù)據(jù),只需要建立日志文件目錄即可。
搭建 MongoDB分片(sharding) / 分區(qū) / 集群環(huán)境 1. 安裝 MongoDB
三臺(tái)機(jī)器
關(guān)閉防火墻
systemctl stop firewalld.service
192.168.252.121 | 192.168.252.122 | 192.168.252.123 |
---|---|---|
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
下載并且安裝
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.6.2.tgz tar -xzvf mongodb-linux-x86_64-amazon-3.6.2.tgz -C /usr/local/
所有版本二進(jìn)制文件,自行下載
https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl?_ga=2.87139544.1567998244.1517190032-1153843332.1517190032&_gac=1.204211492.1517212002.EAIaIQobChMI44v9_9b82AIV1AcqCh0lcABIEAAYASAAEgKI1_D_BwE
改名
cd /usr/local/ mv mongodb-linux-x86_64-amazon-3.6.2 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)境變量
vi /etc/profile # MongoDB 環(huán)境變量?jī)?nèi)容 export MONGODB_HOME=/usr/local/mongodb export PATH=$MONGODB_HOME/bin:$PATH
使立即生效
source /etc/profile2. config server配置服務(wù)器
mongodb3.4以后要求配置服務(wù)器也創(chuàng)建副本集,不然集群搭建不成功。
(三臺(tái)機(jī)器)添加配置文件
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ù)器,初始化配置副本集
連接 MongoDB
mongo --port 21000
config 變量
config = { _id : "configs", members : [ {_id : 0, host : "192.168.252.121:21000" }, {_id : 1, host : "192.168.252.122:21000" }, {_id : 2, host : "192.168.252.123:21000" } ] }
初始化副本集
rs.initiate(config)
其中,"_id" : "configs"應(yīng)與配置文件中配置的 replicaction.replSetName 一致,"members" 中的 "host" 為三個(gè)節(jié)點(diǎn)的 ip 和 port
響應(yīng)內(nèi)容如下
> config = { ... _id : "configs", ... members : [ ... {_id : 0, host : "192.168.252.121:21000" }, ... {_id : 1, host : "192.168.252.122:21000" }, ... {_id : 2, host : "192.168.252.123:21000" } ... ] ... } { "_id" : "configs", "members" : [ { "_id" : 0, "host" : "192.168.252.121:21000" }, { "_id" : 1, "host" : "192.168.252.122:21000" }, { "_id" : 2, "host" : "192.168.252.123:21000" } ] } > rs.initiate(config); { "ok" : 1, "operationTime" : Timestamp(1517369899, 1), "$gleStats" : { "lastOpTime" : Timestamp(1517369899, 1), "electionId" : ObjectId("000000000000000000000000") }, "$clusterTime" : { "clusterTime" : Timestamp(1517369899, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } configs:SECONDARY>
此時(shí)會(huì)發(fā)現(xiàn)終端上的輸出已經(jīng)有了變化。
//從單個(gè)一個(gè) > //變成了 configs:SECONDARY>
查詢狀態(tài)
configs:SECONDARY> rs.status()3. 配置分片副本集 3.1 設(shè)置第一個(gè)分片副本集
(三臺(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 #副本集名稱 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ù)器,初始化副本集(除了192.168.252.123)
連接 MongoDB
mongo --port 27001
使用admin數(shù)據(jù)庫(kù)
use admin
定義副本集配置
config = { _id : "shard1", members : [ {_id : 0, host : "192.168.252.121:27001" }, {_id : 1, host : "192.168.252.122:27001" }, {_id : 2, host : "192.168.252.123:27001" , arbiterOnly: true } ] }
初始化副本集配置
rs.initiate(config)
響應(yīng)內(nèi)容如下
> use admin switched to db admin > config = { ... _id : "shard1", ... members : [ ... {_id : 0, host : "192.168.252.121:27001" }, ... {_id : 1, host : "192.168.252.122:27001" }, ... {_id : 2, host : "192.168.252.123:27001" , arbiterOnly: true } ... ] ... } { "_id" : "shard1", "members" : [ { "_id" : 0, "host" : "192.168.252.121:27001" }, { "_id" : 1, "host" : "192.168.252.122:27001" }, { "_id" : 2, "host" : "192.168.252.123:27001", "arbiterOnly" : true } ] } > rs.initiate(config) { "ok" : 1 }
此時(shí)會(huì)發(fā)現(xiàn)終端上的輸出已經(jīng)有了變化。
//從單個(gè)一個(gè) > //變成了 shard1:SECONDARY>
查詢狀態(tài)
shard1:SECONDARY> rs.status()3.2 設(shè)置第二個(gè)分片副本集
設(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 #副本集名稱 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
連接 MongoDB
mongo --port 27002
使用admin數(shù)據(jù)庫(kù)
use admin
定義副本集配置
config = { _id : "shard2", members : [ {_id : 0, host : "192.168.252.121:27002" , arbiterOnly: true }, {_id : 1, host : "192.168.252.122:27002" }, {_id : 2, host : "192.168.252.123:27002" } ] }
初始化副本集配置
rs.initiate(config)
響應(yīng)內(nèi)容如下
> use admin switched to db admin > config = { ... _id : "shard2", ... members : [ ... {_id : 0, host : "192.168.252.121:27002" , arbiterOnly: true }, ... {_id : 1, host : "192.168.252.122:27002" }, ... {_id : 2, host : "192.168.252.123:27002" } ... ] ... } { "_id" : "shard2", "members" : [ { "_id" : 0, "host" : "192.168.252.121:27002", "arbiterOnly" : true }, { "_id" : 1, "host" : "192.168.252.122:27002" }, { "_id" : 2, "host" : "192.168.252.123:27002" } ] } > rs.initiate(config) { "ok" : 1 } shard2:SECONDARY> rs.status()3.3 設(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 #副本集名稱 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ù)器,初始化副本集(除了192.168.252.121)
mongo --port 27003
使用admin數(shù)據(jù)庫(kù)
use admin
定義副本集配置
config = { _id : "shard3", members : [ {_id : 0, host : "192.168.252.121:27003" }, {_id : 1, host : "192.168.252.122:27003" , arbiterOnly: true}, {_id : 2, host : "192.168.252.123:27003" } ] }
初始化副本集配置
rs.initiate(config)
響應(yīng)內(nèi)容如下
> use admin switched to db admin > config = { ... _id : "shard3", ... members : [ ... {_id : 0, host : "192.168.252.121:27003" }, ... {_id : 1, host : "192.168.252.122:27003" , arbiterOnly: true}, ... {_id : 2, host : "192.168.252.123:27003" } ... ] ... } { "_id" : "shard3", "members" : [ { "_id" : 0, "host" : "192.168.252.121:27003" }, { "_id" : 1, "host" : "192.168.252.122:27003", "arbiterOnly" : true }, { "_id" : 2, "host" : "192.168.252.123:27003" } ] } > rs.initiate(config) { "ok" : 1 } shard3:SECONDARY> rs.status()3.4 配置路由服務(wù)器 mongos
(三臺(tái)機(jī)器)先啟動(dòng)配置服務(wù)器和分片服務(wù)器,后啟動(dòng)路由實(shí)例啟動(dòng)路由實(shí)例:
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)聽(tīng)的配置服務(wù)器,只能有1個(gè)或者3個(gè) configs為配置服務(wù)器的副本集名字 configdb = configs/192.168.252.121:21000,192.168.252.122:21000,192.168.252.123:21000 #設(shè)置最大連接數(shù) maxConns = 20000
啟動(dòng)三臺(tái)服務(wù)器的mongos server
mongos -f /usr/local/mongodb/conf/mongos.conf4. 串聯(lián)路由服務(wù)器
目前搭建了mongodb配置服務(wù)器、路由服務(wù)器,各個(gè)分片服務(wù)器,不過(guò)應(yīng)用程序連接到mongos路由服務(wù)器并不能使用分片機(jī)制,還需要在程序里設(shè)置分片配置,讓分片生效。
登陸任意一臺(tái)mongos
mongo --port 20000
使用admin數(shù)據(jù)庫(kù)
use admin
串聯(lián)路由服務(wù)器與分配副本集
sh.addShard("shard1/192.168.252.121:27001,192.168.252.122:27001,192.168.252.123:27001"); sh.addShard("shard2/192.168.252.121:27002,192.168.252.122:27002,192.168.252.123:27002"); sh.addShard("shard3/192.168.252.121:27003,192.168.252.122:27003,192.168.252.123:27003");
查看集群狀態(tài)
sh.status()
響應(yīng)內(nèi)容如下
mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5a713a37d56e076f3eb47acf") } shards: { "_id" : "shard1", "host" : "shard1/192.168.252.121:27001,192.168.252.122:27001", "state" : 1 } { "_id" : "shard2", "host" : "shard2/192.168.252.122:27002,192.168.252.123:27002", "state" : 1 } { "_id" : "shard3", "host" : "shard3/192.168.252.121:27003,192.168.252.123:27003", "state" : 1 } active mongoses: "3.6.2" : 3 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "config", "primary" : "config", "partitioned" : true } mongos>5. 啟用集合分片生效
目前配置服務(wù)、路由服務(wù)、分片服務(wù)、副本集服務(wù)都已經(jīng)串聯(lián)起來(lái)了,但我們的目的是希望插入數(shù)據(jù),數(shù)據(jù)能夠自動(dòng)分片。連接在mongos上,準(zhǔn)備讓指定的數(shù)據(jù)庫(kù)、指定的集合分片生效。
登陸任意一臺(tái)mongos
mongo --port 20000
使用admin數(shù)據(jù)庫(kù)
use admin
指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
指定數(shù)據(jù)庫(kù)里需要分片的集合和片鍵,哈希id 分片
db.runCommand( { shardcollection : "testdb.table1",key : {"id": "hashed"} } );
我們?cè)O(shè)置testdb的 table1 表需要分片,根據(jù) id 自動(dòng)分片到 shard1 ,shard2,shard3 上面去。要這樣設(shè)置是因?yàn)椴皇撬衜ongodb 的數(shù)據(jù)庫(kù)和表 都需要分片!
測(cè)試分片配置結(jié)果
連接 MongoDB 路由服務(wù)
mongo 127.0.0.1:20000
切換到 testdb 數(shù)據(jù)庫(kù)
use testdb;
插入測(cè)試數(shù)據(jù)
for(i=1;i<=100000;i++){db.table1.insert({"id":i,"name":"penglei"})};
總條數(shù)
db.table1.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}])
查看分片情況如下
shard1: "count": 33755
shard2: "count": 33143,
shard3: "count": 33102
結(jié)論數(shù)據(jù)基本均勻
db.table1.stats();
mongos> db.table1.stats(); { "sharded": true, "capped": false, "ns": "testdb.table1", "count": 100000, "size": 5200000, "storageSize": 1519616, "totalIndexSize": 3530752, "indexSizes": { "_id_": 892928, "id_hashed": 2637824 }, "avgObjSize": 52, "nindexes": 2, "nchunks": 6, "shards": { "shard1": { "ns": "testdb.table1", "size": 1755260, "count": 33755, "avgObjSize": 52, "storageSize": 532480, "capped": false, "wiredTiger": { ...省略很多 } }, "shard2": { "ns": "testdb.table1", "size": 1723436, "count": 33143, "avgObjSize": 52, "storageSize": 479232, "capped": false, "wiredTiger": { ...省略很多 } }, "shard3": { "ns": "testdb.table1", "size": 1721304, "count": 33102, "avgObjSize": 52, "storageSize": 507904, "capped": false, "wiredTiger": { ...省略很多 } } }, "ok": 1, "$clusterTime": { "clusterTime": Timestamp(1517488062, 350), "signature": { "hash": BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId": NumberLong(0) } }, "operationTime": Timestamp(1517488062, 350) } mongos>
分組查看總數(shù)量是:100000
mongos> db.table1.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}]) { "_id" : "penglei", "totle" : 100000 } mongos>后期運(yùn)維
參考
手把手教你 MongoDB 的安裝與詳細(xì)使用(一)
http://www.ymq.io/2018/01/26/MongoDB-1/
手把手教你 MongoDB 的安裝與詳細(xì)使用(二)
http://www.ymq.io/2018/01/29/MongoDB-2/
創(chuàng)建索引
db.table1.createIndex({"name":1}) db.table1.getIndexes()
啟動(dòng)
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 mongos -f /usr/local/mongodb/conf/mongos.conf
啟動(dòng)報(bào)錯(cuò)
about to fork child process, waiting until server is ready for connections. forked process: 1303 child process started successfully, parent exiting [root@node1 ~]# mongod -f /usr/local/mongodb/conf/shard1.conf about to fork child process, waiting until server is ready for connections. forked process: 1384
刪除 mongod.lock
cd /usr/local/mongodb/shard1/data rm -rf mongod.lock
關(guān)閉防火墻(不關(guān)防火墻也會(huì)遇到這樣的問(wèn)題)
systemctl stop firewalld.service
關(guān)閉
#debian、ubuntu系統(tǒng)下: apt-get install psmisc #centos或、rhel系統(tǒng)下: yum install psmisc
關(guān)閉時(shí),直接killall殺掉所有進(jìn)程
killall mongod killall mongos
參考:
mongodb 3.4 集群搭建:分片+副本集 http://www.ityouknow.com/mongodb/2017/08/05/mongodb-cluster-setup.html
Runoob 教程:http://www.runoob.com/mongodb...
Tutorials 教程:Pointhttps://www.tutorialspoint.co...
MongoDB 官網(wǎng)地址:https://www.mongodb.com
MongoDB 官方英文文檔:https://docs.mongodb.com/manual
MongoDB 各平臺(tái)下載地址:https://www.mongodb.com/downl...
MongoDB 安裝 https://docs.mongodb.com/manu...
作者:鵬磊
出處:http://www.ymq.io/2018/01/29/MongoDB-2
Email:[email protected]
版權(quán)歸作者所有,轉(zhuǎn)載請(qǐng)注明出處
Wechat:關(guān)注公眾號(hào),搜云庫(kù),專注于開(kāi)發(fā)技術(shù)的研究與知識(shí)分享
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/19193.html
摘要:復(fù)制一份,命名為,修改文件內(nèi)容如下注意改為我們第一步創(chuàng)建的目錄,端口號(hào)改為這個(gè)隨意,只要該端口沒(méi)被占用即可,表示這是一個(gè)配置服務(wù)器,另外由于我們的配置服務(wù)器要做成備份集,所以要設(shè)置。 分片是指將數(shù)據(jù)拆分,拆分后存放在不同的機(jī)器上的過(guò)程,以此來(lái)降低單個(gè)服務(wù)器的壓力,同時(shí)也解決單個(gè)服務(wù)器硬盤(pán)空間不足的問(wèn)題,讓我們可以用廉價(jià)的機(jī)器實(shí)現(xiàn)高性能的數(shù)據(jù)架構(gòu)。有的小伙伴不理解分片和副本集的差異,一言...
摘要:本文中我們就來(lái)聊一聊如何使用和兩個(gè)工具快速搭建一個(gè)測(cè)試集群。注意的目的旨在為我們快速搭建測(cè)試環(huán)境,絕對(duì)不應(yīng)該用于生產(chǎn)環(huán)境的使用上。準(zhǔn)備工作安裝是用于的一系列工具,就是其中用于快速啟動(dòng)的一部分。以下這些參數(shù)是我們經(jīng)常使用的啟動(dòng)一個(gè)復(fù)制集。 前言 不知道大家在使用MongoDB的時(shí)候有沒(méi)有遇到突然想要一個(gè)集群但是手邊又沒(méi)有的時(shí)候?特別是我已經(jīng)升級(jí)到4.0了,突然想要一個(gè)3.2的集群怎么辦?...
摘要:聲明構(gòu)造函數(shù),作用是把從數(shù)據(jù)庫(kù)取出的數(shù)據(jù)實(shí)例化為對(duì)象。該構(gòu)造函數(shù)傳入的值為從中取出的數(shù)據(jù)省略接口提供增刪改查接口實(shí)現(xiàn)提供增刪改查接口實(shí)現(xiàn)提供了一個(gè)類似于的設(shè)計(jì)的類。 本文快速入門(mén),MongoDB 結(jié)合SpringBoot starter-data-mongodb 進(jìn)行增刪改查 1、什么是MongoDB ? MongoDB 是由C++語(yǔ)言編寫(xiě)的,是一個(gè)基于分布式文件存儲(chǔ)的開(kāi)源數(shù)據(jù)庫(kù)系統(tǒng)。...
摘要:集群搭建方式主從復(fù)制目前官方已不推薦使用副本集的副本集不同于以往的主從模式。分片是一種可以水平擴(kuò)展的模式在數(shù)據(jù)量很大時(shí)特給力實(shí)際大規(guī)模應(yīng)用一般會(huì)采用這種架構(gòu)去構(gòu)建。 mongodb集群搭建方式 1.master-slave 主從復(fù)制 目前官方已不推薦使用 2.Replica Sets 副本集 showImg(https://segmentfault.com/img/bVbsIpa?w=...
摘要:簡(jiǎn)述為何要分片減少單機(jī)請(qǐng)求數(shù),降低單機(jī)負(fù)載,提高總負(fù)載減少單機(jī)的存儲(chǔ)空間,提高總存空間。就是用來(lái)存儲(chǔ)所有節(jié)點(diǎn)的配置信息每個(gè)的范圍在各的分布情況該集群中所有和的配置信息。 簡(jiǎn)述 為何要分片 減少單機(jī)請(qǐng)求數(shù),降低單機(jī)負(fù)載,提高總負(fù)載 減少單機(jī)的存儲(chǔ)空間,提高總存空間。 showImg(http://static.oschina.net/uploads/space/2014/0201/1...
閱讀 3104·2023-04-25 20:43
閱讀 1736·2021-09-30 09:54
閱讀 1604·2021-09-24 09:47
閱讀 2895·2021-09-06 15:02
閱讀 3528·2021-02-22 17:09
閱讀 1250·2019-08-30 15:53
閱讀 1452·2019-08-29 17:04
閱讀 1974·2019-08-28 18:22