Mongodb Cluster Construction

There are three ways to build a mongodb cluster:

  1. Master-slave mode, or master-slave replication mode.
  2. Replica Set mode.
  3. Sharding mode.

The first method makes little sense and is not officially recommended. The other two are replica sets and sharding. Today we will introduce the replica set method to build mongodb high availability cluster

Introduction and Overview

First let’s take a look at the concept of a Mongo cluster. A Mongo cluster has three main components

ConfigServer: ConfigServer provides configuration information about the entire cluster and configures storage. If a high-availability ConfigServer is required, three nodes are required.

Shard: Shard, storing real data, each Shard is responsible for storing data in the cluster, for example, a cluster has three shards, and then we define sharding rules as hashing, so the data of the whole cluster will be divided into one of the three shards, so sharding is particularly important. If all the cluster a shard of the collapsed the cluster will not be available, so we have to guarantee the high availability cluster, so we need a shard configuration three nodes, 2 copies set an arbitration node, the node is similar to Redis sentry mode, if it is found that the master node hang up then let the other a set of copy for data storage.

Mongos: Mongos can be understood as an entry point to the whole cluster, similar to Kafka’s Broker agent, or client, through which we connect to the cluster for queries.

Mongos is a route, and their information is stored in ConfigServer. We add it through Mongos, and then fragment the data into the sharded replica set according to the conditions

Mongo Sharding cluster (not highly available, not recommended)

Build ConfigServer

First, we set up 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}
Copy the code

Then configure the ports in the configuration file

Create the first configuration file
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 configuration file
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
Copy the code

And 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 
Copy the code

It then goes into container initialization

# Enter 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"}}]);Copy the code

If OK is 1, it succeeds

Creating a Shard cluster

Let’s create two shards for each server

Create a mount file

Create two shard directories for 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 shard directories for config-server-2
mkdir -p /docker/mongo-cluster/mongo-server2-shard1/{data,conf}
mkdir -p /docker/mongo-cluster/mongo-server2-shard2/{data,conf}
Copy the code

Creating a Configuration File

Create two shard config files for 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
Copy the code
Create two shard config files for 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
Copy the code

And then start the container

Start two shard containers for 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
Copy the code
Start two shard containers for 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
Copy the code

Enter the first shard

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

Go to the second shard

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

Install the Mongos

Create a 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
Copy the code

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:20011114.67. 80.169:20012 - bind_ip_allCopy the code

Mongo adds the shard group

Sh. AddShard (rs_shard_server1/114.67.80.169:20021114.67. 80.169: "20022") Sh. AddShard (rs_shard_server2/182.61.2.16:20023182.61. 2.16: "20024")Copy the code

Enable sharding for new data

Sh.shardcollection ("test.order", {"_id": "hashed"})Copy the code
Use test for (I = 1; i <= 1000; i=i+1){db.order.insert({'price': 1})}Copy the code

Mongo Sharding Cluster High Availability + Permissions (recommended)

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

Mongos: 3 units

Configserver: 3 units

Shard: 3 pieces

Each shard deploys two replica sets and one quorum node: three

So 3 + 3 + 3 times 3 = 15, and I’m going to show you three servers

114.67.80.169 4-core 16GB ConfigServer, mongos, and two sharding groups are deployed

182.61.2.16 2-core 4G deploy a ConfigServer, a Mongos, and a sharding group

106.12.113.62 One configServer and one Mongos are deployed for 1 core 2G, and the fragment group is not set up

The distribution is not balanced because of the server. Set up the configuration based on the 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

We will build ConfigServer first, because we know that to build it must be highly available and must have permissions. Here the communication between Mongo uses the secret key file, so we will build it first

Set up the config – server1

Create a directory for mounting files

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

Write configuration file

echo "# # # systemLog log files: destination: the file # logAppend: true # path: / var/log/mongo/data0802 log # network Settings net: port: Replication: replSetName: configSvr Sharding: clusterRole: ConfigSvr Security: Authorization: enabled keyFile: /data/configdb/mongo.key #keyFile path"  > /docker/mongo-cluster/config-server1/conf/mongo.conf
Copy the code

Then generate the keyFile

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

The file is as follows, we, we all use this key (please use the key generated by yourself)

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
Copy the code

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
Copy the code

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
Copy the code

Set up the config – server2

Create a directory for mounting files

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

Write configuration file

Write configuration file

echo "# # # systemLog log files: destination: the file # logAppend: true # path: / var/log/mongo/data0802 log # network Settings net: port: Replication: replSetName: configSvr Sharding: clusterRole: ConfigSvr Security: Authorization: enabled keyFile: /data/configdb/mongo.key #keyFile path"  > /docker/mongo-cluster/config-server2/conf/mongo.conf
Copy the code

The file is as follows, we, we all use this key (please use the key generated by yourself)

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
Copy the code

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
Copy the code

