Configure Query RouterPermalink In this section, we’ll set up the MongoDB query router. The query router obtains metadata from the config servers, caches it, and uses that metadata to send read and write queries to the correct shards.
All steps here should be performed from your query router Linode (this will be the same as your application server). Since we’re only configuring one query router, we’ll only need to do this once. However, it’s also possible to use a replica set of query routers. If you’re using more than one (i.e., in a high availability setup), perform these steps on each query router Linode.
Create a new configuration file called /etc/mongos.conf, and supply the following values:
/etc/mongos.conf
systemLog: destination: file logAppend: true path: /var/log/mongodb/mongos.log
net: port: 27017 bindIp: 192.0.2.4
security: keyFile: /opt/mongo/mongodb-keyfile
sharding: configDB: configReplSet/mongo-config-1:27019,mongo-config-2:27019,mongo-config-3:27019 Replace 192.0.2.4 with your router Linode’s private IP address, and save the file.
Create a new systemd unit file for mongos called /lib/systemd/system/mongos.service, with the following information:
/lib/systemd/system/mongos.service
[Unit] Description=Mongo Cluster Router After=network.target
[Service] User=mongodb Group=mongodb ExecStart=/usr/bin/mongos --config /etc/mongos.conf
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
TasksMax=infinity TasksAccounting=false
[Install] WantedBy=multi-user.target Note that the above example uses the mongodb user that MongoDB runs as by default on Ubuntu and Debian. If you’re using CentOS, substitute the following values under the Service section of the file:
[Service] User=mongod Group=mongod The mongos service needs to obtain data locks that conflicts with mongod, so be sure mongod is stopped before proceeding:
sudo systemctl stop mongod Enable mongos.service so that it automatically starts on reboot, and then initialize the mongos:
sudo systemctl enable mongos.service sudo systemctl start mongos Confirm that mongos is running:
systemctl status mongos You should see output similar to this:
Loaded: loaded (/lib/systemd/system/mongos.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2017-01-31 19:43:05 UTC; 10s ago Main PID: 3901 (mongos) CGroup: /system.slice/mongos.service └─3901 /usr/bin/mongos --config /etc/mongos.conf
[Unit]
Description=Mongo Cluster Router
After=network.target
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongos --config /etc/mongos.conf
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
[Install]
WantedBy=multi-user.target