MongoDB replica sets + sharding high-availability cluster creation (including authentication)

Source: Internet
Author: User

The cluster is composed of three servers (assuming the IP address is servera, serverb, and serverc) and uses the MongoDB replica set + sharding (replica sets + sharding) implements high reliability, high availability, and load balancing for Data Reading and Writing in clusters. The three machines are divided into two replica sets, which form two shards (shard1 and shard2) of a cluster ). The details are as follows:

1, servera s1-1/serverb s1-2/serverc s1-3 constitute a 3 node replica set S1 2, servera s2-1/serverb s2-2/serverc s2-3 constitute a 3 node Replication set S2 3. Set S1 and S2 to form a sharded cluster with two slices. 4. To ensure high availability of configuration information, the cluster uses three configuration nodes to store cluster configuration information: C3 5 of C2/serverc of C1/serverb of servera, which provides high availability for external services, the cluster uses three routing nodes to provide external services: mongos 3 of mongos 2/serverc of mongos 1/serverb of servera, and a VIP can also be provided to external users through keepalive. mongoDB cluster Logical Structure: MongoDB Service Process Planning: four service processes (2 + 1 + 1) run on each machine, and port and directory planning:

The detailed configuration steps are as follows:

1. Configure shard1 to use the replica set S1:

On servera: mongod -- replset S1 -- Port 27020 -- dbpath =/data/Mongo/s1_1/DB -- logpath =/data/Mongo/s1_1/log/Mongo. log -- logappend -- fork

On serverb: mongod -- replset S1 -- Port 27020 -- dbpath =/data/Mongo/s1_2/DB -- logpath =/data/Mongo/s1_2/log/Mongo. log -- logappend -- fork

On serverc: mongod -- replset S1 -- Port 27020 -- dbpath =/data/Mongo/s1_3/DB -- logpath =/data/Mongo/s1_3/log/Mongo. log -- logappend -- fork

Connects any of the three nodes to initialize the replica set S1.

> Use Admin

> Config = {_ ID: 's1', members: [{_ ID: 0, host: 'servera: 100', Priority: 1}, {_ ID: 1, HOST: 'serverb: 100'}, {_ ID: 2, host: 'serverc: 100'}]}

> Rs. Initiate (config)

> Rs. Status ()

In addition, execute the following command on all nodes in the replication set S1 to ensure that all nodes share the read pressure.

> DB. getmongo (). setslaveok ();

2. Configure shard2 to use the replica set S2:

On servera: mongod -- replset S2 -- Port 27021 -- dbpath =/data/Mongo/s2_1/DB -- logpath =/data/Mongo/s2_1/log/Mongo. log -- logappend -- fork

On serverb: mongod -- replset S2 -- Port 27021 -- dbpath =/data/Mongo/S2_2/DB -- logpath =/data/Mongo/S2_2/log/Mongo. log -- logappend -- fork

On serverc: mongod -- replset S2 -- Port 27021 -- dbpath =/data/Mongo/s2_3/DB -- logpath =/data/Mongo/s2_3/log/Mongo. log -- logappend -- fork

Connects any of the three nodes to initiate the replication set S2

> Use Admin

> Config = {_ ID: 's2 ', members: [{_ ID: 0, host: 'servera: 27021'}, {_ ID: 1, host: 'serverb: 27021 ', Priority: 1}, {_ ID: 2, host: 'serverc: 27021'}]}

> Rs. Initiate (config)

> Rs. Status ()

In addition, execute the following command on all nodes of the replica set S2 to ensure that all nodes share the read pressure.

> DB. getmongo (). setslaveok ();

3. Configure three config servers:

On servera:mongod  -fork --configsvr --dbpath /data/mongo/config/db --port 27018 --logpath /data/mongo/config/log/mongo.log --fork

  On serverb:mongod  -fork --configsvr --dbpath /data/mongo/config/db --port 27018 --logpath /data/mongo/config/log/mongo.log --fork

  On serverc:mongod  -fork --configsvr --dbpath /data/mongo/config/db --port 27018 --logpath /data/mongo/config/log/mongo.log --fork

4. Configure three route servers:

On servera:mongos -fork --logpath /data/mongo/route/log/mongo.log --configdb ServerA:27018,ServerB:27018,ServerC:27018 --port 27017

  On serverb:mongos -fork --logpath /data/mongo/route/log/mongo.log --configdb ServerA:27018,ServerB:27018,ServerC:27018 --port 27017

  On serverc:mongos -fork --logpath /data/mongo/route/log/mongo.log --configdb ServerA:27018,ServerB:27018,ServerC:27018 --port 27017

