Hand in hand super detailed docker deployment mongodb cluster

Time:2021-2-7

Mongodb cluster construction

There are three ways to build mongodb cluster

  1. Master – slave mode, or master – slave copy mode.
  2. Replica set mode.
  3. Sharding mode.

Among them, the first way is basically meaningless, and the government does not recommend this way. The other two are replica set and fragmentation. Today, we will introduce how to build mongodb high availability cluster by replica set

Introduction and overview

First of all, let’s understand the concept of Mongo cluster. Mongo cluster has three main components

Configserver: plays the role of storing the configuration information of the whole cluster in the cluster, and is responsible for configuring the storage. If a highly available configserver is needed, three nodes are required.

​ Shard: shard is used to store real data. Each shard is responsible for storing data in the cluster. For example, a cluster has three Shards. Then we define the shard rule as hash, and the data of the whole cluster will be (split) into one of the three Shards. Shard is particularly important. If one shard in the cluster crashes, the cluster will crash It is not available, so to ensure the high availability of the cluster, we need to configure 3 nodes, 2 replica sets and an arbitration node in a slice. The arbitration node is similar to the sentinel mode of redis. If the primary node is found to be down, then another replica set is used for data storage.

Mongos: mongos can be understood as the entrance of the whole cluster, which is similar to Kafka’s broker agent, that is, the client. We use the client to connect to the cluster for query.

The following is the official cluster architecture diagram of mongodb. We can see that mongos is a route, and their information is stored in configserver. We add it through mongos, and then fragment the data into the fragmented replica set according to the conditions.

Hand in hand super detailed docker deployment mongodb cluster

Mongo partitioned cluster (not highly available, not recommended)

Build configserver

First, we build two config servers

Create two config server configuration files

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

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

Then configure the port in the configuration file

#Create the first profile
#Write configuration information, port number

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

#Create a second profile
#Write configuration information, port number

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

Then start the container

#Start 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

Then enter the container initialization

#Enter the container
docker exec -it mongo-server1 bash

mongo -port 20011

#Initialization
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" }
]
}
);

If ok is 1, it means success

Creating fragmented clusters

Next, we create two partitions for each server

Create mount file

#Create two partition directories of config-server-1
mkdir -p /docker/mongo-cluster/mongo-server1-shard1/{data,conf}
mkdir -p /docker/mongo-cluster/mongo-server1-shard2/{data,conf}

#Create two partition directories of config-server-2
mkdir -p /docker/mongo-cluster/mongo-server2-shard1/{data,conf}
mkdir -p /docker/mongo-cluster/mongo-server2-shard2/{data,conf}

create profile

#Create two partition configuration files of 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
#Create two partition configuration files of 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

Then start the container

#Start the two partition containers of 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
#Start the two partition containers of 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

Enter the first slice

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

Enter the second slice

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

Install mongos

Create mount file

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

Then start 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 add fragment group

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")

New data enable fragmentation

sh.enableSharding("test")

yes test.order Of_ Hash partition with ID
sh.shardCollection("test.order", {"_id": "hashed" })
View sliced data after inserting data
use test
for (i = 1; i <= 1000; i=i+1){db.order.insert({'price': 1})}

Mongo cluster high availability + permission (recommended)

So let’s summarize how many mongos we need to build a high availability cluster

Mongos: 3

Configserver: 3

Shard: three

Each shard has two replica sets and one arbitration node: three

So 3 + 3 + 3 * 3 = 15, I’ll demonstrate using 3 servers here

114.67.80.169 4-core 16g with one configserver, one mongos and two partition groups

182.61.2.16 2-core 4G is deployed with one configserver, one mongos and one partition group

106.12.113.62 1-core 2G deploys one configserver and one mongos without building a partition group

Due to the server reason, it is not evenly distributed. Please build it according to your actual situation

role ip port
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

Build configserver

Let’s build configserver first, because we know that it must be highly available and must have permissions. Here, Mongo uses secret key file for communication, so we will generate it first

