MongoDB Sharding Installation and Configuration with Authentication
Recently I got a chance to install & configure MongoDB Sharded Cluster at one of our Production Environment. Here I am showing you how I have set up a MongoDB cluster.
So basically while configuration of MongoDB sharding we should take care about configuration of three major components which are Mongos (Router), Config Server and Shards (which can be 2 and more).
Diagram:-
So here I am configuring MongoDB Cluster with 2 Shards.
For MongoDB Sharding I have choosed total 6 boxes. I have pick 3 servers for Mongos and config Server Only and rest 3 servers to configure config Server replica Set, shard1 and shard2 replica sets.
Note:- I picked 3 low resource boxes for Mongos and config Server as Mongos are very lightweight service and Config servers are also generally lightweight in terms of storage and CPU/memory usage, as the metadata for a sharded cluster is significantly smaller than the actual cluster data. For Shard configuration I have picked 3 servers which hardware resources will definitely at higher end as these are the data bearing nodes.
IP, Port & Application Details:-
Here I used MongoDB — 4.0.9 version and all 6 boxes have Centos 7 OS installed.
First we install MongoDB on all above mentioned 6 boxes by following below procedure. Here I am going to install MongoDB from the Binary installation method.
— Download MgoDB-4.0.9 from MongoDB official website with following below URL’s.
cd /opt
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-4.0.9.tgz
I am installing mongoDB at /usr/local path by executing below command.
cd /usr/local
tar -xvzf /opt/mongodb-linux-x86_64–4.0.9.tgz
Then mongodb directory has created at path /usr/local
cd /usr/local/mongodb-linux-x86_64–4.0.9
Also create a linux user
groupadd mongodb
useradd -g mongodb -s /bin/bash mongodb
Now we will do these steps on the rest of the boxes for MongoDB installation.
Config Server Configuration:-
Then we will start configuring Config Server, Shards and Mongos.
First I’m going to start configuration of Config Servers.
Step 1:-
On 192.168.0.1, I am creating a directory at path /usr/local/mongodb-linux-x86_64–4.0.9
cd /usr/local/mongodb-linux-x86_64–4.0.9
mkdir mongodb_configserver
cd mongodb_configserver
then create keyfile using below commands :
openssl rand -base64 756 > keyfile
chmod 400 keyfile
I am using keyfile based authentication so we have to create a keyfile using above commnd
Note:- keyfile should be same in a cluster means we have to copy this keyfile on rest of the replica sets.
Step 2:-
Now create config.conf file and copy below contents and save it.
#### mongod config server
fork=true
quiet=true
port=27019
dbpath=/usr/local/mongodb-linux-x86_64–4.0.9/mongodb_configserver
logpath=/usr/local/mongodb-linux-x86_64–4.0.9/mongodb_configserver/config.log
logappend=true
bind_ip = 0.0.0.0
auth=true
keyFile=/usr/local/mongodb-linux-x86_64–4.0.9/mongodb_configserver/keyfile
configsvr=true
replSet=REPLSET-CONFIGSERVER
the change the ownership of chown mongodb.mongodb mongodb_configserver data directory by executing
chown mongodb.mongodb /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_configserver -R
and then start the mongoDB process for configServer by using below command.
runuser -l mongodb -c ‘/usr/local/mongodb-linux-x86_64–4.0.9/bin/mongod -f /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_configserve/config.conf’
Step 3:-
Now connect to config server by executing below command:-
./bin/mongo — port=27019
Then create a superUser by executing below command in mongodb shell
> use admin
> db.createUser({user: “dios”, pwd: “supreme”, roles: [“root”]});
> db.auth(“dios”,”supreme”) ## for authorization
> rs.initiate() ## Intialization of replicaSet
REPLSET-CONFIGSERVER:PRIMARY>
Now we have configured one config server. Now we are going to repeat all the steps except step 3 on 192.168.0.2 & 192.168.0.3
Once we start config Server on 192.168.0.2 & 192.168.0.3 as well, we will go to 192.168.0.1 and execute below queries after login on mongo Shell.
./bin/mongo — port=27019
REPLSET-CONFIGSERVER:PRIMARY>use admin
REPLSET-CONFIGSERVER:PRIMARY>db.auth(“dios”,”supreme”)
REPLSET-CONFIGSERVER:PRIMARY>rs.add(“192.168.0.2:27019”)
REPLSET-CONFIGSERVER:PRIMARY>rs.add({“host”:“192.168.0.3:27019”, “priority” : 0 , “hidden”: true ,”votes” : 1 })
As I am using 192.168.0.3 for backup purpose that’s why I defined priority 0 & hidden true for same
Config servers replica Set is ready. & We have 3 members in a replica Set now.
Shard1 Configuration:-
Now we will go towards MongoDB shards configuration.
So I am going to configure two shards shard1 and shard2 on 192.168.0.11/192.168.0.12/192.168.0.13 and 192.168.0.13 will be used for backup purposes.
First we will setup Shard1 on 192.168.0.11
Step 1:-
Created a directory with named mongodb_shard1 at path ‘/usr/local/mongodb-linux-x86_64–4.0.9/’ by executing below commands
cd /usr/local/mongodb-linux-x86_64–4.0.9/
mkdir mongodb_shard1
As I mentioned above, Copy keyfile from 192.168.0.1 to 192.168.0.11.
At path /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard1/
Then create a file mongod.conf and paste below lines.
######MongoDB Shard1###########
logpath = /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard1/mongod.log
logappend = true
fork = true
dbpath = /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard1
directoryperdb=true
journal=true
pidfilepath = /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard1/mongod.pid
port = 27027
maxConns = 5000
bind_ip = 0.0.0.0
auth=true
keyFile=/usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard1/keyfile
replSet=REPLSET-SHARD1
oplogSize=22048
shardsvr=true
Then save this conf file.
Change the ownership of below mentioned director.
Also /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard1/keyfile permission should be 400.
chmod 400 /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard1/
chown mongodb.mongodb /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard1/ -R
Step 2 : -
Now we are going to start the mongoDB shard1 on 192.168.2.11
runuser -l mongodb -c ‘/usr/local/mongodb-linux-x86_64–4.0.9/bin/mongod -f /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard1/mongod.conf’
Step 3
Connect to mongoDB Shard1 with below command.
./bin/mongo — port=27027
> use admin
> db.createUser({user: “dios”, pwd: “supreme”, roles: [“root”]});
> db.auth(“dios”,”supreme”) ## for authorization
> rs.initiate() ## Intialization of replicaSet
REPLSET-SHARD1:PRIMARY> ###
Now Shard1 one member is configured at 192.168.2.11.
Repeat Step 1 & Step 2 on the rest of the Servers 192.168.2.12/192.168.2.13.
Then again Login to 192.168.2.11 and login to shard1 mongo shell
./bin/mongo — port=27027
REPLSET-SHARD1:PRIMARY> use admin
REPLSET-SHARD1:PRIMARY> db.auth(“dios”,”supreme”) ## for authorization
REPLSET-SHARD1:PRIMARY> rs.add(“192.168.0.12:27027”)
REPLSET-SHARD1:PRIMARY> rs.add({“host”:“192.168.0.13:27027”, “priority” : 0 , “hidden”: true ,”votes” : 1 })
Now the Shard1 replica Set is ready.
Shard2 Configuration:-
Now I am going to start Shard2 configuration
First we will setup Shard1 on 192.168.0.11
Step 1:-
Created a directory with named mongodb_shard1 at path ‘/usr/local/mongodb-linux-x86_64–4.0.9/’ by executing below commands
cd /usr/local/mongodb-linux-x86_64–4.0.9/
mkdir mongodb_shard2
As I mentioned above, Copy keyfile from 192.168.0.1 to 192.168.0.11.
At path /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard2/
Then create a file mongod.conf and paste below lines.
######MongoDB Shard2 Configuration###########
logpath = /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard2/mongod.log
logappend = true
fork = true
dbpath = /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard2
directoryperdb=true
journal=true
pidfilepath = /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard2/mongod.pid
port = 27037
maxConns = 5000
bind_ip = 0.0.0.0
auth=true
keyFile=/usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard2/keyfile
replSet=REPLSET-SHARD2
oplogSize=22048
shardsvr=true
Then save this conf file.
Change the ownership of below mentioned director.
Also /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard2/keyfile permission should be 400.
chmod 400 /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard2/
chown mongodb.mongodb /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard2/ -R
Step 2 : -
Now we are going to start the mongoDB shard2 on 192.168.2.11
runuser -l mongodb -c ‘/usr/local/mongodb-linux-x86_64–4.0.9/bin/mongod -f /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_shard2/mongod.conf’
Step 3
Connect to mongoDB Shard2 with below command.
./bin/mongo — port=27037
> use admin
> db.createUser({user: “dios”, pwd: “supreme”, roles: [“root”]});
> db.auth(“dios”,”supreme”) ## for authorization
> rs.initiate() ## Intialization of replicaSet
REPLSET-SHARD2:PRIMARY> ###
Now one member is configured at 192.168.2.11 for Shard2.
Repeat Step 1 & Step 2 on the rest of the Servers 192.168.2.12/192.168.2.13.
Then again Login to 192.168.2.11 and login to shard2 mongo shell
./bin/mongo — port=27037
REPLSET-SHARD2:PRIMARY> use admin
REPLSET-SHARD2:PRIMARY> db.auth(“dios”,”supreme”) ## for authorization
REPLSET-SHARD2:PRIMARY> rs.add(“192.168.0.12:27037”)
REPLSET-SHARD2:PRIMARY> rs.add({“host”:“192.168.0.13:27037”, “priority” : 0 , “hidden”: true ,”votes” : 1 })
Now shard2 replica set is also configured.
Mongos (Router) Configuration:-
Now we are going to start mongos configuration on 192.168.2.1/192.168.2.2/192.168.2.3.
I am starting configuring mongos (router) on 192.168.2.1
Step 1:-
Create directory mongodb_router at path ‘/usr/local/mongodb-linux-x86_64–4.0.9’
mkdir /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_router
Copy above mentioned “keyfile” at /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_router/
Create router.conf file at path /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_router/
And paste below mentioned content on it.
#####Router Config##########
fork=true
quiet=true
port=27020
logpath=/usr/local/mongodb-linux-x86_64–4.0.9/mongodb_router/router.log
logappend=true
keyFile=/usr/local/mongodb-linux-x86_64–4.0.9/mongodb_router/keyfile
bind_ip = 0.0.0.0
configdb=REPLSET-CONFIGSERVER/192.168.2.1:27019,192.168.2.2:27019,192.168.2.3:27019
Execute below mentioned command:-
chown -R mongodb.mongodb /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_router/
chmod 400 /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_router/keyfile
Step 2:-
Now start mongoDB router.
runuser -l mongodb -c ‘/usr/local/mongodb-linux-x86_64–4.0.9/bin/mongos — config /usr/local/mongodb-linux-x86_64–4.0.9/mongodb_router/router.conf’
Now Our MongoDB monogs (router) has been configured now on 192.168.2.1
Repeat Step 1 and Step to config MongoDB Router configuration on 192.168.2.2/192.168.2.3.
Add shards to Cluster:-
Go to 192.168.2.1 or any of the mongos configured boxes.
Login to mongos via below command.
./bin/mongo — port=27020
mongos>use admin
mongos> db.auth(“dios”,”supreme”)
mongos>sh.addShard( “REPLSET-SHARD1/192.168.2.11:27027,192.168.2.12:27027,192.168.2.13:27027”)
mongos>sh.addShard( “REPLSET-SHARD1/192.168.2.11:27037,192.168.2.12:27037,192.168.2.13:27037”)
Now shards have been added in the cluster.