When you paste a connection string into the MongoDB Compass connection form, Compass shows credentials in plaintext by default. To hide your credentials and secure your connection string, use the 当您将连接字符串粘贴到MongoDB Compass连接表单中时,Compass默认以明文显示凭据。要隐藏凭据并保护连接字符串,请使用protectConnectionStrings option.protectConnectionStrings选项。
About This Task关于此任务
When 启用protectConnectionStrings is enabled, users cannot perform the following actions:protectConnectionStrings后,用户无法执行以下操作:
Edit the connection string in the Compass connection form.在Compass连接窗体中编辑连接字符串。Copy the connection string in the Compass interface.复制Compass界面中的连接字符串。See the credentials when exporting a query.导出查询时查看凭据。
Procedure过程
To secure your connection string, enable the 要保护连接字符串,请启用protectConnectionStrings option.protectConnectionStrings选项。
You can set the 您可以在以下任一位置设置protectConnectionStrings option in either:protectConnectionStrings选项:
The Compass Settings panelCompass“设置”面板The command line命令行A configuration file配置文件
Compass Settings PanelCompass设置面板
To open the MongoDB Compass Settings Panel, click the gear icon on the left column of the Compass home screen.要打开MongoDB Compass设置面板,请单击Compass主屏幕左列上的齿轮图标。

Alternatively, you can use keyboard shortcuts to open the Settings panel:或者,您可以使用键盘快捷键打开“设置”面板:
- Windows / Linux:
Ctrl+, - Mac:
⌘+,
Compass opens a dialog box where you can configure your MongoDB Compass settings.Compass打开一个对话框,您可以在其中配置MongoDB Compass设置。
Command Line Example命令行示例
The following command starts Compass from the command line and sets the 以下命令从命令行启动Compass并设置--protectConnectionStrings option:--protectConnectionStrings选项:
<path-to-Compass-executable> --protectConnectionStrings
Note
The name and filepath of the Compass executable depend on your operating system.Compass可执行文件的名称和文件路径取决于您的操作系统。
Configuration File Example配置文件示例
You can specify the Compass configuration file in either EJSON or YAML format. The following configurations set the 您可以以EJSON或YAML格式指定Compass配置文件。以下配置将protectConnectionStrings option to true:protectConnectionStrings选项设置为true:
EJSON
{ "protectConnectionStrings": true }YAML
protectConnectionStrings: trueLearn More了解更多
To learn more about the MongoDB Compass configuration file, see Configuration File Settings.要了解有关MongoDB Compass配置文件的更多信息,请参阅配置文件设置。