Docs HomeMongoDB Manual

Run-time Database Configuration运行时数据库配置

The command line and configuration file interfaces provide MongoDB administrators with a large number of options and settings for controlling the operation of the database system. 命令行配置文件接口为MongoDB管理员提供了大量选项和设置,用于控制数据库系统的操作。This document provides an overview of common configurations and examples of best-practice configurations for common use cases.本文档概述了常见配置以及常见用例的最佳实践配置示例。

While both interfaces provide access to the same collection of options and settings, this document primarily uses the configuration file interface.虽然这两个接口都提供了对相同选项和设置集合的访问,但本文档主要使用配置文件接口。

  • If you installed MongoDB with a package manager such as yum or apt on Linux or brew on macOS, or with the MSI installer on Windows, a default configuration file has been provided as part of your installation:如果您在Linux上使用诸如yumapt之类的软件包管理器安装MongoDB,或在macOS上使用brew,或在Windows上使用MSI安装程序安装,则会在安装过程中提供默认配置文件

    Platform平台Method方法Configuration File配置文件
    Linuxapt, yum, or zypper Package Manager/etc/mongod.conf
    macOSbrew Package Manager/usr/local/etc/mongod.conf (on Intel processors), or
    /opt/homebrew/etc/mongod.conf (on Apple M1 processors)
    WindowsMSI Installer<install directory>\bin\mongod.cfg
  • If you installed MongoDB via a downloaded TGZ or ZIP file, you will need to create your own configuration file. 如果您通过下载的TGZZIP文件安装MongoDB,则需要创建自己的配置文件。The basic example configuration is a good place to start.基本示例配置是一个很好的起点。

For package installations of MongoDB on Linux or macOS, an initialization script which uses this default configuration file is also provided. 对于Linux或macOS上的MongoDB包安装,还提供了使用此默认配置文件的初始化脚本。This initialization script can be used to start the mongod on these platforms in the following manner:此初始化脚本可用于以以下方式在这些平台上启动mongod

  • On Linux systems that use the systemd init system (the systemctl command):在使用systemd-init系统(systemctl命令)的Linux系统上:

    sudo systemctl start mongod
  • On Linux systems that use the SystemV init init system (the service command):在使用SystemV init init系统(service命令)的Linux系统上:

    sudo service mongod start
  • On macOS, using the brew package manger:在macOS上,使用brew包管理器:

    brew services start mongodb-community@7.0

If you installed MongoDB using a TGZ or ZIP file, you will need to create your own configuration file. 如果您使用TGZZIP文件安装MongoDB,则需要创建自己的配置文件。A basic example configuration can be found later in this document. 基本配置示例可在本文档后面找到。Once you have created a configuration file, you can start a MongoDB instance with this configuration file by using either the --config or -f options to mongod. 一旦创建了一个配置文件,就可以通过使用mongod--config-f选项来使用该配置文件启动MongoDB实例。For example, on Linux:例如,在Linux上:

mongod --config /etc/mongod.conf
mongod -f /etc/mongod.conf

Modify the values in the mongod.conf file on your system to control the configuration of your database instance.修改系统上mongod.conf文件中的值,以控制数据库实例的配置。

Configure the Database配置数据库

Consider the following basic configuration:考虑以下基本配置:

processManagement:
fork: true
net:
bindIp: localhost
port: 27017
storage:
dbPath: /var/lib/mongo
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true

