乐趣区

关于mongodb:手把手超详细Docker部署MongoDB集群

Mongodb 集群搭建

mongodb 集群搭建的形式有三种:

  1. 主从备份(Master – Slave)模式,或者叫主从复制模式。
  2. 正本集(Replica Set)模式。
  3. 分片(Sharding)模式。

其中,第一种形式根本没什么意义,官网也不举荐这种形式搭建。另外两种别离就是正本集和分片的形式。明天介绍正本集的形式搭建 mongodb 高可用集群

简介以及概述

​ 首先咱们先来理解一下 Mongo 集群的概念,Mongo 集群有 3 个次要组件

​ ConfigServer:在集群中表演存储整个集群的配置信息,负责配置存储,如果须要高可用的 ConfigServer 那么须要 3 个节点。

​ Shard:分片,存储实在的数据,每一个 Shard 分片都负责存储集群中的数据,例如一个集群有 3 个分片,而后咱们定义分片规定为哈希,那么整个集群的数据就会(宰割)到 3 个分片中的某一个分片,那么分片是特地重要的,如果集群中的一个分片全副解体了那么集群将不可用,所以咱们要保障集群的高可用,那么咱们须要一个分片配置 3 个节点,2 个正本集一个仲裁节点,仲裁节点相似于 Redis 的哨兵模式,如果发现主节点挂了那么让另一个正本集进行数据存储。

​ Mongos:Mongos 咱们能够了解为整个集群的入口,相似于 Kafka 的 Broker 代理,也就是客户端,咱们通过客户端连贯集群进行查问。

​ 上面是 MongoDB 的官网集群架构图,咱们看到 Mongos 是一个路由,他们的信息都存储在 ConfigServer 中,咱们通过 Mongos 进行增加,而后依据条件将数据进行分片到分片的正本集中。


Mongo 分片集群 (非高可用, 不举荐)

搭建 ConfigServer

首先咱们搭建两个 config-server

创立两个 config-server 的配置文件

# 创立 config-server-1
mkdir -p /docker/mongo-cluster/mongo-server1/{data,conf}

#创立 config-server-2
mkdir -p /docker/mongo-cluster/mongo-server2/{data,conf}

而后配置文件中配置端口

# 创立第一个配置文件
#写入配置信息,端口号

echo "# mongodb.conf
logappend=true
# bind_ip=127.0.0.1
port=20011
auth=false" > /docker/mongo-cluster/mongo-server1/conf/mongo.conf

#创立第二个配置文件
#写入配置信息,端口号

echo "# mongodb.conf
logappend=true
# bind_ip=127.0.0.1
port=20012
auth=false" > /docker/mongo-cluster/mongo-server2/conf/mongo.conf

而后启动容器

# 启动 Server1

docker run --name mongo-server1 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/mongo-server1/conf:/data/configdb \
-v /docker/mongo-cluster/mongo-server1/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf --configsvr --replSet "rs_config_server" --bind_ip_all 

docker run --name mongo-server2 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/mongo-server2/conf:/data/configdb \
-v /docker/mongo-cluster/mongo-server2/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf --configsvr --replSet "rs_config_server" --bind_ip_all 

而后进入容器初始化

# 进入容器
docker exec -it mongo-server1 bash

mongo -port 20011

#初始化
rs.initiate(
{
_id: "rs_config_server",
configsvr: true,
members: [{ _id : 0, host : "114.67.80.169:20011"},
{_id : 1, host : "114.67.80.169:20012"}
]
}
);

如果 ok 为 1 示意胜利

创立分片集群

上面咱们给每个 server 创立 2 个分片

创立挂载文件

# 创立 config-server- 1 的两个分片目录
mkdir -p /docker/mongo-cluster/mongo-server1-shard1/{data,conf}
mkdir -p /docker/mongo-cluster/mongo-server1-shard2/{data,conf}

#创立 config-server- 2 的两个分片目录
mkdir -p /docker/mongo-cluster/mongo-server2-shard1/{data,conf}
mkdir -p /docker/mongo-cluster/mongo-server2-shard2/{data,conf}

创立配置文件

# 创立 config-server- 1 的两个分片配置文件

echo "# mongodb.conf
logappend=true
# bind_ip=127.0.0.1
port=20021
auth=false" > /docker/mongo-cluster/mongo-server1-shard1/conf/mongo.conf

echo "# mongodb.conf
logappend=true
# bind_ip=127.0.0.1
port=20022
auth=false" > /docker/mongo-cluster/mongo-server1-shard2/conf/mongo.conf
# 创立 config-server- 2 的两个分片配置文件

echo "# mongodb.conf
logappend=true
# bind_ip=127.0.0.1
port=20023
auth=false" > /docker/mongo-cluster/mongo-server2-shard1/conf/mongo.conf

echo "# mongodb.conf
logappend=true
# bind_ip=127.0.0.1
port=20024
auth=false" > /docker/mongo-cluster/mongo-server2-shard2/conf/mongo.conf

而后启动容器

# 启动 config-server- 1 的两个分片容器
docker run --name mongo-server1-shard1 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/mongo-server1-shard1/conf:/data/configdb \
-v /docker/mongo-cluster/mongo-server1-shard1/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf --shardsvr --replSet "rs_shard_server1" --bind_ip_all

docker run --name mongo-server1-shard2 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/mongo-server1-shard2/conf:/data/configdb \
-v /docker/mongo-cluster/mongo-server1-shard2/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf --shardsvr --replSet "rs_shard_server1" --bind_ip_all
# 启动 config-server- 2 的两个分片容器
docker run --name mongo-server2-shard1 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/mongo-server2-shard1/conf:/data/configdb \
-v /docker/mongo-cluster/mongo-server2-shard1/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf --shardsvr --replSet "rs_shard_server2" --bind_ip_all

docker run --name mongo-server2-shard2 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/mongo-server2-shard2/conf:/data/configdb \
-v /docker/mongo-cluster/mongo-server2-shard2/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf --shardsvr --replSet "rs_shard_server2" --bind_ip_all

进入第一个分片

 docker exec  -it mongo-server1-shard1 bash
 mongo -port 20021
 
#进行分片
 rs.initiate(
{
_id : "rs_shard_server1",
members: [{ _id : 0, host : "114.67.80.169:20021"},
{_id : 1, host : "114.67.80.169:20022"}
]
}
);

进入第二个分片

 docker exec  -it mongo-server2-shard1 bash
 mongo -port 20023
 
 #进行分片
 rs.initiate(
{
_id : "rs_shard_server2",
members: [{ _id : 0, host : "182.61.2.16:20023"},
{_id : 1, host : "182.61.2.16:20024"}
]
}
);

