mongodb高可用集群搭建

时间:2022-12-22 14:51:59

集群构架图如下:

mongodb高可用集群搭建

 

# 1 软件准备
## 1.1 生成yum源
cat <<EOF | sudo tee /etc/yum.repos.d/mongodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
EOF

## 1.2 安装mogo命令包
sudo yum install -y mongodb-org

# 2 部署
## 2.1 目录规划
├── config
│   ├── mongod.conf
│   └── start.sh
├── data
│   ├── config
│   ├── rs1
│   ├── rs2
│   └── rs3
├── logs
│   ├── config.log
│   ├── mongos.log
│   ├── rs1.log
│   ├── rs2.log
│   └── rs3.log
├── mongos
│   ├── mongos.conf
│   └── start.sh
└── shards
├── rs1
├── rs2
├── rs3
└── start_all.sh

shards/
├── rs1
│   ├── mongod.conf
│   └── start.sh
├── rs2
│   ├── mongod.conf
│   └── start.sh
├── rs3
│   ├── mongod.conf
│   └── start.sh
└── start_all.sh

## 2.2 配置详解[212为例]
shards:[rs1为例]
[mongod.conf]
dbpath=/opt/hadoop/mogodb-cluster/mogodb/data/rs1
logpath=/opt/hadoop/mogodb-cluster/mogodb/logs/rs1.log
logappend=true
bind_ip=192.168.3.212
port=10001
replSet=rs1
fork=true
directoryperdb=true
shardsvr=true
[start.sh]
mongod -f /opt/hadoop/mogodb-cluster/mogodb/shards/rs1/mongod.conf &
[start_all.sh]
sh /opt/hadoop/mogodb-cluster/mogodb/shards/rs1/start.sh
sh /opt/hadoop/mogodb-cluster/mogodb/shards/rs2/start.sh
sh /opt/hadoop/mogodb-cluster/mogodb/shards/rs3/start.sh

config
[mongod.conf]
dbpath=/opt/hadoop/mogodb-cluster/mogodb/data/config
logpath=/opt/hadoop/mogodb-cluster/mogodb/logs/config.log
logappend=true
bind_ip=192.168.3.212
port=30000
configsvr=true
fork=true
directoryperdb=true
replSet=configReplSet
[start.sh]
mongod -f /opt/hadoop/mogodb-cluster/mogodb/config/mongod.conf &

mongos
[mongod.conf]
logpath=/opt/hadoop/mogodb-cluster/mogodb/logs/mongos.log
logappend=true
bind_ip=192.168.3.212
port=50000
configdb=configReplSet/192.168.3.212:30000,192.168.3.213:30000,192.168.3.214:30000
fork=true
[start.sh]
mongos -f /opt/hadoop/mogodb-cluster/mogodb/mongos/mongos.conf &

## 2.3 部署步骤
1. 启动数据mongod实例3*3=9个 [3台机器]
./shards/start_all.sh
2. 配置3个副本集rs1、rs2和rs3
mongo 192.168.3.212:10001/admin
conf = {_id:"rs1",members:[{_id:0,host:"192.168.3.212:10001",priority:2},{_id:1,host:"192.168.3.213:10001",priority:1},{_id:2,host:"192.168.3.214:10001",arbiterOnly:true}]};
rs.initiate(conf);
mongo 192.168.3.213:20001/admin
conf = {_id:"rs2",members:[{_id:0,host:"192.168.3.212:20001",arbiterOnly:true},{_id:1,host:"192.168.3.213:20001",priority:2},{_id:2,host:"192.168.3.214:20001",priority:1}]};
rs.initiate(conf);
mongo 192.168.3.214:30001/admin
conf = {_id:"rs3",members:[{_id:0,host:"192.168.3.212:30001",priority:1},{_id:1,host:"192.168.3.213:30001",arbiterOnly:true},{_id:2,host:"192.168.3.214:30001",priority:2}]};
rs.initiate(conf);
3. 启动3个配置mongod实例1*3=3个 [3台机器]
./config/start.sh
4. 配置副本集configReplSet [其中一台]
config = {
_id: "configReplSet",
configsvr: true,
members: [
{_id: 0, host: "192.168.3.212:30000", priority: 30},
{_id: 1, host: "192.168.3.213:30000", priority: 20},
{_id: 2, host: "192.168.3.214:30000", priority: 10}
]};rs.initiate(config)
5. 启动3个路由mongos实例1*3=3个 [3台机器]
mongos -f /opt/hadoop/mogodb-cluster/mogodb/mongos/mongos.conf &
6. 配置分片路由信息 [其中一台]
mongo 192.168.3.212:50000/admin
conf = {addshard:"rs1/192.168.3.212:10001,192.168.3.213:10001,192.168.3.214:10001",name:"rs1"};db.runCommand(conf);
conf = {addshard:"rs2/192.168.3.212:20001,192.168.3.213:20001,192.168.3.214:20001",name:"rs2"};db.runCommand(conf);
conf = {addshard:"rs3/192.168.3.212:30001,192.168.3.213:30001,192.168.3.214:30001",name:"rs3"};db.runCommand(conf);
db.printShardingStatus()