Build config-server1

Create mount file directory

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

Write configuration file

echo "
#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 20011
#Bindip: 127.0.0.1 # binding IP
replication:
  replSetName: configsvr
sharding:
  clusterRole: configsvr
security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path "> / docker / Mongo cluster / config-server1 / conf/ mongo.conf

Then generate keyfile

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

The file is as follows. We will use this for all our keys later (please use the key generated by ourselves)

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

Write key file

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

#The processing permission is 400

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

Then start the config-server1 container

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

Build config-server2

Create mount file directory

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

Write configuration file

Write configuration file

echo "
#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 20012
#Bindip: 127.0.0.1 # binding IP
replication:
  replSetName: configsvr
sharding:
  clusterRole: configsvr
security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path "> / docker / Mongo cluster / config-server2 / conf/ mongo.conf

The file is as follows. We will use this for all our keys later (please use the key generated by ourselves)

Write key file

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

#The processing permission is 400

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

Then start the config-server2 container

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

Build config-server3

Create mount file directory

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

Write configuration file

echo "
#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 20013
#Bindip: 127.0.0.1 # binding IP
replication:
  replSetName: configsvr
sharding:
  clusterRole: configsvr
security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path "> / docker / Mongo cluster / config-server3 / conf/ mongo.conf

The file is as follows. We will use this for all our keys later (please use the key generated by ourselves)

Write key file

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

#The processing permission is 400

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

Then start the config-server3 container

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

Initialize config server

Enter the first container

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

input

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" }
    ]
  }
)

If it returns OK, it is successful

Then we create users

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

Build shard group

Since mongos is a client, we need to build config and shard first, and then mongos.

Build shard1 partition group

Initializing a set of slices on the same server

Create mount file

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}

Configuration profile

echo "
#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 20031
#Bindip: 127.0.0.1 # binding IP
replication:
  replSetName: shard1
sharding:
  clusterRole: shardsvr
security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path "> / docker / Mongo cluster / shard1-server1 / conf/ mongo.conf
------------------------------------------------------------------------------
echo "
#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 20032
#Bindip: 127.0.0.1 # binding IP
replication:
  replSetName: shard1
sharding:
  clusterRole: shardsvr
security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path "> / docker / Mongo cluster / shard1-server2 / conf/ mongo.conf
------------------------------------------------------------------------------
echo "
#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 20033
#Bindip: 127.0.0.1 # binding IP
replication:
  replSetName: shard1
sharding:
  clusterRole: shardsvr
security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path "> / docker / Mongo cluster / shard1-server3 / conf/ mongo.conf

Create 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

#The processing permission is 400

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

#Copy
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

Run shard1 partition group

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

Initialize shard1 partition group

And make the third replica set as the arbitration node

docker exec  -it shard1-server1 bash
 mongo -port 20031
 
#Configure replica set
 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 }
]
}
);

Create a user after returning OK

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

Then exit and the partition group 1 is completed

Set up shard2 partition group

Initializing a set of slices on the same server

Create mount file

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}

Configuration profile

echo "
#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 20034
#Bindip: 127.0.0.1 # binding IP
replication:
  replSetName: shard2
sharding:
  clusterRole: shardsvr
security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path "> / docker / Mongo cluster / shard2-server1 / conf/ mongo.conf
------------------------------------------------------------------------------
echo "
#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 20035
#Bindip: 127.0.0.1 # binding IP
replication:
  replSetName: shard2
sharding:
  clusterRole: shardsvr
security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path "> / docker / Mongo cluster / shard2-server2 / conf/ mongo.conf
------------------------------------------------------------------------------
echo "
#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 20036
#Bindip: 127.0.0.1 # binding IP
replication:
  replSetName: shard2
sharding:
  clusterRole: shardsvr
security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path "> / docker / Mongo cluster / shard2-server3 / conf/ mongo.conf

Create 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

#The processing permission is 400

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