Set up the config – server3

Create a directory for mounting files

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

Write configuration file

echo "# # # systemLog log files: destination: the file # logAppend: true # path: / var/log/mongo/data0802 log # network Settings net: port: Replication: replSetName: configSvr Sharding: clusterRole: ConfigSvr Security: Authorization: enabled keyFile: /data/configdb/mongo.key #keyFile path"  > /docker/mongo-cluster/config-server3/conf/mongo.conf
Copy the code

The file is as follows, we, we all use this key (please use the key generated by yourself)

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
Copy the code

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
Copy the code

Initialize the config – server

Enter the first container

docker exec -it mongo-server1 bash
mongo -port 20011
Copy the code

The 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"})})Copy the code

Success if ok is returned

Then we create the user

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

Build a Shard group

Since Mongos is the client, we set up config and shard first and then Mongos.

Set up shard1 sharding group

Initialize a set of shards on the same server

Create a 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}
Copy the code

Configuration Configuration file

echo "# # # systemLog log files: destination: the file # logAppend: true # path: / var/log/mongo/data0802 log # network Settings net: port: Replication: replSetName: shard1 Sharding: clusterRole: shardSvr Security: Authorization: enabled keyFile: /data/configdb/mongo.key #keyFile path"  > /docker/mongo-cluster/shard1-server1/conf/mongo.conf
------------------------------------------------------------------------------
echo "# # # systemLog log files: destination: the file # logAppend: true # path: / var/log/mongo/data0802 log # network Settings net: port: 20032 # port # bindIp: 127.0.0.1 # bindIp replication: replSetName: shard1 sharding: clusterRole: shardsvr security: Authorization: enabled keyFile: /data/configdb/mongo.key #keyFile path"  > /docker/mongo-cluster/shard1-server2/conf/mongo.conf
------------------------------------------------------------------------------
echo "# # # systemLog log files: destination: the file # logAppend: true # path: / var/log/mongo/data0802 log # network Settings net: port: Replication: replSetName: shard1 Sharding: clusterRole: shardSvr Security: Authorization: enabled keyFile: /data/configdb/mongo.key #keyFile path"  > /docker/mongo-cluster/shard1-server3/conf/mongo.conf
Copy the code

Create a 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
Copy the code

Run shard1 sharding groups

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
Copy the code

Example Initialize the Shard1 fragment group

And the third replica set is designated as the arbitration node

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}]});Copy the code

Return OK to create the user

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

Then exit, and shard group 1 is set up

Set up shard2 sharding group

Initialize a set of shards on the same server

Create a 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}
Copy the code

Configuration Configuration file

echo "# # # systemLog log files: destination: the file # logAppend: true # path: / var/log/mongo/data0802 log # network Settings net: port: Replication: replSetName: shard2 Sharding: clusterRole: shardSvr Security: Authorization: enabled keyFile: /data/configdb/mongo.key #keyFile path"  > /docker/mongo-cluster/shard2-server1/conf/mongo.conf
------------------------------------------------------------------------------
echo "# # # systemLog log files: destination: the file # logAppend: true # path: / var/log/mongo/data0802 log # network Settings net: port: Replication: replSetName: shard2 Sharding: clusterRole: shardSvr Security: Authorization: enabled keyFile: /data/configdb/mongo.key #keyFile path"  > /docker/mongo-cluster/shard2-server2/conf/mongo.conf
------------------------------------------------------------------------------
echo "# # # systemLog log files: destination: the file # logAppend: true # path: / var/log/mongo/data0802 log # network Settings net: port: Replication: replSetName: shard2 Sharding: clusterRole: shardSvr Security: Authorization: enabled keyFile: /data/configdb/mongo.key #keyFile path"  > /docker/mongo-cluster/shard2-server3/conf/mongo.conf
Copy the code

Create a 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
Copy the code

Run shard2 sharding 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
Copy the code

Example Initialize the Shard2 fragment group

And the third replica set is designated as the arbitration node

Rs. initiate({_id: "shard2", members: [{_id:]) docker exec it shard2-server1 bash mongo-port 20034 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}]});Copy the code

Return OK to create the user

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

Then exit, and shard group 2 is set up

Build sharD3 fragment group

Initialize a set of shards on the same server

Create a 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}
Copy the code

Configuration Configuration file

