MongoDB高可用分片集群搭建


1.環境軟件版本

環境&軟件 版本
Oracle VM VirtualBox
服務器&CentOS 7.8
數據庫&MongoDB 4.1.3
遠程連接&Xshell 6

2.環境架構介紹

架構圖如下圖所示:

ip 角色 權限
192.168.124.136 Primary節點 讀寫
192.168.124.137 Secondary節點 只讀
192.168.124.138 Secondary節點 只讀

3.復制集搭建

3.1 MongoDB安裝(3台)

下載

去官網下載社區版 MongoDB 4.1.3 然后上傳到Linux虛擬機

解壓並重命名

tar -xvf mongodb-linux-x86_64-4.1.3.tgz
mv mongodb-linux-x86_64-4.1.3 mongodb

主節點配置

192.168.124.136機器上操作如下:

mkdir /usr/local/mongodb/data/ -p

切到mongodb目錄,添加配置文件mongo.conf

vi mongo.conf

配置文件如下:

# 數據存放路徑 
dbpath=/usr/local/mongo_cluster/data/
bind_ip=0.0.0.0
# 端口
port=27017
# 是否啟用守護線程,即后台運行
fork=true
# 日志存放路徑
logpath=/usr/local/mongo_cluster/data/logs/server.log
# 集群名稱
replSet=myCluster

從節點1配置

192.168.124.137機器上重復以上操作

從節點2配置

192.168.124.138機器上重復以上操作

初始化節點配置

分別在三台機器上執行如下命令,啟動三個節點

./bin/mongod -config mongo.conf

進入任意一個節點運行如下命令:

var cfg ={"_id":"myCluster",
     "protocolVersion" : 1,
	 "members":[
		{"_id":1,"host":"192.168.124.136:27017","priority":10},
		{"_id":2,"host":"192.168.124.137:27017"},
		{"_id":3,"host":"192.168.124.138:27017"},
	 ]
}
rs.initiate(cfg)
rs.status()

4.分片集群搭建

分片(sharding)是MongoDB用來將大型集合水平分割到不同服務器(或者復制集)上所采用的方法。

分片集群架構圖如下:

4.1 分片集群組成

分片集群由以下3個服務組成:

  • Shards Server:每個shard由一個或多個mongod進程組成,用於存儲數據。
  • Router Server: 數據庫集群的請求入口,所有請求都通過Router(mongos)進行協調,不需要在應用程序添加一個路由選擇器,Router(mongos)就是一個請求分發中心它負責把應用程序的請求轉發到對應的Shard服務器上。
  • Config Server: 配置服務器。存儲所有數據庫元信息(路由、分片)的配置。

4.2 搭建過程

配置並啟動config節點集群

  • 節點1

192.168.124.136機器上操作如下:

tar -xvf mongodb-linux-x86_64-4.1.3.tgz
mv mongodb-linux-x86_64-4.1.3 mongo_shard_cluster

mkdir /data/cluster/config/logs -p

切到mongo_shard_cluster目錄,添加配置文件config.conf

# 數據庫文件位置
dbpath=/data/cluster/config
#日志文件位置
logpath=/data/cluster/config/logs/config.log
# 以追加方式寫入日志
logappend=true
# 是否以守護進程方式運行
fork=true
bind_ip=0.0.0.0
port=17017
# 表示是一個配置服務器
configsvr=true
#配置服務器副本集名稱
replSet=configsvr
  • 節點2

192.168.124.137機器上重復以上操作

  • 節點3

192.168.124.138機器上重復以上操作

分別在三台機器上執行如下命令,啟動三個節點

./bin/mongod -config config.conf

進入任意節點的mongo shell並添加配置節點集群,注意這里必須使用admin庫進行操作

./mongo --port 17017

use admin

var cfg ={"_id":"configsvr",
"members":[
{"_id":1,"host":"192.168.124.136:17017"},
{"_id":2,"host":"192.168.124.137:17017"},
{"_id":3,"host":"192.168.124.138:17017"}]
};
rs.initiate(cfg)

配置shard集群

  • shard1集群搭建

分別在三台服務器上操作如下命令:

mkdir /data/cluster/shard1 -p
mkdir /data/cluster/shard2 -p
mkdir /data/cluster/shard3 -p

添加配置文件shard-37017.conf

dbpath=/data/cluster/shard1
bind_ip=0.0.0.0
port=37017
fork=true
logpath=/data/cluster/shard1/shard1.log
replSet=shard1
shardsvr=true

啟動每個mongod,然后進入其中一個進行集群配置

./bin/mongod -config shard-37017.conf
./bin/mongod -config shard-37018.conf
./bin/mongod -config shard-37019.conf
./bin/mongo --port 37017

var cfg ={"_id":"shard1",
"protocolVersion" : 1,
"members":[
{"_id":1,"host":"192.168.124.136:37017"},
{"_id":2,"host":"192.168.124.137:37017"},
{"_id":3,"host":"192.168.124.138:37017"}
]
};
rs.initiate(cfg)
rs.status()
  • shard2集群搭建

分別在三台服務器上操作如下命令:

添加配置文件shard-37018.conf

dbpath=/data/cluster/shard2
bind_ip=0.0.0.0
port=37018
fork=true
logpath=/data/cluster/shard2/shard2.log
replSet=shard2
shardsvr=true

啟動每個mongod,然后進入其中一個進行集群配置

