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如果配置文件包含__restexpansion, on Linux/macOS, the read access to the configuration file must be limited to the user running themongod/mongosprocess only.__rest扩展,在Linux/macOS上,对配置文件的读取权限必须仅限于运行mongod/mongos进程的用户。Expansion Parsing扩展分析
To parse the要解析__restblocks, start themongod/mongoswith the--configExpand "rest"option.__rest块,请使用--configExpand "rest"选项启动mongod/mongos。
Themongod/mongosissues aGETrequest against specified URL.mongod/mongos针对指定的URL发出GET请求。If successful, the如果成功,mongod/mongosreplaces the value ofcertificateKeyFilePasswordwith the returned value.mongod/mongos将用返回的值替换certificateKeyFilePassword的值。If the URL fails to resolve or if the如果URL解析失败或REST端点返回无效值,RESTendpoint returns an invalid value, themongod/mongosthrows an error and terminates.mongod/mongos将抛出错误并终止。
The following configuration file uses the以下配置文件使用__restexpansion directive to load the configuration file from an externalRESTendpoint.__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如果配置文件包含__restexpansion, on Linux/macOS, the read access to the configuration file must be limited to the user running themongod/mongosprocess only.__rest扩展,在Linux/macOS上,对配置文件的读取权限必须仅限于运行mongod/mongos进程的用户。Expansion Parsing扩展分析
To parse the要解析__restblocks, start themongod/mongoswith the--configExpand "rest"option.__rest块,请使用--configExpand "rest"选项启动mongod/mongos。
Themongod/mongosissues aGETrequest against the specified URL.mongod/mongos针对指定的URL发出GET请求。If successful, the如果成功,mongod/mongosparses the returned configurationyamlfile and uses it during startup.mongod/mongos将解析返回的配置yaml文件,并在启动期间使用它。If the URL fails to resolve or return a properly formatted如果URL未能解析或返回格式正确的yamlfile, themongod/mongosthrows an error and terminates.yaml文件,mongod/mongos将抛出错误并终止。Important重要
The value returned by the specified指定的RESTendpoint cannot include any additional expansion directives.REST端点返回的值不能包含任何其他扩展指令。Themongod/mongosdoes 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使用__execExpansion Directive__exec扩展指令
The__execexpansion directive loads configuration file values from a shell or terminal command.__exec扩展指令从shell或终端命令加载配置文件值。__execsupports loading specific values in the configuration file or loading the entire configuration file.__exec支持在配置文件中加载特定值或加载整个配置文件。
The following example configuration file uses the以下示例配置文件使用__execexpansion directive to to load the settingnet.tls.certificateKeyFilePasswordvalue 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如果配置文件包含__execexpansion, on Linux/macOS, the write access to the configuration file must be limited to the user running themongod/mongosprocess only.__exec扩展,则在Linux/macOS上,对配置文件的写入权限必须仅限于运行mongod/mongos进程的用户。Expansion Parsing扩展分析
To parse the要解析__execblocks, start themongod/mongoswith the--configExpand "exec"option.__exec块,请使用--configExpand "exec"选项启动mongod/mongos。
Themongod/mongosattempts to execute the specified operation.mongod/mongos尝试执行指定的操作。If the command executes successfully, the如果命令成功执行,mongod/mongosreplaces the value ofcertificateKeyFilePasswordwith the returned value.mongod/mongos将用返回的值替换certificateKeyFilePassword的值。If the command fails or returns an invalid value for the configuration file setting, the如果命令失败或返回配置文件设置的无效值,mongod/mongosthrows an error and terminates.mongod/mongos将抛出错误并终止。
The following example configuration file uses the以下示例配置文件使用__execexpansion directive to load the configuration file from the output of a shell or terminal command.__exec扩展指令从shell或终端命令的输出加载配置文件。The__execexpansion 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如果配置文件包含__execexpansion, on Linux/macOS, the write access to the configuration file must be limited to the user running themongod/mongosprocess only.__exec扩展,则在Linux/macOS上,对配置文件的写入权限必须仅限于运行mongod/mongos进程的用户。Expansion Parsing扩展分析
To parse the要解析__execblocks, start themongod/mongoswith the--configExpand "rest"option.__exec块,请使用--configExpand "rest"选项启动mongod/mongos。
If the command executes successfully, the如果命令成功执行,mongod/mongosparses the returned configurationyamlfile and uses it during startup.mongod/mongos将解析返回的配置yaml文件,并在启动期间使用它。If the command fails or returns an invalid如果命令失败或返回无效的yaml文件,yamlfile, themongod/mongosthrows an error and terminates.mongod/mongos将抛出错误并终止。Important重要
The data returned by executing the specified通过执行指定的__execstring cannot include any additional expansion directives.__exec字符串返回的数据不能包含任何其他扩展指令。Themongod/mongosdoes 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__restexpansion directive loads configuration file values from aRESTendpoint.__rest扩展指令从REST端点加载配置文件值。__restsupports loading specific values in the configuration file or loading the entire configuration file.支持在配置文件中加载特定值或加载整个配置文件。Themongod/mongosthen starts using the externally sourced values as part of its configuration.mongod/mongos然后开始使用外部来源的值作为其配置的一部分。
The__restexpansion directive has the following syntax:__rest扩展指令具有以下语法:
To specify a要为特定配置文件设置指定RESTendpoint 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要为整个配置文件指定RESTendpoint for the entire configuration file:REST端点,请执行以下操作:__rest: "<string>" type: "yaml" trim: "none|whitespace"
If specifying the entire configuration file via如果通过RESTendpoint, the expansion directive and its options must be the only values specified in the configuration file.REST端点指定整个配置文件,则扩展指令及其选项必须是配置文件中指定的唯一值。
__resttakes the following fields:采用以下字段:
Field字段Type类型Description描述__reststring Required
The URL against which themongod/mongosissues aGETrequest to retrieve the externally sourced value.mongod/mongos发出GET请求以检索外部来源值的URL。
For non-localhost对于非本地主机REST端点(例如远程服务器上托管的RESTendpoints (e.g. aRESTendpoint hosted on a remote server),__restrequires 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中。RESTendpoint specified in the URL requires authentication, encode credentials into the URL with the standard RFC 3986 User Information format.
For localhost对于本地主机RESTendpoints (e.g. aRESTendpoint listening on the host machine),__restallows unencrypted (http://) URLs.REST端点(例如,在主机上侦听的REST端点),__rest允许未加密的(http://)URL。Important重要
The value returned by the specified指定的RESTendpoint cannot include any additional expansion directives.REST端点返回的值不能包含任何其他扩展指令。Themongod/mongosdoes 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不会对返回的数据执行额外的处理,如果返回的数据包含额外的扩展指令,则会以错误代码终止。typestring
Optional.可选。Controls how控制__restparses the returned value from the specified URL.__rest如何解析来自指定URL的返回值。
Possible values are:可能的值包括:
string(Default)
Directs指示__restto parse the returned data as a literal string.__rest将返回的数据解析为文本字符串。If specifying如果指定字符串,则整个string, the entire__restblock and supporting options must be nested under the field for which you are loading externally sourced values.__rest块和支持选项必须嵌套在要为其加载外部源值的字段下。
yaml
Directs指示__restto parse the returned data as ayamlformatted file.__rest将返回的数据解析为yaml格式的文件。If specifying如果指定yaml, the__restblock must be the only content in the configuration file.yaml,__rest块必须是配置文件中的唯一内容。Themongod/mongosreplaces the configuration file contents with theyamlretrieved from the REST resource.mongod/mongos用从REST资源检索的yaml替换配置文件内容。trimstring Optional.可选。Specify指定whitespaceto direct__restto 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或“无修剪”。digeststring
Optional.可选。The SHA-256 digest of the expansion result.扩展结果的SHA-256摘要。
If specified, you must also specify the digest_key.如果指定,还必须指定digest_key。digest_keystring
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如果配置文件包含__restexpansion, on Linux/macOS, the read access to the configuration file must be limited to the user running themongod/mongosprocess only.__rest扩展,在Linux/macOS上,对配置文件的读取权限必须仅限于运行mongod/mongos进程的用户。To enable parsing of the要启用__restexpansion directive, start themongod/mongoswith the--configExpand "rest"option.__rest扩展指令的解析,请使用--configExpand "rest"选项启动mongod/mongos。
For examples, see Use the例如,请参阅使用__restExpansion Directive.__rest扩展指令。
__exec
The__execexpansion directive loads configuration file values from the output of a shell or terminal command.__exec扩展指令从shell或终端命令的输出中加载配置文件值。__execsupports loading specific values in the configuration file or loading the entire configuration file.支持在配置文件中加载特定值或加载整个配置文件。Themongod/mongosthen starts using the externally sourced values as part of its configuration.mongod/mongos然后开始使用外部来源的值作为其配置的一部分。
The__execexpansion 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命令指定整个配置文件,则扩展指令及其选项必须是配置文件中指定的唯一值。
__exectakes the following fields:采用以下字段:
Field字段Type类型Description描述__execstring
Required.必需。The string which themongod/mongosexecutes 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处理执行。__execopens a read-only pipe as the same user that started the与启动mongodormongos.mongod或mongos的同一用户打开只读管道。Important重要
The data returned by executing the specified command cannot include any additional expansion directives.执行指定命令返回的数据不能包含任何其他扩展指令。Themongod/mongosdoes 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不会对返回的数据执行额外的处理,如果返回的数据包含额外的扩展指令,则会以错误代码终止。typestring
Optional.可选。Controls how控制__execparses the value returned by the executed command.__exec如何解析执行的命令返回的值。
Possible values are:可能的值包括:
string(Default )
Directs指示__execto parse the returned data as a literal string.__exec将返回的数据解析为文本字符串。If specifying如果指定string, the entire__execblock and supporting options must be nested under the field for which you are loading externally sourced values.string,则整个__exec块和支持选项必须嵌套在要为其加载外部源值的字段下。
yaml
Directs指示__execto parse the returned data as ayamlformatted file.__exec将返回的数据解析为yaml格式的文件。If specifying如果指定yaml, the__execblock must be the only content in the configuration file.yaml,__exec块必须是配置文件中的唯一内容。Themongod/mongosreplaces the configuration file contents with theyamlretrieved from the executed command.mongod/mongos用从执行的命令中检索的yaml替换配置文件内容。trimstring Optional.可选。Specify指定whitespaceto direct__execto 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或“无修剪”。digeststring
Optional.可选。The SHA-256 digest of the expansion result.扩展结果的SHA-256摘要。
If specified, you must also specify the digest_key如果指定,还必须指定digest_keydigest_keystring
Optional.可选。The hexadecimal string representation of the secret used to calculate the SHA-256 digest.用于计算SHA-256摘要的秘密的十六进制字符串表示。Note注意
If the configuration file includes the如果配置文件包含__execexpansion, on Linux/macOS, the write access to the configuration file must be limited to the user running themongod/mongosprocess only.__exec扩展,则在Linux/macOS上,对配置文件的写入权限必须仅限于运行mongod/mongos进程的用户。To enable parsing of the要启用__execexpansion directives, start themongod/mongoswith the--configExpand "exec"option.__exec扩展指令的解析,请使用--configExpand "exec"选项启动mongod/mongos。
For examples, see Use the例如,请参阅使用__execExpansion 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/mongoswith the--outputConfigoption.mongod/mongos,可以测试指定一个或多个扩展指令的配置文件的最终输出。A以mongod/mongosstarted with--outputConfigoutputs the resolved YAML configuration document tostdoutand halts.--outputConfig开头的mongod/mongos将解析的YAML配置文档输出到stdout并停止。If any expansion directive specified in the configuration file returns additional expansion directives, the如果配置文件中指定的任何扩展指令返回其他扩展指令,mongod/mongosthrows an error and terminates.mongod/mongos将抛出错误并终止。Warning警告
The--outputConfigoption 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.confcontains a__restexpansion 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的字符串为2012820128
If the configuration file includes the如果配置文件包含__restexpansion, on Linux/macOS, the read access to the configuration file must be limited to the user running themongod/mongosprocess only.__rest扩展,在Linux/macOS上,对配置文件的读取权限必须仅限于运行mongod/mongos进程的用户。
Start the使用mongodwith the--configExpand "rest"and--outputConfigoptions:--configExpand "rest"和--outputConfig选项启动mongod:mongod -f mongod.conf --configExpand rest --outputConfig
Themongodoutputs the following tostdoutbefore 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