0
点赞
收藏
分享

微信扫一扫

docker 安装mongodb集群==多台服务器

杨沐涵 2023-03-19 阅读 55

 

服务器

192.168.5.201
192.168.5.202
192.168.5.203

 

==========以下每台都执行========================

===========安装过程=================
镜像

docker pull mongo:4.2.7

 

网络

docker network create --subnet=10.20.0.0/24 mongodbnet

 

文件夹

mkdir -p /home/soft/mongodbmoney/configsvr
mkdir -p /home/soft/mongodbmoney/shard1
mkdir -p /home/soft/mongodbmoney/shard2
mkdir -p /home/soft/mongodbmoney/shard3
mkdir -p /home/soft/mongodbmoney/mongos

 

Config-Server 配置文件
路径:vi /home/soft/mongodbmoney/configsvr/mongod.conf

storage:
dbPath: /data/db
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
bindIp: 0.0.0.0
processManagement:
timeZoneInfo: /usr/share/zoneinfo
replication:
replSetName: cfg
sharding:
clusterRole: configsvr

 


Mongos 配置文件
路径:vi /home/soft/mongodbmoney/mongos/mongos.conf

systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongos.log
net:
port: 27020
bindIp: 0.0.0.0
processManagement:
fork: true
timeZoneInfo: /usr/share/zoneinfo
sharding:
configDB: cfg/192.168.5.201:27019,192.168.5.202:27019,192.168.5.203:27019

 

Shard-Server 配置文件1
路径:vi /home/soft/mongodbmoney/shard1/mongod.conf

storage:
dbPath: /data/db
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
bindIp: 0.0.0.0
processManagement:
timeZoneInfo: /usr/share/zoneinfo
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr

 

Shard-Server 配置文件2
路径:vi /home/soft/mongodbmoney/shard2/mongod.conf

storage:
dbPath: /data/db
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
bindIp: 0.0.0.0
processManagement:
timeZoneInfo: /usr/share/zoneinfo
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr

 

Shard-Server 配置文件3
路径:vi /home/soft/mongodbmoney/shard3/mongod.conf

storage:
dbPath: /data/db
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
bindIp: 0.0.0.0
processManagement:
timeZoneInfo: /usr/share/zoneinfo
replication:
replSetName: shard3
sharding:
clusterRole: shardsvr

 

启动Docker容器 启动3个Config-Server容器:

docker run -d --restart=always --name=cfg_1 -p 27019:27019 --network=mongodbnet -v /home/soft/mongodbmoney/configsvr:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf

 

启动3*3个Shard-Server容器:说明:分片服务器启动后默认是以27018作为端口。
# 启动第一个分片 - shard1

docker run -d --restart=always --name=shard1_1 -p 27018:27018 --network=mongodbnet -v /home/soft/mongodbmoney/shard1:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf

 

# 启动第二个分片 - shard2

docker run -d --restart=always --name=shard2_1 -p 27028:27018 --network=mongodbnet -v /home/soft/mongodbmoney/shard2:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf

 

# 启动第三个分片 - shard3

docker run -d --restart=always --name=shard3_1 -p 27038:27018 --network=mongodbnet -v /home/soft/mongodbmoney/shard3:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf

 

启动3个mongos服务器 说明:这里也使用了mongo镜像,但是需要开启mongos进程,mongod进程并不需要用到。

docker run -d --restart=always --name=mongos_1 -p 27017:27017 -p 27020:27020 --network=mongodbnet -v /home/soft/mongodbmoney/mongos:/etc/mongodb mongo:4.2.7

 

============集群过程========================
进入其中一个容器配置Config-Server副本集:

# 宿主机
docker exec -it cfg_1 bash
# 容器中
mongo --port 27019
# Mongo Shell中
rs.initiate({
"_id":"cfg",
"members":[
{
"_id":0,
"host":"192.168.5.201:27019"
},
{
"_id":1,
"host":"192.168.5.202:27019"
},
{
"_id":2,
"host":"192.168.5.203:27019"
}
]
})

 

进入其中一个容器配置Shard-Server1副本集:

# 宿主机
docker exec -it shard1_1 bash
# 容器中
mongo --port 27018
# Mongo Shell中
rs.initiate({
"_id":"shard1",
"members":[
{
"_id":0,
"host":"192.168.5.201:27018"
},
{
"_id":1,
"host":"192.168.5.202:27018"
},
{
"_id":2,
"host":"192.168.5.203:27018"
}
]
})

 

进入其中一个容器配置Shard-Server2副本集:

# 宿主机
docker exec -it shard2_1 bash
# 容器中
mongo --port 27018
# Mongo Shell中
rs.initiate({
"_id":"shard2",
"members":[
{
"_id":0,
"host":"192.168.5.201:27028"
},
{
"_id":1,
"host":"192.168.5.202:27028"
},
{
"_id":2,
"host":"192.168.5.203:27028"
}
]
})

 

进入其中一个容器配置Shard-Server3副本集:

# 宿主机
docker exec -it shard3_1 bash
# 容器中
mongo --port 27018
# Mongo Shell中
rs.initiate({
"_id":"shard3",
"members":[
{
"_id":0,
"host":"192.168.5.201:27038"
},
{
"_id":1,
"host":"192.168.5.202:27038"
},
{
"_id":2,
"host":"192.168.5.203:27038"
}
]
})

 

进入mongos容器中,启动mongos进程(此处可以改进一下,自动运行mongos进程)

# 宿主机
docker exec -it mongos_1 bash
# 容器中
mongos -f /etc/mongodb/mongos.conf

可以就在其中一个mongos容器中使用mongo shell连接mongos进程配置分片集群
# 连接mongos,端口号与mongos配置文件中设定一致
mongo -port 27020
# 将分片加入集群
sh.addShard("shard1/192.168.5.201:27018,192.168.5.202:27018,192.168.5.203:27018")
sh.addShard("shard2/192.168.5.201:27028,192.168.5.202:27028,192.168.5.203:27028")
sh.addShard("shard3/192.168.5.201:27038,192.168.5.202:27038,192.168.5.203:27038")

 

# 对数据库开启分片功能
sh.enableSharding("mydbtest")
# 对数据库中集合开启分片,并指定片键
sh.shardCollection("mydbtest.coll1",{"age":1})
sh.shardCollection("[dbName.collectionName]",{[keyName]:1})

 

6. 尝试写入数据观察数据分块

# 插入1000个简单的文档,耐心等待插入结束
for(var i=1;i<=1000;i++){
db.coll1.insert({
name:i,
age:Math.round(Math.random() * 100),
score1:Math.round(Math.random() * 100),
score2:Math.round(Math.random() * 100),
score3:Math.round(Math.random() * 100),
score4:Math.round(Math.random() * 100),
score5:Math.round(Math.random() * 100)
});
}

db.coll1.find().count()
db.coll1.find().limit(1)

# 查看分片状态
sh.status()

 

举报

相关推荐

0 条评论