|
| 1 | +# Configure a systemd unit file for `mongos` |
| 2 | + |
| 3 | +`mongos` provides the entry point for an application to connect to a sharded cluster. To automate the `mongos` process management, you can use a system unit file. This file defines how the `mongos` service should behave when the system boots, shuts down, or encounters an issue. |
| 4 | + |
| 5 | +This document provides a sample configuration for a `mongos` systemd unit file that you can use and/or modify to meet your specific needs. For security considerations, cluster components use a keyfile for internal authentication. |
| 6 | + |
| 7 | +## Before you start |
| 8 | + |
| 9 | +1. Ensure you have a working config server replica set and shards. Refer to the [deployment documentation :octicons-link-external-16:](https://www.mongodb.com/docs/manual/tutorial/deploy-sharded-cluster-with-keyfile-access-control/#create-the-config-server-replica-set) for guidelines |
| 10 | + |
| 11 | +2. Check that you have fulfilled all prerequisites in your system: |
| 12 | + * /var/log/mongo directory is created |
| 13 | + * If SELinux is in use, /var/run/mongos.pid is added to the policy so mongos process can create it |
| 14 | + |
| 15 | +3. Get the shared key file from any existing member of the cluster. Refer to the [MongoDB documentation :octicons-link-external-16:](https://www.mongodb.com/docs/manual/reference/configuration-options/#mongodb-setting-security.keyFile) for how to create keyfiles. |
| 16 | + |
| 17 | +## Procedure |
| 18 | + |
| 19 | +The steps are the following: |
| 20 | +{.power-number} |
| 21 | + |
| 22 | +1. Create a `mongos` user and a group. This user will own the `mongos` process. Use the following command: |
| 23 | + |
| 24 | + ```{.bash data-prompt="$"} |
| 25 | + $ groupadd mongos && sudo useradd -r -s /bin/false -g mongos mongos |
| 26 | + ``` |
| 27 | + |
| 28 | +2. Create the environment file at the path `/etc/sysconfig/mongos` and specify the following environment variables within: |
| 29 | + |
| 30 | + ```ini title="/etc/sysconfig/mongos" |
| 31 | + OPTIONS="-f /etc/mongos.conf" |
| 32 | + STDOUT="/var/log/mongo/mongos.stdout" |
| 33 | + STDERR="/var/log/mongo/mongos.stderr" |
| 34 | + ``` |
| 35 | + |
| 36 | +3. Create a `mongos` configuration file at the path `/etc/mongos.conf`. In the following example configuration, replace the `security.keyfile` with the path to your keyfile and specify the name of the config server replica set and its members in the format `hostname:port`: |
| 37 | + |
| 38 | + ```yaml title="/etc/mongos.conf" |
| 39 | + # where to write logging data. |
| 40 | + systemLog: |
| 41 | + destination: file |
| 42 | + logAppend: true |
| 43 | + path: /var/log/mongo/mongos.log |
| 44 | +
|
| 45 | + processManagement: |
| 46 | + fork: true |
| 47 | + pidFilePath: /var/run/mongos.pid |
| 48 | +
|
| 49 | + # network interfaces |
| 50 | + net: |
| 51 | + port: 27017 |
| 52 | + bindIp: 127.0.0.1 |
| 53 | +
|
| 54 | + security: |
| 55 | + keyFile: /etc/mongos.key |
| 56 | +
|
| 57 | + sharding: |
| 58 | + configDB: configRS/cfg1.example.com:27017,cfg2.example.com:27017,cfg3.example.com:27017 |
| 59 | + ``` |
| 60 | + |
| 61 | +4. Create the systemd unit file at the path `/usr/lib/systemd/system/mongos.service`. Specify the following configuration: |
| 62 | + |
| 63 | + ```{.bash data-prompt="$"} |
| 64 | + $ tee /usr/lib/systemd/system/mongos.service <<EOF |
| 65 | + [Unit] |
| 66 | + Description=High-performance, schema-free document-oriented database |
| 67 | + After=time-sync.target network.target |
| 68 | +
|
| 69 | + [Service] |
| 70 | + Type=forking |
| 71 | + User=mongos |
| 72 | + Group=mongos |
| 73 | + PermissionsStartOnly=true |
| 74 | + LimitFSIZE=infinity |
| 75 | + LimitCPU=infinity |
| 76 | + LimitAS=infinity |
| 77 | + LimitNOFILE=64000 |
| 78 | + LimitNPROC=64000 |
| 79 | + EnvironmentFile=/etc/sysconfig/mongos |
| 80 | + ExecStart=/usr/bin/env bash -c "/usr/bin/mongos $OPTIONS > ${STDOUT} 2> ${STDERR}" |
| 81 | + PIDFile=/var/run/mongos.pid |
| 82 | +
|
| 83 | + [Install] |
| 84 | + WantedBy=multi-user.target |
| 85 | + EOF |
| 86 | + ``` |
| 87 | +
|
| 88 | +5. Grant read/write access for the `mongos` user to the following directories and files: |
| 89 | +
|
| 90 | + ```{.bash data-prompt="$"} |
| 91 | + $ sudo chown -R mongos:mongos /var/log/mongo \ |
| 92 | + /var/run/mongos.pid \ |
| 93 | + /etc/mongos.conf \ |
| 94 | + /etc/sysconfig/mongos \ |
| 95 | + <path-to-keyfile> |
| 96 | + ``` |
| 97 | +6. Reload the systemd daemon to apply the changes: |
| 98 | +
|
| 99 | + ```{.bash data-prompt="$"} |
| 100 | + $ sudo systemctl daemon-reload |
| 101 | + ``` |
| 102 | +
|
| 103 | +7. Start the `mongos` service: |
| 104 | +
|
| 105 | + ```{.bash data-prompt="$"} |
| 106 | + $ sudo systemctl start mongos |
| 107 | + ``` |
| 108 | +
|
0 commit comments