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

https://blog.csdn.net/vincerom/article/details/81145938


镜像

docker pull mongo:4.2.7

 

网络

docker network create --subnet=10.20.0.0/24 mongodbnet

 

文件夹

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

 

Config-Server 配置文件
路径:vi /home/soft/mongodbone/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/mongodbone/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
路径:vi /home/soft/mongodbone/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/mongodbone/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/mongodbone/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 --network=mongodbnet --ip=10.20.0.2 -v /home/soft/mongodbone/configsvr:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
docker run -d --restart=always --name=cfg_2 --network=mongodbnet --ip=10.20.0.3 -v /home/soft/mongodbone/configsvr:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
docker run -d --restart=always --name=cfg_3 --network=mongodbnet --ip=10.20.0.4 -v /home/soft/mongodbone/configsvr:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf

 


进入其中一个容器配置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"
}
]
})

 


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

docker run -d --restart=always --name=shard1_1 --network=mongodbnet --ip=10.20.0.5 -v /home/soft/mongodbone/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 /home/soft/mongodbone/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 /home/soft/mongodbone/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

docker run -d --restart=always --name=shard2_1 --network=mongodbnet --ip=10.20.0.8 -v /home/soft/mongodbone/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 /home/soft/mongodbone/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 /home/soft/mongodbone/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

docker run -d --restart=always --name=shard3_1 --network=mongodbnet --ip=10.20.0.11 -v /home/soft/mongodbone/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 /home/soft/mongodbone/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 /home/soft/mongodbone/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"
}
]
})

 

 

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

docker run -d --restart=always --name=mongos_1 --network=mongodbnet --ip=10.20.0.14 -v /home/soft/mongodbone/mongos:/etc/mongodb mongo:4.2.7
docker run -d --restart=always --name=mongos_2 --network=mongodbnet --ip=10.20.0.15 -v /home/soft/mongodbone/mongos:/etc/mongodb mongo:4.2.7
docker run -d --restart=always --name=mongos_3 --network=mongodbnet --ip=10.20.0.16 -v /home/soft/mongodbone/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")

 

# 对数据库开启分片功能

sh.enableSharding("mydbtest")

 

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

sh.shardCollection("mydbtest.chenIndex",{"age":1})

 

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

# 插入3000个简单的文档,耐心等待插入结束这个文档5个字段name,age,score1,score2,score3,score4,score5
for(var i=1;i<=3000;i++){
db.chenIndex.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.chenIndex.find().count()
// 查询一条数据
db.chenIndex.find().limit(1)

 

# 查看分片状态

sh.status()

 

posted on 2020-09-12 10:21  陈惟鲜的博客  阅读(1090)  评论(0编辑  收藏  举报

导航