Initially in primary config server create the key file with OpenSSL and copy the same SSL file to another server in the same location
1.openssl rand -base64 756 > mongo-keyfile
2.sudo mkdir /data/mongo
3.sudo mv ~/mongo-keyfile /data/mongo
4.sudo chmod 400 /data/mongo/mongo-keyfile
5.sudo chown mongodb:mongodb /data/mongo/mongo-keyfile
Once a key file is created, add value in all the /etc/mongod.conf. Its should be same as below because mongod.conf file is case sensitive
security:
keyFile: /opt/mongo/mongodb-keyfile
sudo systemctl restart mongod
Main Components :
Config Server: This stores metadata and configuration settings for the rest of the cluster
Query Router: The Mongols daemon acts as an interface between the client application and the cluster shards. It’s like a listener of mongo instances
Shard: A database server that holds a portion of your data. Items in the database are divided among shards either by range or hashing
Steps involving in the configuration :
1.Configure the config servers
2.Configure the Query Router
3.Configure the sharding
1.Configure the config servers
Using single config server is not enough to maintain the metadata at the time of the disaster, we are setting up one primary and two secondary replica set
On each config server, edit below values in mongod.conf. bind IP values will be different for each server
Then restart mongo service using below command on each config servers
sudo systemctl restart mongod
Once restarted initiate the config server using below command, please replace the hostnames accordingly
And do check the rs.status of config server replica sets
Configuring the config server is completed, let's move on next steps
2.Configure the Query Router
Using the config server metadata information, send read and write queries to the correct shards
Create /etc/mongos.conf file and add the below lines
Create a new systemd unit file for mongos called /lib/systemd/system/mongos.service
Once we created files, needs to enable systemctl for mongos.service using below commands
1.sudo systemctl stop mongod
2.sudo systemctl enable mongos.service
3.sudo systemctl start mongos
4.systemctl status mongos
3.Configure the sharding servers
On each shared server, edit below values in mongod.conf. Bind IP values will be different for each server and restart the mongod service
Once everything is completed, using mongo query router address login into any one of shared servers, I have created a separate admin user for MongoDB. If required create it
mongo 172.31.42.214:27017 -u adminuser -p --authenticationDatabase admin
Connect mongos interface and add the shared nodes, if you have replica set for shared nodes steps will be different to add shared
It’s done, shared000 and shared001 are added. There are many links available for sharding the database and collections to mangos. Tried with below examples for my test and its working as expected
Thanks for reading !!!