装置 Mongos

创立挂载文件

mkdir -p /docker/mongo-cluster/mongos1/{data,conf}

echo "# mongodb.conf
logappend=true
# bind_ip=127.0.0.1
port=20099
auth=false" > /docker/mongo-cluster/mongos1/conf/mongo.conf

而后启动 Mongo

docker run --name mongo-mongos1 -d \
--net=host \
--privileged=true \
-p 27017:27017 \
--entrypoint "mongos" \
-v /docker/mongo-cluster/mongos1/conf:/data/configdb \
-v /docker/mongo-cluster/mongos1/data:/data/db \
docker.io/mongo:latest \
--configdb rs_config_server/114.67.80.169:20011,114.67.80.169:20012 --bind_ip_all

mongo 增加分片组

sh.addShard("rs_shard_server1/114.67.80.169:20021,114.67.80.169:20022")
sh.addShard("rs_shard_server2/182.61.2.16:20023,182.61.2.16:20024")

新建数据启用分片

sh.enableSharding("test")

对 test.order 的_id 进行哈希分片
sh.shardCollection("test.order", {"_id": "hashed"})
 插入数据后查看分片数据
use test
for (i = 1; i <= 1000; i=i+1){db.order.insert({'price': 1})}

Mongo 分片集群高可用 + 权限(举荐)

那么咱们先来总结一下咱们搭建一个高可用集群须要多少个 Mongo

mongos:3 台

configserver:3 台

shard:3 片

每一片 shard 别离 部署两个正本集和一个仲裁节点:3 台

那么就是 3 + 3 + 3 * 3 = 15 台,我这里演示采纳 3 台服务器

​ 114.67.80.169 4 核 16g 部署一个 configserver,一个 mongos,2 个分片组

​ 182.61.2.16 2 核 4g 部署一个 configserver,一个 mongos,1 个分片组

​ 106.12.113.62 1 核 2g 部署一个 configserver,一个 mongos,不搭建分片组

因为此处服务器起因所以不是平衡散布,请依据本身理论状况搭建

角色 ip 端口
config-server1 114.67.80.169 20011
config-server2 182.61.2.16 20012
config-server3 106.12.113.62 20013
mongos1 114.67.80.169 20021
mongos2 182.61.2.16 20022
mongos3 106.12.113.62 20023
shard1-server1 114.67.80.169 20031
shard1-server2 114.67.80.169 20032
shard1-server3 114.67.80.169 20033
shard2-server1 114.67.80.169 20034
shard2-server2 114.67.80.169 20035
shard2-server3 114.67.80.169 20036
shard3-server1 182.61.2.16 20037
shard3-server2 182.61.2.16 20038
shard3-server3 182.61.2.16 20039

搭建 ConfigServer

​ 咱们先来搭建 ConfigServer,因为咱们晓得搭建的话肯定要高可用而且肯定要权限这里 mongo 之间通信采纳秘钥文件,所以咱们先进行生成

搭建 config-server1

创立挂载文件目录

mkdir -p /docker/mongo-cluster/config-server1/{data,conf}

写入配置文件

echo "
# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 20011  #端口号
#  bindIp: 127.0.0.1    #绑定 ip
replication:
  replSetName: configsvr
sharding:
  clusterRole: configsvr
security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 "  > /docker/mongo-cluster/config-server1/conf/mongo.conf

而后生成 keyFile

openssl rand -base64 756  > /docker/mongo-cluster/config-server1/conf/mongo.key

文件如下,咱们,之后咱们所以 key 都采纳这个(请采纳本人生成的 key)

tsUtJb3TeueNR8Mehr7ZLmZx82qfuCQ7LfLjUvQA7hNfWSomyNDISXDiSTJQEVym
OhXXzwB+0iv+czxi4qe9tAP8fMDuXpieZreysg4gxZ1VoFC1q39IrUDAEpCikSKS
abGl8RTEOM/GzVM8BATjaGHuBIi2osBAPg2Hzi+/u9ORbb4I4jzvgStcPcozRgOZ
5kPvXBybanV8MhLA6MfG1rcUiTkGoKb65YuWIfPuuF7PTWZe4VcF+iU6jgw73juZ
pbcZR5oTKvOWz89KCRTmQqHRexmJyn+NJcIGHFS/sZSJXE8LFPBZ+XLGYrtmDqo0
9tA1x8R+u32OJ7iOAU1mFkCHe2Uoph6aeVx/jZx1FgFjW0afT4ou2w7QHsdF0WRn
nskJ1FCA8NKzhYYgv/YrpyAChhTgd//gbWr028qz1W1POpBkj4muKUk7OTHRV6bs
qr2C73bqcZ1n2s60k6WbRUd6LP6POHR93wvi5EaXyorSMBIGiSD1Kyr/iqO7gD4C
GN8iA3MqF+fW5nKn1yBNEfPGoFk+p0EaxIAhfLEpzSRb3Wt5XLOWP7CBGuTo7KST
Y5HAcblqN7TByQhLdH5MZJ4FhfTZ0yNKTOVQdZUYRb5GGgS0GZfUk4bndLTkHrJd
tcR4WreHpz7ccncE5Vt8TGglrEx0noFVBqLqTdrqFUFpvWoukw/eViacLlBHKOxB
QVgfo4491znNMmthqGimVI7TFV706AvVJGqoIyuiFZRE5qx5MsOlIXiFwA3ue1Lo
kiFq5c6ImvS0R9LGu1Xcr0REYN53/bBVgGzJovEn7IIrHChYow7TkTLf/LsnjL3m
rmkDRgzA0C5i6fXgKkJdBhvvA521Yf75YP9n+819NUTZbtGIxRnP07pMS9RP4TjS
ZSd9an5yc7IpnL0gE4Pmnvf8LM86WTt9hZWKrE2LeQPEFgFl/Eq5NH60Zd4utxfi
qM2FH7aNsEukoAvA2v3All1wsM2kn4fMa89Hwui9h4xMy5tU

写入 key 文件

