Preparation environment:

Operating system: Debian

Debian download

Cdimage.debian.org/mirror/cdim…

Select the first download

Three Debian VM systems have been installed and their IP addresses have been set to

192.168.43.11, 192.168.43.12, 192.168.43.13.

The network configuration is set to bridge mode, and the IP address must be in the same network segment. My mobile phone’s hot network segment is 192.168.43.xxx

Obtain the mongodb installation package: mongodb-linux-x86_64-debian92-4.2.1. TGZ

Link: pan.baidu.com/s/1HaWJa-EQ… Password: diil

How to install Debian

Jingyan.baidu.com/article/6d7…

The first step

Add software sources for Debian:

nano /etc/apt/sources.list
Copy the code

Add the USTC source

# source at hkustdeb http://mirrors.ustc.edu.cn/debian/ stretch main non-free contrib deb http://mirrors.ustc.edu.cn/debian/ stretch-updates main non-free contrib deb http://mirrors.ustc.edu.cn/debian/ stretch-backports main non-free contrib deb-src http://mirrors.ustc.edu.cn/debian/ stretch main non-free contrib deb-src http://mirrors.ustc.edu.cn/debian/ stretch-updates main non-free contrib deb-src http://mirrors.ustc.edu.cn/debian/ stretch-backports main non-free contrib  deb http://mirrors.ustc.edu.cn/debian-security/ stretch/updates main non-free contrib deb-src http://mirrors.ustc.edu.cn/debian-security/ stretch/updates main non-free contribCopy the code

CTRL + X, press Y to save

The second step

In this step, all three machines need to do the following operations

Note that each machine has shareD1,shared2, shareD3 directories and configuration files, this is only 3 shards, does not matter how many hosts! We’re also going to have 5 shards and 6 shards is fine.

Update the source:

apt-get update
Copy the code

Install libcurl4:

apt-get install libcurl4-openssl-dev
Copy the code

Run the following command to upload mongodb-linux-x86_64-debian92-4.2.1. TGZ to the /usr/local directory:

cd /usr/localTar -zxvf mongodb-linux-x86_64-debian92-4.2.1. TGZ mv mongodb-linux-x86_64-debian92-4.2.1cd mongodb

mkdir -p /usr/local/mongodb/db
mkdir -p /usr/local/mongodb/log
chmod 777 db log
mkdir -p /usr/local/mongodb/shared1/db
mkdir -p /usr/local/mongodb/shared1/log
mkdir -p /usr/local/mongodb/shared2/db
mkdir -p /usr/local/mongodb/shared2/log
mkdir -p /usr/local/mongodb/shared3/db
mkdir -p /usr/local/mongodb/shared3/log
mkdir -p /usr/local/mongodb/mongos
Copy the code

Create the mongodb configuration file, and the sharded 3 configuration files, and the routing service Mongos configuration file:

cd /usr/local/mongodb
touch mongodb.conf shared1.conf shared2.conf shared3.conf mongos.conf
Copy the code

Next edit the configuration file:

cd /usr/local/mongodb
Copy the code
nano mongodb.conf
Copy the code

Add the following:

pidfilepath=/usr/local/mongodb/log/configsvr.pid
dbpath=/usr/local/mongodb/db
logpath=/usr/local/mongodb/log/mongodb.log
logappend=trueBind_ip = 0.0.0.0 port = = 27017 forktrue
auth=false
journal=true
storageEngine=wiredTiger
configsvr=true
replSet=configs
maxConns=10000

Copy the code

CTRL + X, press Y to save Enter

Edit shared1. Conf shared2. Conf shared3. Conf configuration file

nano shared1.conf
Copy the code

Add the following:

pidfilepath=/usr/local/mongodb/shared1/log/shared1.pid
dbpath=/usr/local/mongodb/shared1/db
logpath=/usr/local/mongodb/shared1/log/shared1.log
logappend = trueBind_ip = 0.0.0.0 port = 30001 fork =true
replSet=shared1
shardsvr = true
maxConns=10000
storageEngine=wiredTiger
Copy the code
nano shared2.conf
Copy the code

Add the following:

pidfilepath=/usr/local/mongodb/shared2/log/shared2.pid
dbpath=/usr/local/mongodb/shared2/db
logpath=/usr/local/mongodb/shared2/log/shared2.log
logappend = trueBind_ip = 0.0.0.0 port = 30002 fork =true
replSet=shared2
shardsvr = true
maxConns=10000
storageEngine=wiredTiger
Copy the code
nano shared3.conf
Copy the code

