详见https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/#run-mongodb-community-edition

Production Notes

Before deploying MongoDB in a production environment, consider the Production Notes document which offers performance considerations and configuration recommendations for production MongoDB deployments.

Install MongoDB Community Edition

Follow these steps to install MongoDB Community Edition using the yum package manager.

1. Configure the package management system (yum).

Create a /etc/yum.repos.d/mongodb-org-4.2.repo file so that you can install MongoDB directly using yum:**

  1. [mongodb-org-4.2]
  2. name=MongoDB Repository
  3. baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
  4. gpgcheck=1
  5. enabled=1
  6. gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

You can also download the .rpm files directly from the MongoDB repository. Downloads are organized by Red Hat / CentOS version (e.g. 7), then MongoDB release version (e.g. 4.2), then architecture (e.g. x86_64). Odd-numbered MongoDB release versions, such as 4.3, are development versions and are unsuitable for production deployment.

2. Install the MongoDB packages.

To install the latest stable version of MongoDB, issue the following command:

  1. sudo yum install -y mongodb-org

Alternatively, to install a specific release of MongoDB, specify each component package individually and append the version number to the package name, as in the following example:

  1. sudo yum install -y mongodb-org-4.2.8 mongodb-org-server-4.2.8 mongodb-org-shell-4.2.8 mongodb-org-mongos-4.2.8 mongodb-org-tools-4.2.8

Run MongoDB Community Edition

Non-Default MongoDB Directory Path(s)
  1. Update the SELinux policy to allow themongodservice to use the new directory:copycopied

semanage fcontext -a -t

  1. where specify one of the following types as appropriate:
    • mongod_var_lib_t for data directory
    • mongod_log_t for log file directory
    • mongod_var_run_t for pid file directory
  2. NOTE
    Be sure to include the .* at the end of the directory.
  3. Update the SELinux user policy for the new directory:copycopied

chcon -Rv -u system_u -t

  1. where specify one of the following types as appropriate:
    • mongod_var_lib_t for data directory
    • mongod_log_t for log directory
    • mongod_var_run_t for pid file directory
  2. Apply the updated SELinux policies to the directory:
    copy
    copied

restorecon -R -v

For examples:
TIP

  • Depending on your user permission, you may need to use sudo to perform these operations.
  • Be sure to include the .* at the end of the directory for the semanage fcontext operations.
  • If using a non-default MongoDB data path of /mongodb/data:
    copy
    copied

semanage fcontext -a -t mongod_var_lib_t ‘/mongodb/data.*’
chcon -Rv -u system_u -t mongod_var_lib_t ‘/mongodb/data’
restorecon -R -v ‘/mongodb/data’

  • If using a non-default MongoDB log directory of /mongodb/log (e.g. if the log file path is /mongodb/log/mongod.log):
    copy
    copied

semanage fcontext -a -t mongod_log_t ‘/mongodb/log.*’
chcon -Rv -u system_u -t mongod_log_t ‘/mongodb/log’
restorecon -R -v ‘/mongodb/log’

Non-Default MongoDB Ports

TIP
Depending on your user permission, you may need to use sudo to perform the operation.
copy
copied
semanage port -a -t mongod_port_t -p tcp