echo "tsUtJb3TeueNR8Mehr7ZLmZx82qfuCQ7LfLjUvQA7hNfWSomyNDISXDiSTJQEVym
OhXXzwB+0iv+czxi4qe9tAP8fMDuXpieZreysg4gxZ1VoFC1q39IrUDAEpCikSKS
abGl8RTEOM/GzVM8BATjaGHuBIi2osBAPg2Hzi+/u9ORbb4I4jzvgStcPcozRgOZ
5kPvXBybanV8MhLA6MfG1rcUiTkGoKb65YuWIfPuuF7PTWZe4VcF+iU6jgw73juZ
pbcZR5oTKvOWz89KCRTmQqHRexmJyn+NJcIGHFS/sZSJXE8LFPBZ+XLGYrtmDqo0
9tA1x8R+u32OJ7iOAU1mFkCHe2Uoph6aeVx/jZx1FgFjW0afT4ou2w7QHsdF0WRn
nskJ1FCA8NKzhYYgv/YrpyAChhTgd//gbWr028qz1W1POpBkj4muKUk7OTHRV6bs
qr2C73bqcZ1n2s60k6WbRUd6LP6POHR93wvi5EaXyorSMBIGiSD1Kyr/iqO7gD4C
GN8iA3MqF+fW5nKn1yBNEfPGoFk+p0EaxIAhfLEpzSRb3Wt5XLOWP7CBGuTo7KST
Y5HAcblqN7TByQhLdH5MZJ4FhfTZ0yNKTOVQdZUYRb5GGgS0GZfUk4bndLTkHrJd
tcR4WreHpz7ccncE5Vt8TGglrEx0noFVBqLqTdrqFUFpvWoukw/eViacLlBHKOxB
QVgfo4491znNMmthqGimVI7TFV706AvVJGqoIyuiFZRE5qx5MsOlIXiFwA3ue1Lo
kiFq5c6ImvS0R9LGu1Xcr0REYN53/bBVgGzJovEn7IIrHChYow7TkTLf/LsnjL3m
rmkDRgzA0C5i6fXgKkJdBhvvA521Yf75YP9n+819NUTZbtGIxRnP07pMS9RP4TjS
ZSd9an5yc7IpnL0gE4Pmnvf8LM86WTt9hZWKrE2LeQPEFgFl/Eq5NH60Zd4utxfi
qM2FH7aNsEukoAvA2v3All1wsM2kn4fMa89Hwui9h4xMy5tU"  > /docker/mongo-cluster/config-server1/conf/mongo.key

#解决权限为 400

chmod 400 /docker/mongo-cluster/config-server1/conf/mongo.key

而后启动 config-server1 容器

docker run --name mongo-server1 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/config-server1/conf:/data/configdb \
-v /docker/mongo-cluster/config-server1/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf

搭建 config-server2

创立挂载文件目录

mkdir -p /docker/mongo-cluster/config-server2/{data,conf}

写入配置文件

写入配置文件

echo "
# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 20012  #端口号
#  bindIp: 127.0.0.1    #绑定 ip
replication:
  replSetName: configsvr
sharding:
  clusterRole: configsvr
security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 "  > /docker/mongo-cluster/config-server2/conf/mongo.conf

文件如下,咱们,之后咱们所以 key 都采纳这个(请采纳本人生成的 key)

写入 key 文件

echo "tsUtJb3TeueNR8Mehr7ZLmZx82qfuCQ7LfLjUvQA7hNfWSomyNDISXDiSTJQEVym
OhXXzwB+0iv+czxi4qe9tAP8fMDuXpieZreysg4gxZ1VoFC1q39IrUDAEpCikSKS
abGl8RTEOM/GzVM8BATjaGHuBIi2osBAPg2Hzi+/u9ORbb4I4jzvgStcPcozRgOZ
5kPvXBybanV8MhLA6MfG1rcUiTkGoKb65YuWIfPuuF7PTWZe4VcF+iU6jgw73juZ
pbcZR5oTKvOWz89KCRTmQqHRexmJyn+NJcIGHFS/sZSJXE8LFPBZ+XLGYrtmDqo0
9tA1x8R+u32OJ7iOAU1mFkCHe2Uoph6aeVx/jZx1FgFjW0afT4ou2w7QHsdF0WRn
nskJ1FCA8NKzhYYgv/YrpyAChhTgd//gbWr028qz1W1POpBkj4muKUk7OTHRV6bs
qr2C73bqcZ1n2s60k6WbRUd6LP6POHR93wvi5EaXyorSMBIGiSD1Kyr/iqO7gD4C
GN8iA3MqF+fW5nKn1yBNEfPGoFk+p0EaxIAhfLEpzSRb3Wt5XLOWP7CBGuTo7KST
Y5HAcblqN7TByQhLdH5MZJ4FhfTZ0yNKTOVQdZUYRb5GGgS0GZfUk4bndLTkHrJd
tcR4WreHpz7ccncE5Vt8TGglrEx0noFVBqLqTdrqFUFpvWoukw/eViacLlBHKOxB
QVgfo4491znNMmthqGimVI7TFV706AvVJGqoIyuiFZRE5qx5MsOlIXiFwA3ue1Lo
kiFq5c6ImvS0R9LGu1Xcr0REYN53/bBVgGzJovEn7IIrHChYow7TkTLf/LsnjL3m
rmkDRgzA0C5i6fXgKkJdBhvvA521Yf75YP9n+819NUTZbtGIxRnP07pMS9RP4TjS
ZSd9an5yc7IpnL0gE4Pmnvf8LM86WTt9hZWKrE2LeQPEFgFl/Eq5NH60Zd4utxfi
qM2FH7aNsEukoAvA2v3All1wsM2kn4fMa89Hwui9h4xMy5tU"  > /docker/mongo-cluster/config-server2/conf/mongo.key

#解决权限为 400

chmod 400 /docker/mongo-cluster/config-server2/conf/mongo.key

而后启动 config-server2 容器

docker run --name mongo-server2 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/config-server2/conf:/data/configdb \
-v /docker/mongo-cluster/config-server2/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf

搭建 config-server3

创立挂载文件目录

mkdir -p /docker/mongo-cluster/config-server3/{data,conf}

写入配置文件

echo "
# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 20013  #端口号
#  bindIp: 127.0.0.1    #绑定 ip
replication:
  replSetName: configsvr
sharding:
  clusterRole: configsvr
security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 "  > /docker/mongo-cluster/config-server3/conf/mongo.conf

文件如下,咱们,之后咱们所以 key 都采纳这个(请采纳本人生成的 key)

写入 key 文件