For most standalone servers, this is a sufficient base configuration. It makes several assumptions, but consider the following explanation:对于大多数独立服务器来说,这是一个足够的基本配置。它做出了几个假设,但考虑以下解释:

  • fork is true, which enables a daemon mode for mongod, which detaches (i.e. "forks") the MongoDB from the current session and allows you to run the database as a conventional server.forktrue,它为mongod启用了守护进程模式,该模式将MongoDB从当前会话中分离(即“forks”),并允许您将数据库作为传统服务器运行。
  • bindIp is localhost, which forces the server to only listen for requests on the localhost IP. bindIplocalhost,它强一致性务器只侦听localhost IP上的请求。Only bind to secure interfaces that the application-level systems can access with access control provided by system network filtering (i.e. "firewall").仅绑定到应用程序级系统可以通过系统网络筛选(即“防火墙”)提供的访问控制访问的安全接口。
  • port is 27017, which is the default MongoDB port for database instances. MongoDB can bind to any port. port27017,这是数据库实例的默认MongoDB端口。MongoDB可以绑定到任何端口。You can also filter access based on port using network filtering tools.您也可以使用网络筛选工具根据端口筛选访问。

    Note

    UNIX-like systems require superuser privileges to attach processes to ports lower than 1024.类UNIX系统需要超级用户权限才能将进程连接到低于1024的端口。

  • quiet is true. trueThis disables all but the most critical entries in output/log file, and is not recommended for production systems. 这将禁用输出/日志文件中除最关键的条目外的所有条目,不建议用于生产系统。If you do set this option, you can use setParameter to modify this setting during run time.如果确实设置了此选项,则可以在运行时使用setParameter修改此设置。
  • dbPath is /var/lib/mongo, which specifies where MongoDB will store its data files./var/lib/mongo,它指定MongoDB将数据文件存储在哪里。

    If you installed MongoDB on Linux using a package manager, such as yum or apt, the /etc/mongod.conf file provided with your MongoDB installation sets the following default dbPath, depending on your Linux distro:如果您在Linux上使用包管理器(如yumapt)安装MongoDB,则MongoDB安装附带的/etc/mongod.conf文件会根据您的Linux发行版设置以下默认dbPath:

    Platform平台Package Manager程序包管理器Default 默认dbPath
    RHEL / CentOS and Amazonyum/var/lib/mongo
    SUSEzypper/var/lib/mongo
    Ubuntu and Debianapt/var/lib/mongodb
    macOSbrew/usr/local/var/mongodb

    The user account that mongod runs under will need read and write access to this directory.mongod运行的用户帐户需要对该目录进行读写访问。

  • systemLog.path is /var/log/mongodb/mongod.log which is where mongod will write its output. /var/log/mongodb/mongod.logmongod将在这里编写输出。If you do not set this value, mongod writes all output to standard output (e.g. stdout.)如果不设置此值,mongod会将所有输出写入标准输出(例如stdout
  • logAppend is true, which ensures that mongod does not overwrite an existing log file following the server start operation.logAppendtrue,它确保mongod不会在服务器启动操作后覆盖现有的日志文件。

Given the default configuration, some of these values may be redundant. However, in many situations explicitly stating the configuration increases overall system intelligibility.给定默认配置,其中一些值可能是多余的。然而,在许多情况下,明确说明配置会增加整个系统的可理解性。

Security Considerations安全注意事项

The following configuration options are useful for limiting access to a mongod instance:以下配置选项可用于限制对mongod实例的访问:

net:
bindIp: localhost,10.8.0.10,192.168.4.24,/tmp/mongod.sock
security:
authorization: enabled
net.bindIp

This example provides four values to the bindIp option:此示例为bindIp选项提供了四个值:

  • localhost, the localhost interface;,localhost接口;
  • 10.8.0.10, a private IP address typically used for local networks and VPN interfaces;,通常用于本地网络和VPN接口的专用IP地址;
  • 192.168.4.24, a private network interface typically used for local networks; and,通常用于本地网络的专用网络接口;和
  • /tmp/mongod.sock, a Unix domain socket path.,Unix域套接字路径。

Because production MongoDB instances need to be accessible from multiple database servers, it is important to bind MongoDB to multiple interfaces that are accessible from your application servers. 因为生产MongoDB实例需要可以从多个数据库服务器访问,所以将MongoDB绑定到可以从应用程序服务器访问的多个接口非常重要。At the same time it's important to limit these interfaces to interfaces controlled and protected at the network layer.同时,重要的是要将这些接口限制为在网络层控制和保护的接口。

security.authorization
Setting this option to true enables the authorization system within MongoDB. If enabled you will need to log in by connecting over the localhost interface for the first time to create user credentials.将此选项设置为true将启用MongoDB中的授权系统。如果启用,您将需要第一次通过localhost接口连接登录以创建用户凭据。
Tip

See also: 另请参阅:

Security

Replication and Sharding Configuration复制和共享配置

Replication Configuration复制配置

replica set configuration is straightforward, and only requires that the replSetName have a value that is consistent among all members of the set. 副本集配置很简单,只要求replSetName的值在该集的所有成员之间保持一致。Consider the following:考虑以下内容:

replication:
replSetName: set0

Use descriptive names for sets. Once configured, use mongosh to add hosts to the replica set.对集合使用描述性名称。配置后,使用mongosh将主机添加到复制副本集中。

To enable authentication for the replica set using keyfiles , add the following keyFile option [1]:要使用键文件复制副本集启用身份验证,请添加以下keyFile选项[1]

security:
keyFile: /srv/mongodb/keyfile

Setting keyFile enables authentication and specifies a keyfile for the replica set member to use when authenticating to each other.设置keyFile可启用身份验证,并指定副本集成员在相互进行身份验证时要使用的键文件。

Tip

See also: 另请参阅:

The Replica Set Security section for information on configuring authentication with replica sets.有关使用副本集配置身份验证的信息,请参阅副本集安全性部分。

The Replication document for more information on replication in MongoDB and replica set configuration in general.有关MongoDB中复制和副本集配置的详细信息,请参阅复制文档。

[1] Sharded clusters and replica sets can use x.509 for membership verification instead of keyfiles. 共享集群和副本集可以使用x.509进行成员身份验证,而不是键文件。For details, see x.509.有关详细信息,请参阅x.509

Sharding Configuration分片配置

Sharding requires mongod instances with different mongod configurations for the config servers and the shards. 分片需要对配置服务器分片具有不同mongod配置的mongod实例。The config servers store the cluster's metadata, while the shards store the data.配置服务器存储集群的元数据,而分片存储数据。

To configure the config server mongod instances, in the configuration file, specify configsvr for the sharding.clusterRole setting.要配置配置服务器mongod实例,请在配置文件中为sharding.clusterRole设置指定configsvr

Changed in version 3.4.3.4版更改。Starting in version 3.4, MongoDB removes support for mirrored config servers and config servers must be deployed as a replica set.从3.4版本开始,MongoDB删除了对镜像配置服务器的支持,配置服务器必须作为副本集部署。

 sharding:
clusterRole: configsvr
net:
bindIp: 10.8.0.12
port: 27001
replication:
replSetName: csRS

To deploy config servers as a replica set, the config servers must run the WiredTiger Storage Engine. Initiate the replica set and add members.要将配置服务器部署为副本集,配置服务器必须运行WiredTiger存储引擎启动复制副本集并添加成员。

To configure the shard mongod instances, specify shardsvr for the sharding.clusterRole setting, and if running as a replica set, the replica set name:要配置分片-mongod实例,请为sharding.clusterRole设置指定shardsvr,如果作为副本集运行,则副本集名称为:

sharding:
clusterRole: shardsvr
replication:
replSetName: shardA

If running as a replica set, initiate the shard replica set and add members.如果作为副本集运行,请启动分片副本集并添加成员。

For the router (i.e. mongos), configure at least one mongos process with the following setting:对于路由器(即mongos),请使用以下设置至少配置一个mongos进程:

sharding:
configDB: csRS/10.8.0.12:27001

You can specify additional members of the config server replica set by specifying hostnames and ports in the form of a comma separated list after the replica set name.通过在副本集名称后以逗号分隔列表的形式指定主机名和端口,可以指定配置服务器副本集的其他成员。

Tip

See also: 另请参阅:

The Sharding section of the manual for more information on sharding and cluster configuration.有关分片和集群配置的更多信息,请参阅手册的分片部分。

Run Multiple Database Instances on the Same System在同一系统上运行多个数据库实例

In many cases running multiple instances of mongod on a single system is not recommended. 在许多情况下,不建议在单个系统上运行mongod的多个实例。On some types of deployments [2] and for testing purposes you may need to run more than one mongod on a single system.在某些类型的部署[2]上,出于测试目的,您可能需要在单个系统上运行多个mongod

In these cases, use a base configuration for each instance, but consider the following configuration values:在这些情况下,请为每个实例使用基本配置,但要考虑以下配置值:

storage:
dbPath: /var/lib/mongo/db0/
processManagement:
pidFilePath: /var/lib/mongo/db0.pid

The dbPath value controls the location of the mongod instance's data directory. Ensure that each database has a distinct and well labeled data directory. dbPath值控制mongod实例的数据目录的位置。确保每个数据库都有一个不同且标记良好的数据目录。The pidFilePath controls where mongod process places it's process ID (PID) file. pidFilePath控制mongod进程将其进程ID(PID)文件放置在何处。As this tracks the specific mongod file, it is crucial that file be unique and well labeled to make it easy to start and stop these processes.由于它跟踪特定的mongod文件,因此文件的唯一性和良好的标记至关重要,以便于启动和停止这些过程。

Create additional init scripts and/or adjust your existing MongoDB configuration and init script as needed to control these processes.根据需要创建额外的init脚本和/或调整现有的MongoDB配置和init脚本来控制这些进程。

[2] Single-tenant systems with SSD or other high performance disks may provide acceptable performance levels for multiple mongod instances. 具有SSD或其他高性能磁盘的单租户系统可以为多个mongod实例提供可接受的性能级别。Additionally, you may find that multiple databases with small working sets may function acceptably on a single system.此外,您可能会发现,具有小工作集的多个数据库可能在单个系统上正常工作。

Diagnostic Configurations诊断配置

The following configuration options control various mongod behaviors for diagnostic purposes:以下配置选项控制用于诊断目的的各种mongod行为:

  • operationProfiling.mode sets the database profiler level. operationProfiling.mode设置数据库探查器级别。The profiler is not active by default because of the possible impact on the profiler itself on performance. Unless this setting is on, queries are not profiled.探查器默认情况下未处于活动状态,因为探查器本身可能会对性能产生影响。除非启用此设置,否则不会分析查询。
  • operationProfiling.slowOpThresholdMs configures the threshold which determines whether a query is "slow" for the purpose of the logging system and the profiler. operationProfiling.slowOpThresholdMs配置阈值,该阈值用于确定出于日志记录系统和探查器的目的,查询是否“缓慢”。The default value is 100 milliseconds. Set to a lower value if the logging system and the database profiler do not return useful results or set to a higher value to only log the longest running queries.默认值为100毫秒。如果日志记录系统和数据库探查器未返回有用的结果,请设置为较低的值,或者设置为较高的值以仅记录运行时间最长的查询。
    Starting in version 4.2, secondary members of a replica set now log oplog entries that take longer than the slow operation threshold to apply. 从4.2版开始,副本集的辅助成员现在会记录应用时间超过慢速操作阈值的oplog条目These slow oplog messages:这些慢速操作日志消息:
    • Are logged for the secondaries in the diagnostic log.diagnostic log中为辅助设备记录。
    • Are logged under the REPL component with the text applied op: <oplog entry> took <num>ms.REPL组件下记录,并应用文本applied op: <oplog entry> took <num>ms
    • Do not depend on the log levels (either at the system or component level)不依赖于日志级别(在系统或组件级别)
    • Do not depend on the profiling level.不要依赖于分析级别。
    • May be affected by slowOpSampleRate, depending on your MongoDB version:可能会受到slowOpSampleRate的影响,具体取决于您的MongoDB版本:

      • In MongoDB 4.2, these slow oplog entries are not affected by the slowOpSampleRate. 在MongoDB 4.2中,这些慢速操作日志条目不受slowOpSampleRate的影响。MongoDB logs all slow oplog entries regardless of the sample rate.MongoDB记录所有慢速操作日志条目,而不管采样率如何。
      • In MongoDB 4.4 and later, these slow oplog entries are affected by the slowOpSampleRate.在MongoDB 4.4及更高版本中,这些慢速操作日志条目受到slowOpSampleRate的影响。

    The profiler does not capture slow oplog entries.探查器未捕获慢速操作日志项。

  • systemLog.verbosity controls the amount of logging output that mongod write to the log. 控制mongod写入日志的日志输出量。Only use this option if you are experiencing an issue that is not reflected in the normal logging level.只有在遇到未反映在正常日志记录级别中的问题时,才使用此选项。

    Starting in MongoDB 3.0, you can also specify verbosity level for specific components using the systemLog.component.<name>.verbosity setting. 从MongoDB 3.0开始,您还可以使用systemLog.component.<name>.verbosity设置。For the available components, see component verbosity settings.有关可用组件,请参阅组件详细程度设置

For more information, see also Database Profiling and MongoDB Performance.有关更多信息,请参阅数据库评测MongoDB性能