mac下docker安装 sharding集群

mac下docker安装 sharding集群

最近要做一个分享,在自己的mac上搭建了一个sharding集群,以下为部署记录:
参考文章:https://www.cnblogs.com/a393060727/p/13656038.html

1、下载镜像:

docker 部署mongodb 4.2.7 sharding集群;

2、创建网络:

docker network create --subnet=10.20.0.0/24 mongodbnet

3、创建文件夹:

cd /home/hequn/mongosharding/
mkdir configsvr shard1 shard2 shard3 mongos

4、创建配置文件:

Config-Server 配置文件

cd /home/hequn/mongosharding/
vi configsvr/configsvr.conf

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

Mongos 配置文件

cd /home/hequn/mongosharding/
vi 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/10.20.0.2:27019,10.20.0.3:27019,10.20.0.4:27019

Shard-Server 配置文件1
cd /home/hequn/mongosharding/
vi 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
cd /home/hequn/mongosharding/
路径:vi 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
cd /home/hequn/mongosharding/
路径:vi 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
5、启动Docker容器 启动3个Config-Server容器:
cd ~/mongosharding
docker run -d --restart=always --name=cfg_1 --network=mongodbnet --ip=10.20.0.2 -v $PWD/configsvr:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/configsvr.conf
docker run -d --restart=always --name=cfg_2 --network=mongodbnet --ip=10.20.0.3 -v $PWD/configsvr:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/configsvr.conf
docker run -d --restart=always --name=cfg_3 --network=mongodbnet --ip=10.20.0.4 -v $PWD/configsvr:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/configsvr.conf

-v /home/hequn/mongosharding/configsvr:/etc/mongodb : 将主机中当前目录下的/home/hequn/mongosharding/configsvr挂载到容器的/etc/mongodb

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

# 宿主机
docker exec -it cfg_1 bash
# 容器中
mongo --port 27019
# Mongo Shell中
rs.initiate({
"_id":"cfg",
"members":[
{
"_id":0,
"host":"10.20.0.2:27019"
},
{
"_id":1,
"host":"10.20.0.3:27019"
},
{
"_id":2,
"host":"10.20.0.4:27019"
}
]
})
6、启动3*3个Shard-Server容器:

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

cd ~/mongosharding
docker run -d --restart=always --name=shard1_1 --network=mongodbnet --ip=10.20.0.5 -v $PWD/shard1:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
docker run -d --restart=always --name=shard1_2 --network=mongodbnet --ip=10.20.0.6 -v $PWD/shard1:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
docker run -d --restart=always --name=shard1_3 --network=mongodbnet --ip=10.20.0.7 -v $PWD/shard1:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf

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

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

启动第二个分片 - shard2

cd ~/mongosharding
docker run -d --restart=always --name=shard2_1 --network=mongodbnet --ip=10.20.0.8 -v $PWD/shard2:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
docker run -d --restart=always --name=shard2_2 --network=mongodbnet --ip=10.20.0.9 -v $PWD/shard2:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
docker run -d --restart=always --name=shard2_3 --network=mongodbnet --ip=10.20.0.10 -v $PWD/shard2:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf

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

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

启动第三个分片 - shard3

cd ~/mongosharding
docker run -d --restart=always --name=shard3_1 --network=mongodbnet --ip=10.20.0.11 -v $PWD/shard3:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
docker run -d --restart=always --name=shard3_2 --network=mongodbnet --ip=10.20.0.12 -v $PWD/shard3:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
docker run -d --restart=always --name=shard3_3 --network=mongodbnet --ip=10.20.0.13 -v $PWD/shard3:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf

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

# 宿主机
docker exec -it shard3_1 bash
# 容器中
mongo --port 27018
# Mongo Shell中
rs.initiate({
"_id":"shard3",
"members":[
{
"_id":0,
"host":"10.20.0.11:27018"
},
{
"_id":1,
"host":"10.20.0.12:27018"
},
{
"_id":2,
"host":"10.20.0.13:27018"
}
]
})
7、启动3个mongos服务器

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

cd ~/mongosharding
docker run -d --restart=always --name=mongos_1 --network=mongodbnet --ip=10.20.0.14 -v $PWD/mongos:/etc/mongodb mongo:4.2.7
docker run -d --restart=always --name=mongos_2 --network=mongodbnet --ip=10.20.0.15 -v $PWD/mongos:/etc/mongodb mongo:4.2.7
docker run -d -p 27020:27020 --restart=always --name=mongos_3 --network=mongodbnet --ip=10.20.0.16 -v $PWD/mongos:/etc/mongodb mongo:4.2.7

进入每个容器中,启动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/10.20.0.5:27018,10.20.0.6:27018,10.20.0.7:27018")
sh.addShard("shard2/10.20.0.8:27018,10.20.0.9:27018,10.20.0.10:27018")
sh.addShard("shard3/10.20.0.11:27018,10.20.0.12:27018,10.20.0.13:27018")
8、对数据库开启分片功能
sh.enableSharding("gamedbtest")

对数据库中集合开启分片,并指定sharding key
sh.shardCollection("[dbName.collectionName]",{[keyName]:1})

sh.shardCollection("gamedbtest.score",{"age":1})

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

# 插入30000个简单的文档,耐心等待插入结束这个文档5个字段name,age,score1,score2,score3,score4,score5
for(var i=1;i<=30000;i++){
    db.score.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)
    });
}

查看分片状态

sh.status()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值