echo "tsUtJb3TeueNR8Mehr7ZLmZx82qfuCQ7LfLjUvQA7hNfWSomyNDISXDiSTJQEVym
OhXXzwB+0iv+czxi4qe9tAP8fMDuXpieZreysg4gxZ1VoFC1q39IrUDAEpCikSKS
abGl8RTEOM/GzVM8BATjaGHuBIi2osBAPg2Hzi+/u9ORbb4I4jzvgStcPcozRgOZ
5kPvXBybanV8MhLA6MfG1rcUiTkGoKb65YuWIfPuuF7PTWZe4VcF+iU6jgw73juZ
pbcZR5oTKvOWz89KCRTmQqHRexmJyn+NJcIGHFS/sZSJXE8LFPBZ+XLGYrtmDqo0
9tA1x8R+u32OJ7iOAU1mFkCHe2Uoph6aeVx/jZx1FgFjW0afT4ou2w7QHsdF0WRn
nskJ1FCA8NKzhYYgv/YrpyAChhTgd//gbWr028qz1W1POpBkj4muKUk7OTHRV6bs
qr2C73bqcZ1n2s60k6WbRUd6LP6POHR93wvi5EaXyorSMBIGiSD1Kyr/iqO7gD4C
GN8iA3MqF+fW5nKn1yBNEfPGoFk+p0EaxIAhfLEpzSRb3Wt5XLOWP7CBGuTo7KST
Y5HAcblqN7TByQhLdH5MZJ4FhfTZ0yNKTOVQdZUYRb5GGgS0GZfUk4bndLTkHrJd
tcR4WreHpz7ccncE5Vt8TGglrEx0noFVBqLqTdrqFUFpvWoukw/eViacLlBHKOxB
QVgfo4491znNMmthqGimVI7TFV706AvVJGqoIyuiFZRE5qx5MsOlIXiFwA3ue1Lo
kiFq5c6ImvS0R9LGu1Xcr0REYN53/bBVgGzJovEn7IIrHChYow7TkTLf/LsnjL3m
rmkDRgzA0C5i6fXgKkJdBhvvA521Yf75YP9n+819NUTZbtGIxRnP07pMS9RP4TjS
ZSd9an5yc7IpnL0gE4Pmnvf8LM86WTt9hZWKrE2LeQPEFgFl/Eq5NH60Zd4utxfi
qM2FH7aNsEukoAvA2v3All1wsM2kn4fMa89Hwui9h4xMy5tU"  > /docker/mongo-cluster/config-server3/conf/mongo.key

#解决权限为 400

chmod 400 /docker/mongo-cluster/config-server3/conf/mongo.key

而后启动 config-server3 容器

docker run --name mongo-server3 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/config-server3/conf:/data/configdb \
-v /docker/mongo-cluster/config-server3/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf

初始化 config-server

进入第一台容器

docker exec -it mongo-server1 bash
mongo -port 20011

输出

rs.initiate(
  {
    _id: "configsvr",
    members: [{ _id : 1, host : "114.67.80.169:20011"},
      {_id : 2, host : "182.61.2.16:20012"},
      {_id : 3, host : "106.12.113.62:20013"}
    ]
  }
)

如果返回 ok 则胜利

而后咱们创立用户

use admin
db.createUser({user:"root",pwd:"root",roles:[{role:'root',db:'admin'}]})

搭建 Shard 分片组

​ 因为 mongos 是客户端,所以咱们先搭建好 config 以及 shard 之后再搭建 mongos。

搭建 shard1 分片组

在同一台服务器上初始化一组分片

创立挂载文件

mkdir -p /docker/mongo-cluster/shard1-server1/{data,conf}
mkdir -p /docker/mongo-cluster/shard1-server2/{data,conf}
mkdir -p /docker/mongo-cluster/shard1-server3/{data,conf}

配置配置文件

echo "
# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 20031  #端口号
#  bindIp: 127.0.0.1    #绑定 ip
replication:
  replSetName: shard1
sharding:
  clusterRole: shardsvr
security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 "  > /docker/mongo-cluster/shard1-server1/conf/mongo.conf
------------------------------------------------------------------------------
echo "
# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 20032  #端口号
#  bindIp: 127.0.0.1    #绑定 ip
replication:
  replSetName: shard1
sharding:
  clusterRole: shardsvr
security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 "  > /docker/mongo-cluster/shard1-server2/conf/mongo.conf
------------------------------------------------------------------------------
echo "
# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 20033  #端口号
#  bindIp: 127.0.0.1    #绑定 ip
replication:
  replSetName: shard1
sharding:
  clusterRole: shardsvr
security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 "  > /docker/mongo-cluster/shard1-server3/conf/mongo.conf

创立 keyfile

echo "tsUtJb3TeueNR8Mehr7ZLmZx82qfuCQ7LfLjUvQA7hNfWSomyNDISXDiSTJQEVym
OhXXzwB+0iv+czxi4qe9tAP8fMDuXpieZreysg4gxZ1VoFC1q39IrUDAEpCikSKS
abGl8RTEOM/GzVM8BATjaGHuBIi2osBAPg2Hzi+/u9ORbb4I4jzvgStcPcozRgOZ
5kPvXBybanV8MhLA6MfG1rcUiTkGoKb65YuWIfPuuF7PTWZe4VcF+iU6jgw73juZ
pbcZR5oTKvOWz89KCRTmQqHRexmJyn+NJcIGHFS/sZSJXE8LFPBZ+XLGYrtmDqo0
9tA1x8R+u32OJ7iOAU1mFkCHe2Uoph6aeVx/jZx1FgFjW0afT4ou2w7QHsdF0WRn
nskJ1FCA8NKzhYYgv/YrpyAChhTgd//gbWr028qz1W1POpBkj4muKUk7OTHRV6bs
qr2C73bqcZ1n2s60k6WbRUd6LP6POHR93wvi5EaXyorSMBIGiSD1Kyr/iqO7gD4C
GN8iA3MqF+fW5nKn1yBNEfPGoFk+p0EaxIAhfLEpzSRb3Wt5XLOWP7CBGuTo7KST
Y5HAcblqN7TByQhLdH5MZJ4FhfTZ0yNKTOVQdZUYRb5GGgS0GZfUk4bndLTkHrJd
tcR4WreHpz7ccncE5Vt8TGglrEx0noFVBqLqTdrqFUFpvWoukw/eViacLlBHKOxB
QVgfo4491znNMmthqGimVI7TFV706AvVJGqoIyuiFZRE5qx5MsOlIXiFwA3ue1Lo
kiFq5c6ImvS0R9LGu1Xcr0REYN53/bBVgGzJovEn7IIrHChYow7TkTLf/LsnjL3m
rmkDRgzA0C5i6fXgKkJdBhvvA521Yf75YP9n+819NUTZbtGIxRnP07pMS9RP4TjS
ZSd9an5yc7IpnL0gE4Pmnvf8LM86WTt9hZWKrE2LeQPEFgFl/Eq5NH60Zd4utxfi
qM2FH7aNsEukoAvA2v3All1wsM2kn4fMa89Hwui9h4xMy5tU"  > /docker/mongo-cluster/shard1-server1/conf/mongo.key