echo "# # # systemLog log files: destination: the file # logAppend: true # path: / var/log/mongo/data0802 log # network Settings net: port: Replication: replSetName: shard3 Sharding: clusterRole: shardSvr Security: Authorization: enabled keyFile: /data/configdb/mongo.key #keyFile path"  > /docker/mongo-cluster/shard3-server1/conf/mongo.conf
------------------------------------------------------------------------------
echo "# # # systemLog log files: destination: the file # logAppend: true # path: / var/log/mongo/data0802 log # network Settings net: port: Replication: replSetName: shard3 Sharding: clusterRole: shardSvr Security: Authorization: enabled keyFile: /data/configdb/mongo.key #keyFile path"  > /docker/mongo-cluster/shard3-server2/conf/mongo.conf
------------------------------------------------------------------------------
echo "# # # systemLog log files: destination: the file # logAppend: true # path: / var/log/mongo/data0802 log # network Settings net: port: Replication: replSetName: shard3 Sharding: clusterRole: shardSvr Security: Authorization: enabled keyFile: /data/configdb/mongo.key #keyFile path"  > /docker/mongo-cluster/shard3-server3/conf/mongo.conf
Copy the code

Create a 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
Copy the code

Run shard3 sharding groups

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
Copy the code

Example Initialize the Shard3 fragment group

And the third replica set is designated as the arbitration node

Rs. initiate({_id: "shard3", members: [{_id:]) docker exec it shard3-server1 bash mongo-port 20037 182.61.2.16:0, host: "20037"}, {_id: 1, host: 182.61.2.16: "20038"}, {_id: 2, host: "182.61.2.16:20039",arbiterOnly:true}]});Copy the code

Return OK to create the user

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

Then exit, and shard group 3 is set up

Build Mongos

Build Mongos1

Creating a Configuration File

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

Fill in the configuration file, we delete the authentication information, because Mongos cannot set authentication, he also uses the previous password, such as configServer password

echo Net: port: 20021 # sharding: configDB: Configsvr / 114.67.80.169:20011182.61. 2.16:20012106.12. 113.62:20013 security: keyFile: /data/configdb/mongo.key  > /docker/mongo-cluster/mongos1/conf/mongo.conf
Copy the code

Create a 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
Copy the code

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
Copy the code

Build Mongos2

Creating a Configuration File

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

Fill in the configuration file, we delete the authentication information, because Mongos cannot set authentication, he also uses the previous password, such as configServer password

echo Net: port: 20022 # sharding: configDB: Configsvr / 114.67.80.169:20011182.61. 2.16:20012106.12. 113.62:20013 security: keyFile: /data/configdb/mongo.key  > /docker/mongo-cluster/mongos2/conf/mongo.conf
Copy the code

Create a 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
Copy the code

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
Copy the code

Build Mongos3

Creating a Configuration File

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

Fill in the configuration file, we delete the authentication information, because Mongos cannot set authentication, he also uses the previous password, such as configServer password

echo Net: port: 20023 sharding: configDB: Configsvr / 114.67.80.169:20011182.61. 2.16:20012106.12. 113.62:20013 security: keyFile: /data/configdb/mongo.key  > /docker/mongo-cluster/mongos3/conf/mongo.conf
Copy the code

Create a 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
Copy the code

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
Copy the code

Configure all mongos

Enter the first Mongos

docker exec -it mongos1 bash
mongo -port 20021
Copy the code

Log in first (using the root password you set earlier)

use admin;
db.auth("root","root");
Copy the code

Configure fragment information

Sh. AddShard (" shard1/114.67.80.169:20031114.67. 80.169:20032114.67. 80.169:20033 ") Sh. AddShard (" shard2/114.67.80.169:20034114.67. 80.169:20035114.67. 80.169:20036 ") Sh. AddShard (" shard3/182.61.2.16:20037182.61. 2.16:20038182.61. 2.16:20039 ")Copy the code

All returns OK 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:20031114.67. 80.169:20032114.67. 80.169:20033 ") Sh. AddShard (" shard2/114.67.80.169:20034114.67. 80.169:20035114.67. 80.169:20036 ") Sh. AddShard (" shard3/182.61.2.16:20037182.61. 2.16:20038182.61. 2.16:20039 ")Copy the code

mongos3

docker exec -it mongos3 bash mongo -port 20023 use admin; db.auth("root","root"); Sh. AddShard (" shard1/114.67.80.169:20031114.67. 80.169:20032114.67. 80.169:20033 ") Sh. AddShard (" shard2/114.67.80.169:20034114.67. 80.169:20035114.67. 80.169:20036 ") Sh. AddShard (" shard3/182.61.2.16:20037182.61. 2.16:20038182.61. 2.16:20039 ")Copy the code

A functional test

Database sharding

Sh.shardcollection ("test.test", {"_id": "hashed"})Copy the code

Create a user

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

Insert data

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

The 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 # Port number
# bindIp: 127.0.0.1

security:
  authorization: Enabled # Whether to enable authentication
  keyFile: / data/configdb/mongo. Key # keyFile path
Copy the code

Clean up the environment

Clear the fragmented data of 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}Copy the code

Clear server2 fragmented data

Docker stop mongo-server2-shard{1,2} docker rm mongo-server2-shard{1,2} rm -rf / docker/mongo - cluster/mongo - server2 - shard {1, 2}Copy the code
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
Copy the code