Add the following:

pidfilepath=/usr/local/mongodb/shared3/log/shared3.pid
dbpath=/usr/local/mongodb/shared3/db
logpath=/usr/local/mongodb/shared3/log/shared3.log
logappend = trueBind_ip = 0.0.0.0 port = 30003 fork =true
replSet=shared3
shardsvr = true
maxConns=10000
storageEngine=wiredTiger

Copy the code

Configuring the routing service configuration file:

nano mongos.conf
Copy the code

Add the following:

pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid
logpath = /usr/local/mongodb/mongos/log/mongos.log
logappend = trueBind_ip = 0.0.0.0 port = 40000 fork =trueConfigdb = configs / 192.168.43.11:27017192168 43.12:27017192168 43.13:27017 maxConns = 10000Copy the code

The above configuration of 3 machines should be configured

After the configuration is complete, run the following command on all three hosts to start the mongodb service:

cd /usr/local/mongodb/bin
./mongod -f /usr/local/mongodb/mongodb.conf
Copy the code

The third step

On one of the hosts, select the first host

cd /usr/local/mongodb/bin
Copy the code

Connect the mongo

/mongo --host 192.168.43.11 --port 27017Copy the code

Switching databases

use admin
Copy the code

Initialize a replication set

rs.initiate({_id:"configs",members:[{_id:0,host:"192.168.43.11:27017"},{_id:1,host:"192.168.43.12:27017"}, {_id:2,host:"192.168.43.13:27017"}]})
Copy the code

Configs with the _id:”configs” is the name of the replication set in the mongodb.conf configuration file, which consists of the configuration services of the three servers

Check the status

rs.status()
Copy the code

Wait for dozens of seconds, run the above command to check the status, the configuration service of the three machines has formed a replication set, one of which is PRIMARY, and the other two are SECONDARY

The fourth step

In this step, all three machines need to do the following operations

./mongod -f /usr/local/mongodb/shared1.conf
./mongod -f /usr/local/mongodb/shared2.conf
./mongod -f /usr/local/mongodb/shared3.conf
Copy the code

Step 5

Operating on one of the hosts:

Connect the mongo:

/mongo --host 192.168.43.11 --port 30001Copy the code

Switching databases

use admin
Copy the code

Initialize a replication set

rs.initiate({_id:"shared1",members:[{_id:0,host:"192.168.43.11:30001"},{_id:1,host:"192.168.43.12:30001"},{_id:2,host:"192.168.43.13:30001"}]})
Copy the code

The following is the same, but the ports are different

/mongo --host 192.168.43.11 --port 30002 use admin rs. Initiate ({_id:"shared2",members:[{_id:0,host:"192.168.43.11:30002"},{_id:1,host:"192.168.43.12:30002"},{_id:2,host:"192.168.43.13:30002"}]})
Copy the code
/mongo --host 192.168.43.11 --port 30003 Use admin rs. Initiate ({_id:"shared3",members:[{_id:0,host:"192.168.43.11:30003"},{_id:1,host:"192.168.43.12:30003"},{_id:2,host:"192.168.43.13:30003"}]})
Copy the code

Step 6

This step is performed on three machines

Routing Service Deployment

./mongos -f /usr/local/mongodb/mongos.conf
Copy the code

Step 7

Operating on one of the hosts:

Enable sharding:

Connect the mongo:

/mongo --host 192.168.43.11 --port 40000Copy the code

Switching databases

use admin
Copy the code

Adding sharding is done on a single machine

sh.addShard("Shared1/192.168.43.11:30001192168 43.12:30001192168 43.13:30001")
sh.addShard("Shared2/192.168.43.11:30002192168 43.12:30002192168 43.13:30002")
sh.addShard("Shared3/192.168.43.11:30003192168 43.12:30003192168 43.13:30003")
Copy the code

View cluster status:

mongos> sh.status()

Conf, shared{1/2/3}. Conf, replSet= XXX in the configuration file must be commented out. It depends on which database is connected to perform operations. Generally, write operations to the database are connected to the database of the routing service

Create a database and table at the routing node 192.168.43.13

It is found that all hosts have synchronized data in the shard database shared2.

Whether it is assigned to shareD1 or shareD2 or shareD3 is indeterminate and random.

Test writing data: use test

for(i=1; i<=30000; i++){db.user.insert({“id”:i,”name”:”jack”+i})}

It can also be executed by 3 machines at the same time, one machine inserts 10000 pieces of data, which can save a lot of time

At this point, the distributed MongoDB setup is complete