#解决权限为 400

chmod 400 /docker/mongo-cluster/shard1-server1/conf/mongo.key

#复制
cp /docker/mongo-cluster/shard1-server1/conf/mongo.key /docker/mongo-cluster/shard1-server2/conf/mongo.key

cp /docker/mongo-cluster/shard1-server1/conf/mongo.key /docker/mongo-cluster/shard1-server3/conf/mongo.key

运行 shard1 分片组

docker run --name shard1-server1 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/shard1-server1/conf:/data/configdb \
-v /docker/mongo-cluster/shard1-server1/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf

docker run --name shard1-server2 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/shard1-server2/conf:/data/configdb \
-v /docker/mongo-cluster/shard1-server2/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf

docker run --name shard1-server3 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/shard1-server3/conf:/data/configdb \
-v /docker/mongo-cluster/shard1-server3/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf

初始化 shard1 分片组

并且制订第三个正本集为仲裁节点

 docker exec  -it shard1-server1 bash
 mongo -port 20031
 
#进行正本集配置
 rs.initiate(
{
_id : "shard1",
members: [{ _id : 0, host : "114.67.80.169:20031"},
{_id : 1, host : "114.67.80.169:20032"},
{_id : 2, host : "114.67.80.169:20033",arbiterOnly:true}
]
}
);

返回 ok 后创立用户

use admin
db.createUser({user:"root",pwd:"root",roles:[{role:'root',db:'admin'}]})

而后退出,分片组 1 搭建实现

搭建 shard2 分片组

在同一台服务器上初始化一组分片

创立挂载文件

mkdir -p /docker/mongo-cluster/shard2-server1/{data,conf}
mkdir -p /docker/mongo-cluster/shard2-server2/{data,conf}
mkdir -p /docker/mongo-cluster/shard2-server3/{data,conf}

配置配置文件

echo "
# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 20034  #端口号
#  bindIp: 127.0.0.1    #绑定 ip
replication:
  replSetName: shard2
sharding:
  clusterRole: shardsvr
security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 "  > /docker/mongo-cluster/shard2-server1/conf/mongo.conf
------------------------------------------------------------------------------
echo "
# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 20035  #端口号
#  bindIp: 127.0.0.1    #绑定 ip
replication:
  replSetName: shard2
sharding:
  clusterRole: shardsvr
security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 "  > /docker/mongo-cluster/shard2-server2/conf/mongo.conf
------------------------------------------------------------------------------
echo "
# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 20036  #端口号
#  bindIp: 127.0.0.1    #绑定 ip
replication:
  replSetName: shard2
sharding:
  clusterRole: shardsvr
security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 "  > /docker/mongo-cluster/shard2-server3/conf/mongo.conf

创立 keyfile

echo "tsUtJb3TeueNR8Mehr7ZLmZx82qfuCQ7LfLjUvQA7hNfWSomyNDISXDiSTJQEVym
OhXXzwB+0iv+czxi4qe9tAP8fMDuXpieZreysg4gxZ1VoFC1q39IrUDAEpCikSKS
abGl8RTEOM/GzVM8BATjaGHuBIi2osBAPg2Hzi+/u9ORbb4I4jzvgStcPcozRgOZ
5kPvXBybanV8MhLA6MfG1rcUiTkGoKb65YuWIfPuuF7PTWZe4VcF+iU6jgw73juZ
pbcZR5oTKvOWz89KCRTmQqHRexmJyn+NJcIGHFS/sZSJXE8LFPBZ+XLGYrtmDqo0
9tA1x8R+u32OJ7iOAU1mFkCHe2Uoph6aeVx/jZx1FgFjW0afT4ou2w7QHsdF0WRn
nskJ1FCA8NKzhYYgv/YrpyAChhTgd//gbWr028qz1W1POpBkj4muKUk7OTHRV6bs
qr2C73bqcZ1n2s60k6WbRUd6LP6POHR93wvi5EaXyorSMBIGiSD1Kyr/iqO7gD4C
GN8iA3MqF+fW5nKn1yBNEfPGoFk+p0EaxIAhfLEpzSRb3Wt5XLOWP7CBGuTo7KST
Y5HAcblqN7TByQhLdH5MZJ4FhfTZ0yNKTOVQdZUYRb5GGgS0GZfUk4bndLTkHrJd
tcR4WreHpz7ccncE5Vt8TGglrEx0noFVBqLqTdrqFUFpvWoukw/eViacLlBHKOxB
QVgfo4491znNMmthqGimVI7TFV706AvVJGqoIyuiFZRE5qx5MsOlIXiFwA3ue1Lo
kiFq5c6ImvS0R9LGu1Xcr0REYN53/bBVgGzJovEn7IIrHChYow7TkTLf/LsnjL3m
rmkDRgzA0C5i6fXgKkJdBhvvA521Yf75YP9n+819NUTZbtGIxRnP07pMS9RP4TjS
ZSd9an5yc7IpnL0gE4Pmnvf8LM86WTt9hZWKrE2LeQPEFgFl/Eq5NH60Zd4utxfi
qM2FH7aNsEukoAvA2v3All1wsM2kn4fMa89Hwui9h4xMy5tU"  > /docker/mongo-cluster/shard2-server1/conf/mongo.key

#解决权限为 400

chmod 400 /docker/mongo-cluster/shard2-server1/conf/mongo.key

#复制
cp /docker/mongo-cluster/shard2-server1/conf/mongo.key /docker/mongo-cluster/shard2-server2/conf/mongo.key

cp /docker/mongo-cluster/shard2-server1/conf/mongo.key /docker/mongo-cluster/shard2-server3/conf/mongo.key

运行 shard2 分片组

docker run --name shard2-server1 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/shard2-server1/conf:/data/configdb \
-v /docker/mongo-cluster/shard2-server1/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf

docker run --name shard2-server2 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/shard2-server2/conf:/data/configdb \
-v /docker/mongo-cluster/shard2-server2/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf

docker run --name shard2-server3 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/shard2-server3/conf:/data/configdb \
-v /docker/mongo-cluster/shard2-server3/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf

初始化 shard2 分片组

并且制订第三个正本集为仲裁节点

 docker exec  -it shard2-server1 bash
 mongo -port 20034
 
#进行正本集配置
 rs.initiate(
{
_id : "shard2",
members: [{ _id : 0, host : "114.67.80.169:20034"},
{_id : 1, host : "114.67.80.169:20035"},
{_id : 2, host : "114.67.80.169:20036",arbiterOnly:true}
]
}
);

返回 ok 后创立用户

use admin
db.createUser({user:"root",pwd:"root",roles:[{role:'root',db:'admin'}]})

而后退出,分片组 2 搭建实现

搭建 shard3 分片组

在同一台服务器上初始化一组分片

创立挂载文件

mkdir -p /docker/mongo-cluster/shard3-server1/{data,conf}
mkdir -p /docker/mongo-cluster/shard3-server2/{data,conf}
mkdir -p /docker/mongo-cluster/shard3-server3/{data,conf}

配置配置文件

echo "
# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 20037  #端口号
#  bindIp: 127.0.0.1    #绑定 ip
replication:
  replSetName: shard3
sharding:
  clusterRole: shardsvr
security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 "  > /docker/mongo-cluster/shard3-server1/conf/mongo.conf
------------------------------------------------------------------------------
echo "
# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 20038  #端口号
#  bindIp: 127.0.0.1    #绑定 ip
replication:
  replSetName: shard3
sharding:
  clusterRole: shardsvr
security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 "  > /docker/mongo-cluster/shard3-server2/conf/mongo.conf
------------------------------------------------------------------------------
echo "
# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 20039  #端口号
#  bindIp: 127.0.0.1    #绑定 ip
replication:
  replSetName: shard3
sharding:
  clusterRole: shardsvr
security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 "  > /docker/mongo-cluster/shard3-server3/conf/mongo.conf

创立 keyfile

echo "tsUtJb3TeueNR8Mehr7ZLmZx82qfuCQ7LfLjUvQA7hNfWSomyNDISXDiSTJQEVym
OhXXzwB+0iv+czxi4qe9tAP8fMDuXpieZreysg4gxZ1VoFC1q39IrUDAEpCikSKS
abGl8RTEOM/GzVM8BATjaGHuBIi2osBAPg2Hzi+/u9ORbb4I4jzvgStcPcozRgOZ
5kPvXBybanV8MhLA6MfG1rcUiTkGoKb65YuWIfPuuF7PTWZe4VcF+iU6jgw73juZ
pbcZR5oTKvOWz89KCRTmQqHRexmJyn+NJcIGHFS/sZSJXE8LFPBZ+XLGYrtmDqo0
9tA1x8R+u32OJ7iOAU1mFkCHe2Uoph6aeVx/jZx1FgFjW0afT4ou2w7QHsdF0WRn
nskJ1FCA8NKzhYYgv/YrpyAChhTgd//gbWr028qz1W1POpBkj4muKUk7OTHRV6bs
qr2C73bqcZ1n2s60k6WbRUd6LP6POHR93wvi5EaXyorSMBIGiSD1Kyr/iqO7gD4C
GN8iA3MqF+fW5nKn1yBNEfPGoFk+p0EaxIAhfLEpzSRb3Wt5XLOWP7CBGuTo7KST
Y5HAcblqN7TByQhLdH5MZJ4FhfTZ0yNKTOVQdZUYRb5GGgS0GZfUk4bndLTkHrJd
tcR4WreHpz7ccncE5Vt8TGglrEx0noFVBqLqTdrqFUFpvWoukw/eViacLlBHKOxB
QVgfo4491znNMmthqGimVI7TFV706AvVJGqoIyuiFZRE5qx5MsOlIXiFwA3ue1Lo
kiFq5c6ImvS0R9LGu1Xcr0REYN53/bBVgGzJovEn7IIrHChYow7TkTLf/LsnjL3m
rmkDRgzA0C5i6fXgKkJdBhvvA521Yf75YP9n+819NUTZbtGIxRnP07pMS9RP4TjS
ZSd9an5yc7IpnL0gE4Pmnvf8LM86WTt9hZWKrE2LeQPEFgFl/Eq5NH60Zd4utxfi
qM2FH7aNsEukoAvA2v3All1wsM2kn4fMa89Hwui9h4xMy5tU"  > /docker/mongo-cluster/shard3-server1/conf/mongo.key

#解决权限为 400

chmod 400 /docker/mongo-cluster/shard3-server1/conf/mongo.key

#复制
cp /docker/mongo-cluster/shard3-server1/conf/mongo.key /docker/mongo-cluster/shard3-server2/conf/mongo.key

cp /docker/mongo-cluster/shard3-server1/conf/mongo.key /docker/mongo-cluster/shard3-server3/conf/mongo.key

运行 shard3 分片组

docker run --name shard3-server1 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/shard3-server1/conf:/data/configdb \
-v /docker/mongo-cluster/shard3-server1/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf

docker run --name shard3-server2 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/shard3-server2/conf:/data/configdb \
-v /docker/mongo-cluster/shard3-server2/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf

docker run --name shard3-server3 -d \
--net=host \
--privileged=true \
-v /docker/mongo-cluster/shard3-server3/conf:/data/configdb \
-v /docker/mongo-cluster/shard3-server3/data:/data/db \
docker.io/mongo:latest mongod -f /data/configdb/mongo.conf

初始化 shard3 分片组

并且制订第三个正本集为仲裁节点

 docker exec  -it shard3-server1 bash
 mongo -port 20037
 
#进行正本集配置
 rs.initiate(
{
_id : "shard3",
members: [{ _id : 0, host : "182.61.2.16:20037"},
{_id : 1, host : "182.61.2.16:20038"},
{_id : 2, host : "182.61.2.16:20039",arbiterOnly:true}
]
}
);

返回 ok 后创立用户

use admin
# 创立用户
db.createUser({user:"root",pwd:"root",roles:[{role:'root',db:'admin'}]})

而后退出,分片组 3 搭建实现

搭建 Mongos

搭建 Mongos1

创立配置文件

mkdir -p /docker/mongo-cluster/mongos1/{data,conf}

填入配置文件, 这里咱们删除了认证的信息,因为 mongos 是不能设置认证的,他也是用的后面应用的明码即可,如 configserver 的明码

echo "net:
  port: 20021  #端口号
sharding:
  configDB: configsvr/114.67.80.169:20011,182.61.2.16:20012,106.12.113.62:20013
security:
  keyFile: /data/configdb/mongo.key #keyFile 门路
"  > /docker/mongo-cluster/mongos1/conf/mongo.conf

创立 keyfile

