On this page本页内容
New in version 4.2.在版本4.2中新增。
MongoDB supports using expansion directives in configuration files to load externally sourced values. MongoDB支持在配置文件中使用扩展指令来加载外部来源的值。Expansion directives can load values for specific configuration file options or load the entire configuration file. 扩展指令可以加载特定配置文件选项的值,也可以加载整个配置文件。Expansion directives help obscure confidential information like security certificates and passwords.扩展指令有助于隐藏安全证书和密码等机密信息。
storage: dbPath: "/var/lib/mongo" systemLog: destination: file path: "/var/log/mongodb/mongod.log" net: bindIp: __exec: "python /home/user/getIPAddresses.py" type: "string" trim: "whitespace" digest: 85fed8997aac3f558e779625f2e51b4d142dff11184308dc6aca06cff26ee9ad digest_key: 68656c6c30303030307365637265746d796f6c64667269656e64 tls: mode: requireTLS certificateKeyFile: "/etc/tls/mongod.pem" certificateKeyFilePassword: __rest: "https://myrestserver.example.net/api/config/myCertKeyFilePassword" type: "string" digest: b08519162ba332985ac18204851949611ef73835ec99067b85723e10113f5c26 digest_key: 6d795365637265744b65795374756666
__rest
expansion, on Linux/macOS, the read access to the configuration file must be limited to the user running the mongod
/mongos
process only.__rest
扩展,在Linux/macOS上,对配置文件的读取权限必须仅限于运行mongod
/mongos
进程的用户。__exec
expansion, on Linux/macOS, the write access to the configuration file must be limited to the user running the mongod
/mongos
process only.__exec
扩展,则在Linux/macOS上,对配置文件的写入权限必须仅限于运行mongod
/mongos
进程的用户。To use expansion directives, you must specify the 要使用扩展指令,必须指定--configExpand
command-line option with the complete list of expansion directives used:--configExpand
命令行选项以及使用的扩展指令的完整列表:
mongod --config "/path/to/config/mongod.conf" --configExpand "rest,exec"
If you omit the 如果忽略--configExpand
option or if you do not specify the complete list of expansion directives used in the configuration file, the mongod
/mongos
returns an error and terminates. --configExpand
选项,或者没有指定配置文件中使用的扩展指令的完整列表,mongod
/mongos
将返回错误并终止。You can only specify the 只能在命令行上指定--configExpand
option on the command line.--configExpand
选项。
__rest
Expansion Directive__rest
扩展指令The __rest
expansion directive loads configuration file values from a REST
endpoint. __rest
扩展指令从REST
端点加载配置文件值。__rest
supports loading specific values in the configuration file or loading the entire configuration file.__rest
支持在配置文件中加载特定值或加载整个配置文件。
The following configuration file uses the 以下配置文件使用__rest
expansion directive to load the setting net.tls.certificateKeyFilePassword
value from an external REST
endpoint:__rest
扩展指令从外部REST
端点加载设置net.tls.certificateKeyFilePassword
值:
storage: dbPath: "/var/lib/mongo" systemLog: destination: file path: "/var/log/mongodb/mongod.log" net: bindIp: 192.51.100.24,127.0.0.1 tls: mode: requireTLS certificateKeyFile: "/etc/tls/mongod.pem"certificateKeyFilePassword: __rest: "https://myrestserver.example.net/api/config/myCertKeyFilePassword" type: "string"
File Permission文件权限If the configuration file includes the如果配置文件包含__rest
expansion, on Linux/macOS, the read access to the configuration file must be limited to the user running themongod
/mongos
process only.__rest
扩展,在Linux/macOS上,对配置文件的读取权限必须仅限于运行mongod
/mongos
进程的用户。Expansion Parsing扩展分析
To parse the要解析__rest
blocks, start themongod
/mongos
with the--configExpand "rest"
option.__rest
块,请使用--configExpand "rest"
选项启动mongod
/mongos
。
Themongod
/mongos
issues aGET
request against specified URL.mongod
/mongos
针对指定的URL发出GET请求。If successful, the如果成功,mongod
/mongos
replaces the value ofcertificateKeyFilePassword
with the returned value.mongod
/mongos
将用返回的值替换certificateKeyFilePassword
的值。If the URL fails to resolve or if the如果URL解析失败或REST端点返回无效值,REST
endpoint returns an invalid value, themongod
/mongos
throws an error and terminates.mongod
/mongos
将抛出错误并终止。
The following configuration file uses the以下配置文件使用__rest
expansion directive to load the configuration file from an externalREST
endpoint.__rest
扩展指令从外部REST
端点加载配置文件。The expansion directive and its options must be the only values specified in the configuration file.扩展指令及其选项必须是配置文件中指定的唯一值。__rest: "https://myrestserver.example.net/api/config/fullConfig" type: "yaml"
File Permission文件权限If the configuration file includes the如果配置文件包含__rest
expansion, on Linux/macOS, the read access to the configuration file must be limited to the user running themongod
/mongos
process only.__rest
扩展,在Linux/macOS上,对配置文件的读取权限必须仅限于运行mongod
/mongos
进程的用户。Expansion Parsing扩展分析
To parse the要解析__rest
blocks, start themongod
/mongos
with the--configExpand "rest"
option.__rest
块,请使用--configExpand "rest"
选项启动mongod
/mongos
。
Themongod
/mongos
issues aGET
request against the specified URL.mongod
/mongos
针对指定的URL发出GET请求。If successful, the如果成功,mongod
/mongos
parses the returned configurationyaml
file and uses it during startup.mongod
/mongos
将解析返回的配置yaml
文件,并在启动期间使用它。If the URL fails to resolve or return a properly formatted如果URL未能解析或返回格式正确的yaml
file, themongod
/mongos
throws an error and terminates.yaml
文件,mongod
/mongos
将抛出错误并终止。Important重要
The value returned by the specified指定的REST
endpoint cannot include any additional expansion directives.REST
端点返回的值不能包含任何其他扩展指令。Themongod
/mongos
does not perform additional processing on the returned data and will terminate with an error code if the returned data includes additional expansion directives.mongod
/mongos
不会对返回的数据执行额外的处理,如果返回的数据包含额外的扩展指令,则会以错误代码终止。
Use the使用__exec
Expansion Directive__exec
扩展指令
The__exec
expansion directive loads configuration file values from a shell or terminal command.__exec
扩展指令从shell或终端命令加载配置文件值。__exec
supports loading specific values in the configuration file or loading the entire configuration file.__exec
支持在配置文件中加载特定值或加载整个配置文件。
The following example configuration file uses the以下示例配置文件使用__exec
expansion directive to to load the settingnet.tls.certificateKeyFilePassword
value from the output of a shell or terminal command:__exec
扩展指令从shell或终端命令的输出中加载设置net.tls.certificateKeyFilePassword
值:storage: dbPath: "/var/lib/mongo" systemLog: destination: file path: "/var/log/mongodb/mongod.log" net: bindIp: 192.51.100.24,127.0.0.1 tls: mode: requireTLS certificateKeyFile: "/etc/tls/mongod.pem"certificateKeyFilePassword: __exec: "python /home/myUserName/getPEMPassword.py" type: "string"
File Permission文件权限If the configuration file includes the如果配置文件包含__exec
expansion, on Linux/macOS, the write access to the configuration file must be limited to the user running themongod
/mongos
process only.__exec
扩展,则在Linux/macOS上,对配置文件的写入权限必须仅限于运行mongod
/mongos
进程的用户。Expansion Parsing扩展分析
To parse the要解析__exec
blocks, start themongod
/mongos
with the--configExpand "exec"
option.__exec
块,请使用--configExpand "exec"
选项启动mongod
/mongos
。
Themongod
/mongos
attempts to execute the specified operation.mongod
/mongos
尝试执行指定的操作。If the command executes successfully, the如果命令成功执行,mongod
/mongos
replaces the value ofcertificateKeyFilePassword
with the returned value.mongod
/mongos
将用返回的值替换certificateKeyFilePassword
的值。If the command fails or returns an invalid value for the configuration file setting, the如果命令失败或返回配置文件设置的无效值,mongod
/mongos
throws an error and terminates.mongod
/mongos
将抛出错误并终止。
The following example configuration file uses the以下示例配置文件使用__exec
expansion directive to load the configuration file from the output of a shell or terminal command.__exec
扩展指令从shell或终端命令的输出加载配置文件。The__exec
expansion directive and its options must be the only values specified in the configuration file.__exec
扩展指令及其选项必须是配置文件中指定的唯一值。__exec: "python /home/myUserName/getFullConfig.py" type: "yaml"
File Permission文件权限If the configuration file includes the如果配置文件包含__exec
expansion, on Linux/macOS, the write access to the configuration file must be limited to the user running themongod
/mongos
process only.__exec
扩展,则在Linux/macOS上,对配置文件的写入权限必须仅限于运行mongod
/mongos
进程的用户。Expansion Parsing扩展分析
To parse the要解析__exec
blocks, start themongod
/mongos
with the--configExpand "rest"
option.__exec
块,请使用--configExpand "rest"
选项启动mongod
/mongos
。
If the command executes successfully, the如果命令成功执行,mongod
/mongos
parses the returned configurationyaml
file and uses it during startup.mongod
/mongos
将解析返回的配置yaml文件,并在启动期间使用它。If the command fails or returns an invalid如果命令失败或返回无效的yaml文件,yaml
file, themongod
/mongos
throws an error and terminates.mongod
/mongos
将抛出错误并终止。Important重要
The data returned by executing the specified通过执行指定的__exec
string cannot include any additional expansion directives.__exec
字符串返回的数据不能包含任何其他扩展指令。Themongod
/mongos
does not perform additional processing on the returned data and will terminate with an error code if the returned data includes additional expansion directives.mongod
/mongos
不会对返回的数据执行额外的处理,如果返回的数据包含额外的扩展指令,则会以错误代码终止。
Expansion Directives Reference扩展指令参考
__rest
The__rest
expansion directive loads configuration file values from aREST
endpoint.__rest
扩展指令从REST
端点加载配置文件值。__rest
supports loading specific values in the configuration file or loading the entire configuration file.支持在配置文件中加载特定值或加载整个配置文件。Themongod
/mongos
then starts using the externally sourced values as part of its configuration.mongod
/mongos
然后开始使用外部来源的值作为其配置的一部分。
The__rest
expansion directive has the following syntax:__rest
扩展指令具有以下语法:
To specify a要为特定配置文件设置指定REST
endpoint for a specific configuration file setting or settings:REST
端点,请执行以下操作:<some configuration file setting>: __rest: "<string>" type: "string" trim: "none|whitespace" digest: "<string>" digest_key: "<string>"
To specify a要为整个配置文件指定REST
endpoint for the entire configuration file:REST
端点,请执行以下操作:__rest: "<string>" type: "yaml" trim: "none|whitespace"
If specifying the entire configuration file via如果通过REST
endpoint, the expansion directive and its options must be the only values specified in the configuration file.REST
端点指定整个配置文件,则扩展指令及其选项必须是配置文件中指定的唯一值。
__rest
takes the following fields:采用以下字段:
Field字段Type类型Description描述__rest
string Required
The URL against which themongod
/mongos
issues aGET
request to retrieve the externally sourced value.mongod
/mongos
发出GET请求以检索外部来源值的URL。
For non-localhost对于非本地主机REST端点(例如远程服务器上托管的REST
endpoints (e.g. aREST
endpoint hosted on a remote server),__rest
requires encrypted (https://
) URLs where both the host machine and the remote server support TLS 1.1 or later.REST
端点),__rest
需要加密(https://
)URL,其中主机和远程服务器都支持TLS 1.1或更高版本。
If the如果URL中指定的REST端点需要身份验证,请使用标准RFC 3986用户信息格式将凭据编码到URL中。REST
endpoint specified in the URL requires authentication, encode credentials into the URL with the standard RFC 3986 User Information format.
For localhost对于本地主机REST
endpoints (e.g. aREST
endpoint listening on the host machine),__rest
allows unencrypted (http://
) URLs.REST
端点(例如,在主机上侦听的REST端点),__rest
允许未加密的(http://)URL。Important重要
The value returned by the specified指定的REST
endpoint cannot include any additional expansion directives.REST
端点返回的值不能包含任何其他扩展指令。Themongod
/mongos
does not perform additional processing on the returned data and will terminate with an error code if the returned data includes additional expansion directives.mongod
/mongos
不会对返回的数据执行额外的处理,如果返回的数据包含额外的扩展指令,则会以错误代码终止。type
string
Optional.可选。Controls how控制__rest
parses the returned value from the specified URL.__rest
如何解析来自指定URL的返回值。
Possible values are:可能的值包括:
string
(Default)
Directs指示__rest
to parse the returned data as a literal string.__rest
将返回的数据解析为文本字符串。If specifying如果指定字符串,则整个string
, the entire__rest
block and supporting options must be nested under the field for which you are loading externally sourced values.__rest
块和支持选项必须嵌套在要为其加载外部源值的字段下。
yaml
Directs指示__rest
to parse the returned data as ayaml
formatted file.__rest
将返回的数据解析为yaml
格式的文件。If specifying如果指定yaml
, the__rest
block must be the only content in the configuration file.yaml
,__rest
块必须是配置文件中的唯一内容。Themongod
/mongos
replaces the configuration file contents with theyaml
retrieved from the REST resource.mongod
/mongos
用从REST
资源检索的yaml
替换配置文件内容。trim
string Optional.可选。Specify指定whitespace
to direct__rest
to trim any leading or trailing whitespace, specifically occurrences of" "
,"\r"
,"\n"
,"\t"
,"\v"
, and"\f"
.whitespace
以指示__rest
修剪任何前导或尾随空白,特别是出现" "
、"\r"
、"\n"
、"\t"
、"\v"
和"\f"
。Defaults to默认为none
, or no trimming.none
或“无修剪”。digest
string
Optional.可选。The SHA-256 digest of the expansion result.扩展结果的SHA-256摘要。
If specified, you must also specify the digest_key.如果指定,还必须指定digest_key。digest_key
string
Optional.可选。The hexadecimal string representation of the secret used to calculate the SHA-256 digest.用于计算SHA-256摘要的秘密的十六进制字符串表示。
If specified, you must also specify the digest.如果指定,还必须指定摘要。Note注意
If the configuration file includes the如果配置文件包含__rest
expansion, on Linux/macOS, the read access to the configuration file must be limited to the user running themongod
/mongos
process only.__rest
扩展,在Linux/macOS上,对配置文件的读取权限必须仅限于运行mongod
/mongos
进程的用户。To enable parsing of the要启用__rest
expansion directive, start themongod
/mongos
with the--configExpand "rest"
option.__rest
扩展指令的解析,请使用--configExpand "rest"
选项启动mongod
/mongos
。
For examples, see Use the例如,请参阅使用__rest
Expansion Directive.__rest
扩展指令。
__exec
The__exec
expansion directive loads configuration file values from the output of a shell or terminal command.__exec
扩展指令从shell或终端命令的输出中加载配置文件值。__exec
supports loading specific values in the configuration file or loading the entire configuration file.支持在配置文件中加载特定值或加载整个配置文件。Themongod
/mongos
then starts using the externally sourced values as part of its configuration.mongod
/mongos
然后开始使用外部来源的值作为其配置的一部分。
The__exec
expansion directive has the following syntax:__exec
扩展指令具有以下语法:
To specify a shell or terminal command for a specific configuration file setting or settings:要为特定配置文件设置指定shell或终端命令,请执行以下操作:<some configuration file setting>: __exec: "<string>" type: "string" trim: "none|whitespace"
To specify a a shell or terminal command for the entire configuration file:要为整个配置文件指定shell或终端命令,请执行以下操作:__exec: "<string>" type: "yaml" trim: "none|whitespace"
If specifying the entire configuration file via a terminal or shell command, the expansion directive and its options must be the only values specified in the configuration file.如果通过终端或shell命令指定整个配置文件,则扩展指令及其选项必须是配置文件中指定的唯一值。
__exec
takes the following fields:采用以下字段:
Field字段Type类型Description描述__exec
string
Required.必需。The string which themongod
/mongos
executes on the terminal or shell to retrieve the externally sourced value.mongod
/mongos
在终端或shell上执行以检索外部源值的字符串。
On Linux and OSX hosts, execution is handled via POSIX在Linux和OSX主机上,通过POSIXpopen()
.popen()
处理执行。On Windows hosts, execution is handled via the process control API.在Windows主机上,通过进程控制API处理执行。__exec
opens a read-only pipe as the same user that started the与启动mongod
ormongos
.mongod
或mongos
的同一用户打开只读管道。Important重要
The data returned by executing the specified command cannot include any additional expansion directives.执行指定命令返回的数据不能包含任何其他扩展指令。Themongod
/mongos
does not perform additional processing on the returned data and will terminate with an error code if the returned data includes additional expansion directives.mongod
/mongos
不会对返回的数据执行额外的处理,如果返回的数据包含额外的扩展指令,则会以错误代码终止。type
string
Optional.可选。Controls how控制__exec
parses the value returned by the executed command.__exec
如何解析执行的命令返回的值。
Possible values are:可能的值包括:
string
(Default )
Directs指示__exec
to parse the returned data as a literal string.__exec
将返回的数据解析为文本字符串。If specifying如果指定string
, the entire__exec
block and supporting options must be nested under the field for which you are loading externally sourced values.string
,则整个__exec
块和支持选项必须嵌套在要为其加载外部源值的字段下。
yaml
Directs指示__exec
to parse the returned data as ayaml
formatted file.__exec
将返回的数据解析为yaml
格式的文件。If specifying如果指定yaml
, the__exec
block must be the only content in the configuration file.yaml
,__exec
块必须是配置文件中的唯一内容。Themongod
/mongos
replaces the configuration file contents with theyaml
retrieved from the executed command.mongod
/mongos
用从执行的命令中检索的yaml
替换配置文件内容。trim
string Optional.可选。Specify指定whitespace
to direct__exec
to trim any leading or trailing whitespace, specifically occurrences of" "
,"\r"
,"\n"
,"\t"
,"\v"
, and"\f"
.whitespace
以指示__exec
修剪任何前导或尾随空白,特别是出现" "
、"\r"
、"\n"
、"\t"
、"\v"
和"\f"
。Defaults to指定none
, or no trimming.whitespace
以指示__exec
修剪任何前导或尾随空白,特别是出现" "
、"\r"
、"\n"
、"\t"
、"\v"
和"\f"
。默认为none
或“无修剪”。digest
string
Optional.可选。The SHA-256 digest of the expansion result.扩展结果的SHA-256摘要。
If specified, you must also specify the digest_key如果指定,还必须指定digest_keydigest_key
string
Optional.可选。The hexadecimal string representation of the secret used to calculate the SHA-256 digest.用于计算SHA-256摘要的秘密的十六进制字符串表示。Note注意
If the configuration file includes the如果配置文件包含__exec
expansion, on Linux/macOS, the write access to the configuration file must be limited to the user running themongod
/mongos
process only.__exec
扩展,则在Linux/macOS上,对配置文件的写入权限必须仅限于运行mongod
/mongos
进程的用户。To enable parsing of the要启用__exec
expansion directives, start themongod
/mongos
with the--configExpand "exec"
option.__exec
扩展指令的解析,请使用--configExpand "exec"
选项启动mongod
/mongos
。
For examples, see Use the例如,请参阅使用__exec
Expansion Directive.__exec
扩展指令。
Output the Configuration File with Resolved Expansion Directive Values使用解析的扩展指令值输出配置文件
You can test the final output of a configuration file that specifies one or more expansion directives by starting the通过使用--outputConfig选项启动mongod
/mongos
with the--outputConfig
option.mongod
/mongos
,可以测试指定一个或多个扩展指令的配置文件的最终输出。A以mongod
/mongos
started with--outputConfig
outputs the resolved YAML configuration document tostdout
and halts.--outputConfig
开头的mongod
/mongos
将解析的YAML配置文档输出到stdout
并停止。If any expansion directive specified in the configuration file returns additional expansion directives, the如果配置文件中指定的任何扩展指令返回其他扩展指令,mongod
/mongos
throws an error and terminates.mongod
/mongos
将抛出错误并终止。Warning警告
The--outputConfig
option returns the resolved values for any field using an expansion directive.--outputConfig
选项使用扩展指令返回任何字段的解析值。This includes any private or sensitive information previously obscured by using an external source for the configuration option.这包括以前使用外部源配置选项而隐藏的任何私人或敏感信息。
For example, the following configuration file例如,以下配置文件mongod.conf
contains a__rest
expansion directive:mongod.conf
包含__rest
扩展指令:storage: dbPath: "/var/lib/mongo" systemLog: destination: file path: "/var/log/mongodb/mongod.log" net: port: __rest: "https://mongoconf.example.net:8080/record/1" type: string
The string recorded at the specified URL is记录在指定URL的字符串为20128
20128
If the configuration file includes the如果配置文件包含__rest
expansion, on Linux/macOS, the read access to the configuration file must be limited to the user running themongod
/mongos
process only.__rest
扩展,在Linux/macOS上,对配置文件的读取权限必须仅限于运行mongod
/mongos
进程的用户。
Start the使用mongod
with the--configExpand "rest"
and--outputConfig
options:--configExpand "rest"
和--outputConfig
选项启动mongod
:mongod -f mongod.conf --configExpand rest --outputConfig
Themongod
outputs the following tostdout
before terminating:mongod
在终止前向stdout
输出以下内容:config: mongod.conf storage: dbPath: "/var/lib/mongo" systemLog: destination: file path: "/var/log/mongodb/mongod.log" net: port: 20128 outputConfig: true