#Copy
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

Run shard2 partition group

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

Initialize shard2 partition group

And make the third replica set as the arbitration node

docker exec  -it shard2-server1 bash
 mongo -port 20034
 
#Configure replica set
 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 }
]
}
);

Create a user after returning OK

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

Then exit, and the construction of partition group 2 is completed

Set up shard3 fragment group

Initializing a set of slices on the same server

Create mount file

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}

Configuration profile

echo "
#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 20037
#Bindip: 127.0.0.1 # binding IP
replication:
  replSetName: shard3
sharding:
  clusterRole: shardsvr
security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path "> / docker / Mongo cluster / shard3-server1 / conf/ mongo.conf
------------------------------------------------------------------------------
echo "
#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 20038
#Bindip: 127.0.0.1 # binding IP
replication:
  replSetName: shard3
sharding:
  clusterRole: shardsvr
security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path "> / docker / Mongo cluster / shard3-server2 / conf/ mongo.conf
------------------------------------------------------------------------------
echo "
#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 20039
#Bindip: 127.0.0.1 # binding IP
replication:
  replSetName: shard3
sharding:
  clusterRole: shardsvr
security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path "> / docker / Mongo cluster / shard3-server3 / conf/ mongo.conf

Create 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

#The processing permission is 400

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

#Copy
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

Run shard3 partition group

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

Initialize shard3 partition group

And make the third replica set as the arbitration node

docker exec  -it shard3-server1 bash
 mongo -port 20037
 
#Configure replica set
 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 }
]
}
);

Create a user after returning OK

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

Then exit, and the partition group 3 is completed

Building mongos

Building mongos1

create profile

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

Fill in the configuration file, here we delete the authentication information, because mongos can’t set the authentication, he also uses the password used before, such as the password of 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 path
"  > /docker/mongo-cluster/mongos1/conf/mongo.conf

Create 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

#The processing permission is 400

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

Run 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

Building mongos2

create profile

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

Fill in the configuration file, here we delete the authentication information, because mongos can’t set the authentication, he also uses the password used before, such as the password of 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 path
"  > /docker/mongo-cluster/mongos2/conf/mongo.conf

Create 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

#The processing permission is 400

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

Run 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

Building mongos3

create profile

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

Fill in the configuration file, here we delete the authentication information, because mongos can’t set the authentication, he also uses the password used before, such as the password of 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 path
"  > /docker/mongo-cluster/mongos3/conf/mongo.conf

Create 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

#The processing permission is 400

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

Run 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

Configure all mongos

Enter the first mongos

docker exec -it mongos1 bash
mongo -port 20021

Log in first (use the root password set previously)

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

Configure the slice information

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")

If all return to OK, it will succeed

Go to the other two 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")

functional testing

Database fragmentation

sh.enableSharding("test")

On the test set of test library_ Hash partition with ID
sh.shardCollection("test.test", {"_id": "hashed" })

Create user

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

insert data

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

configuration file

Configserver configuration

openssl rand -base64 756 > mongo.key

#Log file
#systemLog:
#  destination: file
#  logAppend: true
#  path: /var/log/mongodb/data0802.log

#Network settings
net:
  Port: 27018
#Bindip: 127.0.0.1 # binding IP

security:
  Authorization: enabled # enable authentication
  keyFile: /data/configdb/ mongo.key  #Keyfile path

Environmental cleaning

Clear two pieces of data in 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}

Clear two pieces of data in 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

Recommended Today

Git:fatal: invalid refspec HEAD:[email protected] (push);

Error information Git Pull Failed: invalid refspec ‘HEAD:[email protected]:hoonick/giddy.git (push)’ fatal: invalid refspec ‘HEAD:[email protected]:hoonick/giddy.git (push)’ Problem handling: This error means that the reference specification is invalid, the format is incorrect, and does not conform to the specification You can refer to the GIT official website Find the. Git folder in the warehouse (this is a hidden […]