echo "tsUtJb3TeueNR8Mehr7ZLmZx82qfuCQ7LfLjUvQA7hNfWSomyNDISXDiSTJQEVym
OhXXzwB+0iv+czxi4qe9tAP8fMDuXpieZreysg4gxZ1VoFC1q39IrUDAEpCikSKS
abGl8RTEOM/GzVM8BATjaGHuBIi2osBAPg2Hzi+/u9ORbb4I4jzvgStcPcozRgOZ
5kPvXBybanV8MhLA6MfG1rcUiTkGoKb65YuWIfPuuF7PTWZe4VcF+iU6jgw73juZ
pbcZR5oTKvOWz89KCRTmQqHRexmJyn+NJcIGHFS/sZSJXE8LFPBZ+XLGYrtmDqo0
9tA1x8R+u32OJ7iOAU1mFkCHe2Uoph6aeVx/jZx1FgFjW0afT4ou2w7QHsdF0WRn
nskJ1FCA8NKzhYYgv/YrpyAChhTgd//gbWr028qz1W1POpBkj4muKUk7OTHRV6bs
qr2C73bqcZ1n2s60k6WbRUd6LP6POHR93wvi5EaXyorSMBIGiSD1Kyr/iqO7gD4C
GN8iA3MqF+fW5nKn1yBNEfPGoFk+p0EaxIAhfLEpzSRb3Wt5XLOWP7CBGuTo7KST
Y5HAcblqN7TByQhLdH5MZJ4FhfTZ0yNKTOVQdZUYRb5GGgS0GZfUk4bndLTkHrJd
tcR4WreHpz7ccncE5Vt8TGglrEx0noFVBqLqTdrqFUFpvWoukw/eViacLlBHKOxB
QVgfo4491znNMmthqGimVI7TFV706AvVJGqoIyuiFZRE5qx5MsOlIXiFwA3ue1Lo
kiFq5c6ImvS0R9LGu1Xcr0REYN53/bBVgGzJovEn7IIrHChYow7TkTLf/LsnjL3m
rmkDRgzA0C5i6fXgKkJdBhvvA521Yf75YP9n+819NUTZbtGIxRnP07pMS9RP4TjS
ZSd9an5yc7IpnL0gE4Pmnvf8LM86WTt9hZWKrE2LeQPEFgFl/Eq5NH60Zd4utxfi
qM2FH7aNsEukoAvA2v3All1wsM2kn4fMa89Hwui9h4xMy5tU"  > /docker/mongo-cluster/mongos1/conf/mongo.key

#解决权限为 400

chmod 400 /docker/mongo-cluster/mongos1/conf/mongo.key

运行 mongos1

docker run --name mongos1 -d \
--net=host \
--privileged=true \
--entrypoint "mongos" \
-v /docker/mongo-cluster/mongos1/conf:/data/configdb \
-v /docker/mongo-cluster/mongos1/data:/data/db \
docker.io/mongo:latest -f /data/configdb/mongo.conf --bind_ip_all

搭建 Mongos2

创立配置文件

mkdir -p /docker/mongo-cluster/mongos2/{data,conf}

填入配置文件, 这里咱们删除了认证的信息,因为 mongos 是不能设置认证的,他也是用的后面应用的明码即可,如 configserver 的明码

echo "net:
  port: 20022  #端口号
sharding:
  configDB: configsvr/114.67.80.169:20011,182.61.2.16:20012,106.12.113.62:20013
security:
  keyFile: /data/configdb/mongo.key #keyFile 门路
"  > /docker/mongo-cluster/mongos2/conf/mongo.conf

创立 keyfile

echo "tsUtJb3TeueNR8Mehr7ZLmZx82qfuCQ7LfLjUvQA7hNfWSomyNDISXDiSTJQEVym
OhXXzwB+0iv+czxi4qe9tAP8fMDuXpieZreysg4gxZ1VoFC1q39IrUDAEpCikSKS
abGl8RTEOM/GzVM8BATjaGHuBIi2osBAPg2Hzi+/u9ORbb4I4jzvgStcPcozRgOZ
5kPvXBybanV8MhLA6MfG1rcUiTkGoKb65YuWIfPuuF7PTWZe4VcF+iU6jgw73juZ
pbcZR5oTKvOWz89KCRTmQqHRexmJyn+NJcIGHFS/sZSJXE8LFPBZ+XLGYrtmDqo0
9tA1x8R+u32OJ7iOAU1mFkCHe2Uoph6aeVx/jZx1FgFjW0afT4ou2w7QHsdF0WRn
nskJ1FCA8NKzhYYgv/YrpyAChhTgd//gbWr028qz1W1POpBkj4muKUk7OTHRV6bs
qr2C73bqcZ1n2s60k6WbRUd6LP6POHR93wvi5EaXyorSMBIGiSD1Kyr/iqO7gD4C
GN8iA3MqF+fW5nKn1yBNEfPGoFk+p0EaxIAhfLEpzSRb3Wt5XLOWP7CBGuTo7KST
Y5HAcblqN7TByQhLdH5MZJ4FhfTZ0yNKTOVQdZUYRb5GGgS0GZfUk4bndLTkHrJd
tcR4WreHpz7ccncE5Vt8TGglrEx0noFVBqLqTdrqFUFpvWoukw/eViacLlBHKOxB
QVgfo4491znNMmthqGimVI7TFV706AvVJGqoIyuiFZRE5qx5MsOlIXiFwA3ue1Lo
kiFq5c6ImvS0R9LGu1Xcr0REYN53/bBVgGzJovEn7IIrHChYow7TkTLf/LsnjL3m
rmkDRgzA0C5i6fXgKkJdBhvvA521Yf75YP9n+819NUTZbtGIxRnP07pMS9RP4TjS
ZSd9an5yc7IpnL0gE4Pmnvf8LM86WTt9hZWKrE2LeQPEFgFl/Eq5NH60Zd4utxfi
qM2FH7aNsEukoAvA2v3All1wsM2kn4fMa89Hwui9h4xMy5tU"  > /docker/mongo-cluster/mongos2/conf/mongo.key

#解决权限为 400

chmod 400 /docker/mongo-cluster/mongos2/conf/mongo.key

运行 mongos2

docker run --name mongos2 -d \
--net=host \
--privileged=true \
--entrypoint "mongos" \
-v /docker/mongo-cluster/mongos2/conf:/data/configdb \
-v /docker/mongo-cluster/mongos2/data:/data/db \
docker.io/mongo:latest -f /data/configdb/mongo.conf  --bind_ip_all

搭建 Mongos3

创立配置文件

mkdir -p /docker/mongo-cluster/mongos3/{data,conf}

填入配置文件, 这里咱们删除了认证的信息,因为 mongos 是不能设置认证的,他也是用的后面应用的明码即可,如 configserver 的明码

echo "net:
  port: 20023  #端口号
sharding:
  configDB: configsvr/114.67.80.169:20011,182.61.2.16:20012,106.12.113.62:20013
security:
  keyFile: /data/configdb/mongo.key #keyFile 门路
"  > /docker/mongo-cluster/mongos3/conf/mongo.conf

创立 keyfile

echo "tsUtJb3TeueNR8Mehr7ZLmZx82qfuCQ7LfLjUvQA7hNfWSomyNDISXDiSTJQEVym
OhXXzwB+0iv+czxi4qe9tAP8fMDuXpieZreysg4gxZ1VoFC1q39IrUDAEpCikSKS
abGl8RTEOM/GzVM8BATjaGHuBIi2osBAPg2Hzi+/u9ORbb4I4jzvgStcPcozRgOZ
5kPvXBybanV8MhLA6MfG1rcUiTkGoKb65YuWIfPuuF7PTWZe4VcF+iU6jgw73juZ
pbcZR5oTKvOWz89KCRTmQqHRexmJyn+NJcIGHFS/sZSJXE8LFPBZ+XLGYrtmDqo0
9tA1x8R+u32OJ7iOAU1mFkCHe2Uoph6aeVx/jZx1FgFjW0afT4ou2w7QHsdF0WRn
nskJ1FCA8NKzhYYgv/YrpyAChhTgd//gbWr028qz1W1POpBkj4muKUk7OTHRV6bs
qr2C73bqcZ1n2s60k6WbRUd6LP6POHR93wvi5EaXyorSMBIGiSD1Kyr/iqO7gD4C
GN8iA3MqF+fW5nKn1yBNEfPGoFk+p0EaxIAhfLEpzSRb3Wt5XLOWP7CBGuTo7KST
Y5HAcblqN7TByQhLdH5MZJ4FhfTZ0yNKTOVQdZUYRb5GGgS0GZfUk4bndLTkHrJd
tcR4WreHpz7ccncE5Vt8TGglrEx0noFVBqLqTdrqFUFpvWoukw/eViacLlBHKOxB
QVgfo4491znNMmthqGimVI7TFV706AvVJGqoIyuiFZRE5qx5MsOlIXiFwA3ue1Lo
kiFq5c6ImvS0R9LGu1Xcr0REYN53/bBVgGzJovEn7IIrHChYow7TkTLf/LsnjL3m
rmkDRgzA0C5i6fXgKkJdBhvvA521Yf75YP9n+819NUTZbtGIxRnP07pMS9RP4TjS
ZSd9an5yc7IpnL0gE4Pmnvf8LM86WTt9hZWKrE2LeQPEFgFl/Eq5NH60Zd4utxfi
qM2FH7aNsEukoAvA2v3All1wsM2kn4fMa89Hwui9h4xMy5tU"  > /docker/mongo-cluster/mongos3/conf/mongo.key

#解决权限为 400

chmod 400 /docker/mongo-cluster/mongos3/conf/mongo.key

运行 mongos3

docker run --name mongos3 -d \
--net=host \
--privileged=true \
--entrypoint "mongos" \
-v /docker/mongo-cluster/mongos3/conf:/data/configdb \
-v /docker/mongo-cluster/mongos3/data:/data/db \
docker.io/mongo:latest -f /data/configdb/mongo.conf --bind_ip_all

配置所有 mongos

进入第一台 mongos

docker exec -it mongos1 bash
mongo -port 20021

先登录(应用后面设置的 root 用户明码)

use admin;
db.auth("root","root");

进行配置分片信息

sh.addShard("shard1/114.67.80.169:20031,114.67.80.169:20032,114.67.80.169:20033")
sh.addShard("shard2/114.67.80.169:20034,114.67.80.169:20035,114.67.80.169:20036")
sh.addShard("shard3/182.61.2.16:20037,182.61.2.16:20038,182.61.2.16:20039")

全副返回 ok 则胜利

去其余两台 mongos 执行

mongos2

docker exec -it mongos2 bash
mongo -port 20022

use admin;
db.auth("root","root");

sh.addShard("shard1/114.67.80.169:20031,114.67.80.169:20032,114.67.80.169:20033")
sh.addShard("shard2/114.67.80.169:20034,114.67.80.169:20035,114.67.80.169:20036")
sh.addShard("shard3/182.61.2.16:20037,182.61.2.16:20038,182.61.2.16:20039")

mongos3

docker exec -it mongos3 bash
mongo -port 20023

use admin;
db.auth("root","root");

sh.addShard("shard1/114.67.80.169:20031,114.67.80.169:20032,114.67.80.169:20033")
sh.addShard("shard2/114.67.80.169:20034,114.67.80.169:20035,114.67.80.169:20036")
sh.addShard("shard3/182.61.2.16:20037,182.61.2.16:20038,182.61.2.16:20039")

功能测试

数据库分片

sh.enableSharding("test")

对 test 库的 test 汇合的_id 进行哈希分片
sh.shardCollection("test.test", {"_id": "hashed"})

创立用户

use admin;
db.auth("root","root");
use test;
db.createUser({user:"kang",pwd:"kang",roles:[{role:'dbOwner',db:'test'}]})

插入数据

use test
for (i = 1; i <= 300; i=i+1){db.test.insert({'name': "bigkang"})}

配置文件

ConfigServer 配置

openssl rand -base64 756 > mongo.key

# 日志文件
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#  网络设置
net:
  port: 27018  #端口号
#  bindIp: 127.0.0.1    #绑定 ip

security:
  authorization: enabled #是否开启认证
  keyFile: /data/configdb/mongo.key #keyFile 门路 

环境清理

清空 server1 两个分片数据

docker stop mongo-server1-shard{1,2}
docker rm mongo-server1-shard{1,2}

rm -rf /docker/mongo-cluster/mongo-server1-shard{1,2}

清空 server2 两个分片数据

docker stop mongo-server2-shard{1,2}
docker rm mongo-server2-shard{1,2}

rm -rf /docker/mongo-cluster/mongo-server2-shard{1,2}
docker ps -a| grep mongo | grep -v grep| awk '{print"docker stop "$1}'|sh
docker ps -a| grep mongo | grep -v grep| awk '{print"docker rm "$1}'|sh
退出移动版