对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

rotateCertificates(数据库命令)

版本 5.0 中的新增功能。

rotateCertificates

Rotates the currently used TLS certificates for a mongod or mongos to use the updated values for these certificates defined in the configuration file.

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

注意

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

注意

Atlas会自动轮换证书。使用Atlas时,无需运行rotateCertificates 命令。仅当您想手动轮换证书时才使用 rotateCertificates 命令。

该命令具有以下语法:

db.runCommand(
{
rotateCertificates: 1,
message: "<optional log message>"
}
)

该命令采用以下可选字段:

Parameter
类型
说明

message

字符串

可选服务器记录到日志文件和审核文件中的消息。

The rotateCertificates command returns a document with the following field:

字段
类型
说明

ok

bool

包含命令的执行状态。 成功则为true ,如果发生错误则为false 。 如果为false ,则另外向errmsg字段提供详细的错误消息。

轮换包括以下证书:

轮换一个或多个证书:

  1. 替换您要在文件系统中轮换的证书,并注意以下限制:

    • 每个新证书的文件名和文件路径必须与被替换的证书相同。

    • 如果轮换加密的 TLS Certificate,其密码必须与旧证书的密码相同(如 certificateKeyFilePassword 配置文件设置所指定)。证书轮换不支持交互式密码提示。

  2. Connect mongosh to the mongod or mongos instance that you wish to perform certificate rotation on.

  3. Run the rotateCertificates command to rotate the certificates used by the mongod or mongos instance.

当证书轮换发生时:

  • 与 mongod} 或 mongos 实例的现有连接不会终止,并将继续使用旧证书。

  • 所有新连接都将使用新证书。

If you have configured OCSP for your deployment, the rotateCertificates command will also fetch stapled OCSP responses during rotation.

The rotateCertificates command may be run on a running mongod or mongos regardless of replication status.

Only one instance of db.rotateCertificates() or rotateCertificates may run on each mongod or mongos process at a time. Attempting to initiate a second instance while one is already running will result in an error.

证书文件错误、过期、撤销或丢失会导致证书轮换失败,但不会导致现有 TLS 配置失效或终止正在运行的 mongod 或 mongos 进程。

If the mongod or mongos is running with --tlsCertificateSelector set to thumbprint, rotateCertificates will fail and write a warning message to the log file.

成功轮换后,主题名称、指纹以及服务器和集群证书指纹的有效期都会记录到配置的日志目标中。 如果已启用审核,此信息也会写入审核日志。

在 Linux 和 Windows 平台上,如果存在CRL file ,其指纹和有效期也会记录到这些位置。

You must have the rotateCertificates action in order to use the rotateCertificates command. The rotateCertificates action is part of the hostManager role.

对配置文件进行适当更新以指定更新的证书信息后,以下操作将在运行中的mongod实例上轮换证书:

db.adminCommand( { rotateCertificates: 1 } )

以下操作的执行与上述操作相同,但还会在轮换时将自定义日志消息写入日志文件和审核文件:

db.adminCommand( { rotateCertificates: 1, message: "Rotating certificates" } )