./bin/mongo --port 37018

var cfg ={"_id":"shard2",
"protocolVersion" : 1,
"members":[
{"_id":1,"host":"192.168.124.136:37018"},
{"_id":2,"host":"192.168.124.137:37018"},
{"_id":3,"host":"192.168.124.138:37018"}
]
};
rs.initiate(cfg)
rs.status()
  • shard3集群搭建

分別在三台服務器上操作如下命令:

添加配置文件shard-37019.conf

dbpath=/data/cluster/shard3
bind_ip=0.0.0.0
port=37019
fork=true
logpath=/data/cluster/shard3/shard3.log
replSet=shard3
shardsvr=true

啟動每個mongod,然后進入其中一個進行集群配置

./bin/mongo --port 37019

var cfg ={"_id":"shard3",
"protocolVersion" : 1,
"members":[
{"_id":1,"host":"192.168.124.136:37019"},
{"_id":2,"host":"192.168.124.137:37019"},
{"_id":3,"host":"192.168.124.138:37019"}
]
};
rs.initiate(cfg)
rs.status()
  • 配置和啟動路由節點

192.168.124.136機器上操作如下:

mkdir /data/cluster/route/logs -p

切到mongo_shard_cluster目錄下,添加配置文件route.conf

port=27017
bind_ip=0.0.0.0
fork=true
logpath=/data/cluster/route/logs/route.log
configdb=configsvr/192.168.124.136:17017,192.168.124.137:17017,192.168.124.138:17017

使用mongos啟動路由節點

./bin/mongos -f route.conf

mongos(路由)中添加分片節點

進入路由mongos

./bin/mongo  --port 27017
sh.status()   
sh.addShard("shard1/192.168.124.136:37017,192.168.124.137:37017,192.168.124.138:37017");
sh.addShard("shard2/192.168.124.136:37018,192.168.124.137:37018,192.168.124.138:37018");
sh.addShard("shard3/192.168.124.136:37019,192.168.124.137:37019,192.168.124.138:37019");
sh.status()

開啟數據庫和集合分片(指點片鍵)

繼續使用mongos完成分片開啟和分片大小設置

為數據庫開啟分片功能
sh.enableSharding("lg_resume")
為指定集合開啟分片功能
sh.shardCollection("lg_resume.lg_resume_datas",{"name":"hashed"})

向集合中插入數據測試

use  lg_resume;
for(var i=1;i<= 1000;i++){
  db.lg_resume_datas.insert({"name":"test"+i,
    salary:(Math.random()*20000).toFixed(2)});
}

驗證分片效果

分別進入 shard1、shard2和shard3中的數據庫 進行驗證

5.單節點添加安全認證

MongoDB 默認是沒有賬號的,可以直接連接,無須身份驗證。實際項目中肯定是要權限驗證的,否則后果不堪設想。

切換到admin數據庫對用戶的添加

use admin;

db.createUser({
	user:"root",
	pwd:"123456",
	roles:[{role:"root",db:"admin"}]
})

切換到lg_resume庫對用戶的添加

use lg_resume;

db.createUser({
	user:"lagou_gx",
	pwd:"abc321",
	roles:[{role:"readWrite",db:"lg_resume"}]
})

以auth方式啟動mongod

修改mongo.conf配置文件,添加auth=true參數

驗證用戶

db.auth("賬號","密碼")

6.分片集群添加安全認證

開啟安全認證之前 進入路由創建管理員和普通用戶

參考上面的單節點

關閉所有配置節點、分片節點、路由節點

  • 安裝psmisc

    yum install psmisc
    
  • 安裝完之后可以使用killall 命令 快速關閉多個進程

    killall mongod
    killall mongos
    

生成秘鑰文件 並修改權限

在每一台服務器操作如下命令

mkdir /data/cluster/keyFile -p
# 注意:秘鑰文件生成一個即可,將其拷貝到其它服務器
openssl rand -base64 756 > /data/cluster/keyFile/myKey.file

scp -P22 /data/cluster/keyFile/myKey.file root@192.168.124.137:/data/cluster/keyFile/myKey.file
scp -P22 /data/cluster/keyFile/myKey.file root@192.168.124.138:/data/cluster/keyFile/myKey.file
scp -P22 /data/cluster/keyFile/myKey.file root@192.168.124.139:/data/cluster/keyFile/myKey.file

chmod 600 /data/cluster/keyFile/myKey.file

配置節點集群和分片節點集群開啟安全認證和指定密鑰文件

auth=true
keyFile=/data/cluster/keyFile/myKey.file

在路由配置文件中 設置密鑰文件

keyFile=/data/cluster/keyFile/myKey.file

啟動所有的配置節點 分片節點 和 路由節點 使用路由進行權限驗證

分別在三台服務器上操作如下命令:

切到mongo_shard_cluster目錄,創建mongo_startup.sh腳本

./bin/mongod -f config.conf

./bin/mongod -f shard-37017.conf
./bin/mongod -f shard-37018.conf
./bin/mongod -f shard-37019.conf

./bin/mongos -f route.conf

賦予可執行權限

chmod +x mongo_startup.sh

啟動腳本

cd /usr/local/mongo_shard_cluster/ sh mongod.sh

7.Spring boot 連接安全認證的分片集群

spring.data.mongodb.username=lagou_gx
spring.data.mongodb.password=abc321


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM