安装简介
本文主要介绍了在Docker环境下如何安装Mongo集群的过程,Mongo集群采用单分片三副本模式进行部署。
服务器:bigdata1、bigdata2、bigdata3
部署服务:config-server、shard-server、mongos
Docker镜像:mongo:4.4.14
创建目录
创建mongo配置文件目录及数据存储目录。
mkdir /data/mongo-cluster
cd /data/mongo-cluster/ | mkdir config-server shard-server mongos
cd /data/mongo-cluster/config-server | mkdir data conf
cd /data/mongo-cluster/shard-server | mkdir data conf
cd /data/mongo-cluster/mongos | mkdir data conf
导入配置
(1)生成并配置认证文件:mongo.key
# 生成文件
openssl rand -base64 756 > /data/mongo-cluster/mongo.key
chmod 400 /data/mongo-cluster/mongo.key
# 复制文件到所有文件
cp /data/mongo-cluster/mongo.key /data/mongo-cluster/config-server/conf/
cp /data/mongo-cluster/mongo.key /data/mongo-cluster/mongos/conf/
cp /data/mongo-cluster/mongo.key /data/mongo-cluster/shard-server/conf/
rm -r /data/mongo-cluster/mongo.key
(2)在目录 /data/mongo-cluster/config-server/conf 下创建文件 mongo.conf
vim /data/mongo-cluster/config-server/conf/mongo.conf
# 日志文件
#systemLog:
# destination: file
# logAppend: true
# path: /var/log/mongodb/data0802.log
# 网络设置
net:
port: 27019 #端口号
# bindIp: 127.0.0.1 #绑定ip
replication:
replSetName: configsvr
sharding:
clusterRole: configsvr
security:
authorization: enabled #是否开启认证
keyFile: /data/configdb/mongo.key #keyFile路径
(3)在目录 /data/mongo-cluster/mongos/conf 下创建文件 mongo.conf
vim /data/mongo-cluster/mongos/conf/mongo.conf
net:
port: 27017 #端口号
sharding:
configDB: configsvr/bigdata1:27019,bigdata2:27019,bigdata3:27019
security:
keyFile: /data/configdb/mongo.key #keyFile路径
(4)在目录 /data/mongo-cluster/config-server/conf 下创建文件 mongo.conf
vim /data/mongo-cluster/shard-server/conf/mongo.conf
# 日志文件
#systemLog:
# destination: file
# logAppend: true
# path: /var/log/mongodb/data0802.log
# 网络设置
net:
port: 27018 #端口号
# bindIp: 127.0.0.1 #绑定ip
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
security:
authorization: enabled #是否开启认证
keyFile: /data/configdb/mongo.key #keyFile路径
(5)将文件分发到各个节点
scp -r /data/mongo-cluster root@bigdata2:/data/
scp -r /data/mongo-cluster root@bigdata3:/data/
容器部署
在所有节点都执行以下操作。
(1)config-server
sudo docker run -d \
--name config-server \
--net=host \
--privileged=true \
--restart=always \
-v /data/mongo-cluster/config-server/conf:/data/configdb \
-v /data/mongo-cluster/config-server/data:/data/db \
-v /etc/hosts:/etc/hosts \
-e TZ=Asia/Shanghai \
mongo:4.4.14 -f /data/configdb/mongo.conf --bind_ip_all
(2)mongos
sudo docker run -d \
--name mongos \
--net=host \
--privileged=true \
--restart=always \
--entrypoint "mongos" \
-v /msun/data/mongo-cluster/mongos/conf:/data/configdb \
-v /msun/data/mongo-cluster/mongos/data:/data/db \
-v /etc/hosts:/etc/hosts \
mongo:4.4.14 -f /data/configdb/mongo.conf --bind_ip_all
(3)shard-server
sudo docker run -d \
--name shard-server \
--net=host \
--privileged=true \
--restart=always \
-v /data/mongo-cluster/shard-server/conf:/data/configdb \
-v /data/mongo-cluster/shard-server/data:/data/db \
-v /etc/hosts:/etc/hosts \
-e TZ=Asia/Shanghai \
mongo:4.4.14 -f /data/configdb/mongo.conf --bind_ip_all
初始化Mongo
在bigdata1节点上执行以下操作。
(1)初始化config-server
sudo docker exec -it config-server bash
mongo -port 27019
# 进行副本集配置
rs.initiate(
{
_id: "configsvr",
members: [
{ _id : 1, host : "bigdata1:27019" },
{ _id : 2, host : "bigdata2:27019" },
{ _id : 3, host : "bigdata3:27019" }
]
}
);
# 创建用户密码
use admin
db.createUser({user:"root",pwd:"root",roles:[{role:'root',db:'admin'}]})
(2)初始化shard-serve
sudo docker exec -it shard-server bash
mongo -port 27018
# 进行副本集配置
rs.initiate(
{
_id : "shard1",
members: [
{ _id : 1, host : "bigdata1:27018" },
{ _id : 2, host : "bigdata2:27018" },
{ _id : 3, host : "bigdata3:27018" }
]
}
);
use admin
db.createUser({user:"root",pwd:"root",roles:[{role:'root',db:'admin'}]})
(3)为分片添加副本并测试
sudo docker exec -it mongos bash
mongo -port 27017
# 先登录(使用前面设置的root用户密码)
use admin;
db.auth("root","root");
sh.addShard("shard1/bigdata1:27018,bigdata2:27018,bigdata3:27018")
# 创建数据库
use dw;
# 为数据库启用分片
sh.enableSharding("dw");
# 为集合设置分片规则
sh.shardCollection("dw.test", { "_id": "hashed" })
# 数据验证
for (i = 1; i <= 1000; i++) {
db.getCollection("test").insert({'price': 1})
}
# 查询总数
db.getCollection("test").count()
标签:Mongo,--,server,cluster,集群,conf,Docker,data,mongo From: https://blog.51cto.com/u_13477304/6024167