5. Configure the shard cluster:

Connect to any mongos process and execute the following command:

Use Admin

DB. runcommand ({addshard: "shard1/servera: 27020, serverb: 27020, serverc: 27020 "})

DB. runcommand ({addshard: "shard2/servera: 27021, serverb: 27021, serverc: 27021 "})

DB. printshardingstatus ()

6. Activate the sharding function of the database and set:

Connect to any mongos process and execute the following command:

DB. runcommand ({enablesharding: "testdb "})

DB. runcommand ({shardcollection: "testdb. collection_test", key: {_ ID: 1 }})

7. log on to mongos and add a user:

Use Admin

DB. adduser ("<user>", "<password> ")

DB. adduser ("<user>", "<password>", true) // Add a read-only user

8. Disable all mongod and mongos of the three machines:

Sudo killall mongod

Sudo killall mongos

9. Generate Keyfile: (the key files of each process are consistent)

OpenSSL rand-base64 753> Keyfile

Copy the generated Keyfile to the folder corresponding to the mongod/mongos process.

Execute the statement to change the permission: sudo chmod 600 Keyfile

Use the -- Keyfile parameter to specify the previously generated Keyfile, and restart all mongod and mongos processes on the three machines.

On servera: mongod -- replset S1 -- Port 27020 -- dbpath =/data/Mongo/s1_1/DB -- logpath =/data/Mongo/s1_1/log/Mongo. log -- logappend -- fork -- Keyfile/data/Mongo/s1_1/Keyfile

On serverb: mongod -- replset S1 -- Port 27020 -- dbpath =/data/Mongo/s1_2/DB -- logpath =/data/Mongo/s1_2/log/Mongo. log -- logappend -- fork -- Keyfile/data/Mongo/s1_2/Keyfile

On serverc: mongod -- replset S1 -- Port 27020 -- dbpath =/data/Mongo/s1_3/DB -- logpath =/data/Mongo/s1_3/log/Mongo. log -- logappend -- fork -- Keyfile/data/Mongo/s1_3/Keyfile

  

On servera: mongod -- replset S2 -- Port 27021 -- dbpath =/data/Mongo/s2_1/DB -- logpath =/data/Mongo/s2_1/log/Mongo. log -- logappend -- fork -- Keyfile/data/Mongo/s2_1/Keyfile

On serverb: mongod -- replset S2 -- Port 27021 -- dbpath =/data/Mongo/S2_2/DB -- logpath =/data/Mongo/S2_2/log/Mongo. log -- logappend -- fork -- Keyfile/data/Mongo/S2_2/Keyfile

On serverc: mongod -- replset S2 -- Port 27021 -- dbpath =/data/Mongo/s2_3/DB -- logpath =/data/Mongo/s2_3/log/Mongo. log -- logappend -- fork -- Keyfile/data/Mongo/s2_3/Keyfile

  

On servera:mongod  -fork --configsvr --dbpath /data/mongo/config/db --port 27018 --logpath /data/mongo/config/log/mongo.log --fork --keyFile  /data/mongo/config/keyfile

  On serverb:mongod  -fork --configsvr --dbpath /data/mongo/config/db --port 27018 --logpath /data/mongo/config/log/mongo.log --fork --keyFile  /data/mongo/config/keyfile

  On serverc:mongod  -fork --configsvr --dbpath /data/mongo/config/db --port 27018 --logpath /data/mongo/config/log/mongo.log --fork --keyFile  /data/mongo/config/keyfile

 

On servera:mongos -fork --logpath /data/mongo/route/log/mongo.log --configdb ServerA:27018,ServerB:27018,ServerC:27018 --port 27017 --keyFile  /data/mongo/route/keyfile

  On serverb:mongos -fork --logpath /data/mongo/route/log/mongo.log --configdb ServerA:27018,ServerB:27018,ServerC:27018 --port 27017 --keyFile  /data/mongo/route/keyfile

  On serverc:mongos -fork --logpath /data/mongo/route/log/mongo.log --configdb ServerA:27018,ServerB:27018,ServerC:27018 --port 27017 --keyFile  /data/mongo/route/keyfile

Finished!

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.