This page outlines how to use MongoDB Compass to connect to a MongoDB host. You can connect to a standalone, replica set, or sharded cluster host.本页概述了如何使用MongoDB Compass连接到MongoDB主机。您可以连接到独立、副本集或分片群集主机。
Note
MongoDB Atlas
If you need to create a MongoDB host, consider using MongoDB Atlas. Atlas is a cloud-hosted database-as-a-service which requires no installation, offers a free tier to get started, and provides a copyable URI to easily connect Compass to your deployment.如果你需要创建一个MongoDB主机,可以考虑使用MongoDB Atlas。Atlas是一种云托管的数据库即服务,无需安装,提供免费的入门层,并提供可复制的URI,可轻松将Compass连接到部署。
If you need to install Compass, see Download and Install Compass for instructions.如果您需要安装Compass,请参阅下载和安装Compass以获取说明。
Considerations注意事项
When connecting Compass to a replica set, it is not recommended to connect directly to an individual replica set member. If the member to which you are connected switches from a primary member to a secondary or vice versa as the result of an election, Compass may either forcibly close the connection or display stale data.将Compass连接到副本集时,不建议直接连接到单个副本集成员。如果您连接的成员因选举而从primary成员切换到secondary成员,反之亦然,Compass可能会强制关闭连接或显示过时的数据。Instead, to connect to a replica set, use either the replica set SRV record or Replica Set Name when filling in your connection information.相反,要连接到副本集,请在填写连接信息时使用副本集SRV记录或副本集名称。Starting in MongoDB Compass 1.19, Compass displays a warning message when connected to non-genuine MongoDB instances as these instances may behave differently from the official MongoDB instances; e.g. missing or incomplete features, different feature behaviors, etc.从MongoDB Compass 1.19开始,Compass在连接到非正版MongoDB实例时显示警告消息,因为这些实例的行为可能与官方MongoDB实例不同;例如,缺失或不完整的特征、不同的特征行为等。Starting in version 1.39.2, MongoDB Compass no longer supports migrating from legacy connection files that pre-date version 1.31.0. Legacy connections refer to an internal Compass connection-options format that is stored on disk and no longer supported after version 1.39.0.从版本1.39.2开始,MongoDB Compass不再支持从版本1.31.0之前的旧连接文件迁移。传统连接是指存储在磁盘上的内部Compass连接选项格式,在版本1.39.0之后不再受支持。If you have legacy connections saved in your favorites, export the connections on version 1.39.0 to convert them into the new format before updating to version 1.39.2 or later.如果收藏夹中保存了旧连接,请在更新到1.39.2或更高版本之前,导出1.39.0版本的连接以将其转换为新格式。
Connect连接
If your connection name is already in the Connections Sidebar of your Compass window, click on the connection name to connect.如果连接名称已在Compass窗口的“连接”侧边栏中,请单击连接名称进行连接。
Otherwise, you can connect your deployments by either:否则,您可以通过以下任一方式连接部署:
Providing your connection string.提供连接字符串。Specifying Advanced Connection Options. Advanced connection options allow you to specify authentication, TLS/SSL, and SSH connection options.指定“高级连接选项”。高级连接选项允许您指定身份验证、TLS/SSL和SSH连接选项。To learn more, see Advanced Connection Options.要了解更多信息,请参阅高级连接选项。
Note
Starting in version 1.44.0, you can connect to multiple MongoDB instances at once through Compass.从1.44.0版本开始,您可以通过Compass一次连接到多个MongoDB实例。
Provide your Connection String提供连接字符串
When you provide a connection string, Compass supports most Connection String Options supported by MongoDB.当您提供连接字符串时,Compass支持MongoDB支持的大多数连接字符串选项。
By default, Compass's default socketTimeoutMS value is 60000, or 60 seconds. If you are frequently experiencing timeouts in Compass, consider setting this option to a higher value in your connection string.默认情况下,Compass的默认socketTimeoutMS值为60000,即60秒。如果您经常在Compass中遇到超时,请考虑在连接字符串中将此选项设置为更高的值。
For a complete list of the connection string options that Compass supports, see the Compass Connection README on GitHub.有关Compass支持的连接字符串选项的完整列表,请参阅GitHub上的Compass connection README。
Open the New Connection modal.打开“新建连接”模式。
In the bottom panel of the Connections Sidebar, click Add New Connection to open the New Connection modal.在“连接”侧边栏的底部面板中,单击“添加新连接”以打开“新建连接”模式。
If you already have connections listed in the Connections Sidebar, click the icon on the top right of the sidebar to open the New Connection modal.如果您已经在“连接”侧边栏中列出了连接,请单击侧边栏右上角的图标以打开新连接模式。
Paste your connection string.粘贴连接字符串。
If you have the connection string for your deployment available, you can paste the string directly into the dialog box. 如果部署的连接字符串可用,则可以将字符串直接粘贴到对话框中。You can use either the Standard Connection String Format or the DNS Seedlist Connection Format.您可以使用标准连接字符串格式或DNS种子列表连接格式。
To obtain the connection string for an Atlas cluster:要获取Atlas集群的连接字符串,请执行以下操作:
Navigate to your Atlas Clusters view.导航到Atlas“群集”视图。Click Connect for your desired cluster.单击所需群集的“连接”。Click Connect with MongoDB Compass.单击“使用MongoDB Compass连接”。Copy the provided connection string.复制提供的连接字符串。
Warning
If you click into the MongoDB Compass connection form to edit your connection string, Compass shows credentials in plaintext by default.如果你点击MongoDB Compass连接表单来编辑你的连接字符串,Compass默认会以纯文本显示凭据。
To edit your connection string credentials without exposing your password, use the Advanced Connection Options > Authentication tab on the Compass connection form.要编辑连接字符串凭据而不公开密码,请使用Compass连接窗体上的“高级连接选项”>“身份验证”选项卡。
To learn how to format the connection string for a deployment that is not hosted on Atlas, see Connection String URI Format.要了解如何为不在Atlas上托管的部署格式化连接字符串,请参阅连接字符串URI格式。
(Optional). Name your connection.(可选)。命名连接。
Use the name field to enter a name for your connection. If you do not specify a name, Compass uses the cluster's hostname as the connection name.使用名称字段为连接输入名称。如果不指定名称,Compass将使用集群的主机名作为连接名称。
(Optional). Choose a color for your connection.(可选)。为连接选择一种颜色。
Use the color drop-down menu to select a label color for your connection. When you connect to a connection, the label color is the background color of tabs that reference your connection.使用颜色下拉菜单为连接选择标签颜色。当您连接到连接时,标签颜色是引用连接的选项卡的背景颜色。
(Optional). Favorite your connection.(可选)。喜欢你的连接。
If you want to save the connection as a favorite, check the Favorite this connection option in the modal.如果要将连接另存为收藏夹,请在模式中选中“收藏此连接”选项。
Connect to your cluster.连接到集群。
To navigate to the Compass Home Page, click Save, Connect, or Save & Connect.要导航到Compass主页,请单击“保存”、“连接”或“保存并连接”。
The Save button saves your connection and closes the modal without yet connecting to your cluster.“保存”按钮保存连接并关闭模式,而无需连接到集群。The Connect button allows you to connect to your cluster without saving your connection string or credentials.“连接”按钮允许您连接到群集,而无需保存连接字符串或凭据。The default Save & Connect button both saves your information and connects you to your cluster.默认的“保存和连接”按钮既可以保存信息,也可以将您连接到集群。
Important
Required Access所需访问权限
Once you are connected to your MongoDB deployment, you may require specific user roles to access various Compass features. For more information on the required roles for Compass features, see Required Access.一旦连接到MongoDB部署,您可能需要特定的用户角色来访问各种Compass功能。有关Compass功能所需角色的更多信息,请参阅所需访问权限。
Use Advanced Connection Options使用高级连接选项
The advanced connection options provide additional ways to connect Compass to MongoDB. Advanced connection options allow you to specify authentication, TLS/SSL, and SSH to securely connect to your deployment.高级连接选项提供了将Compass连接到MongoDB的其他方法。高级连接选项允许您指定身份验证、TLS/SSL和SSH,以安全地连接到部署。
For details on how to specify connection options using Compass, see Advanced Connection Options.有关如何使用Compass指定连接选项的详细信息,请参阅高级连接选项。
Connect to Compass from the Command Line从命令行连接到Compass
You can start a Compass session from the command line.您可以从命令行启动Compass会话。
In enterprise environments, a scripted start can make it easier to deploy Compass. For example, to limit access to sensitive systems, you can configure a command line start so that Compass can run on a jump host.在企业环境中,脚本启动可以更容易地部署Compass。例如,为了限制对敏感系统的访问,您可以配置命令行启动,以便Compass可以在跳转主机上运行。
To connect to Compass using the command line, you can include your connection options directly in your connection string or as command line arguments.要使用命令行连接到Compass,可以将连接选项直接包含在连接字符串中或作为命令行参数。
To specify connection options in your connection string, use the following format:要在连接字符串中指定连接选项,请使用以下格式:
mongodb-compass 'mongodb://username:password@hostname:port/database?ssl=true&replicaSet=myReplicaSet'
For more connection string options, see Connection String Options.有关更多连接字符串选项,请参阅连接字符串选项。
To specify connection options as command line arguments, use the following format:要将连接选项指定为命令行参数,请使用以下格式:
<path/to/compass/executable> <connection string> --username <username> --password <password>
For more command line argument connection options, see Start Compass from the Command Line.有关更多命令行参数连接选项,请参阅从命令行启动Compass。
Tip
To learn how to disconnect your deployment, see Disconnect from MongoDB要了解如何断开部署,请参阅断开与MongoDB的连接
Connection String Examples连接字符串示例
The following examples highlight some advanced connection string options. For information on all connection string options, see Connection String Options.以下示例突出显示了一些高级连接字符串选项。有关所有连接字符串选项的信息,请参阅连接字符串选项。
TLS Options选项
The following connection string enables TLS using the 以下连接字符串使用tls URI option. tlsCertificateKeyFile specifies the path to the client certificate and private key. tls URI选项启用TLS。tlsCertificateKeyFile指定客户端证书和私钥的路径。Specifying the 在连接字符串中指定tlsCertificateKeyFile in your connection string is optional, unless the server requires client certificates, or if you are using X.509 authentication.tlsCertificateKeyFile是可选的,除非服务器需要客户端证书,或者您正在使用X.509身份验证。
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db0.example.com/?directConnection=true&tls=true&tlsCAFile=path/to/rootCA.crt&tlsCertificateKeyFile=path/to/server_certificate.pem
To enable SSL, use the equivalent 要启用SSL,请使用等效的ssl=true option in place of tls=true.ssl=true选项代替tls=true。
X.509 AuthenticationX.509身份验证
The following example demonstrates how to specify X.509 authentication in your connection string using the 以下示例演示了如何使用authMechanism connection string option:authMechanism连接字符串选项在连接字符串中指定X.509身份验证:
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.com/?authMechanism=MONGODB-X509&tls=true&tlsCertificateKeyFile=/path/to/client_certificate.pemLDAP
The following example specifies LDAP authentication. 以下示例指定LDAP身份验证。It sets 它将authMechanism to plain to specify the use of the LDAP authentication mechanism and sets authSource to $external to indicate that the authentication database is $external.authMechanism设置为plain以指定LDAP身份验证机制的使用,并将authSource设置为$external以指示身份验证数据库为$externals。
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db2.example.com/?authMechanism=PLAIN&authSource=$externalKerberos
The following example specifies Kerberos authentication. It sets 以下示例指定Kerberos身份验证。它将authMechanism to GSSAPI to use the Kerberos authentication mechanism and sets authSource to $external to indicate that the authentication database is $external.authMechanism设置为GSSAPI以使用Kerberos身份验证机制,并将authSource设置为$external以指示身份验证数据库是$external。
mongodb://myDatabaseUser@db3.example.com/?authMechanism=GSSAPI&authSource=$externalRead/Write Preferences读/写首选项
To enable read/write preferences in your connection string, you can use the 要在连接字符串中启用读/写首选项,您可以使用readPreference option for read preferences and the w option for write concerns.readPreference选项作为读首选项,使用w选项作为写关注。
The following connection string sets both read and write preferences. 以下连接字符串设置了读取和写入首选项。readPreference=secondary specifies that read operations should be directed to secondary members of the replica set and w=majority ensures that write operations are acknowledged by the majority of the replica set members.readPreference=secondary指定读取操作应定向到副本集的次要成员,w=majority确保写入操作得到大多数副本集成员的确认。
mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db4.example.com/database?readPreference=secondary&w=majorityAnalytic Nodes分析节点
To specify an 要在连接字符串中指定analytics node type in your connection string:analytics节点类型,请执行以下操作:
Set your将readPreferencetosecondaryto specify that the read operations should be directed to secondary nodes.readPreference设置为secondary,以指定读取操作应定向到次要节点。Set将readPreferenceTagstonodeType:ANALYTICSto ensure that the read operations are directed specifically to the analytic nodes.readPreferenceTags设置为nodeType:ANALYTICS,以确保读取操作专门针对分析节点。
The following connection string demonstrates how to specify an 以下连接字符串演示了如何在连接字符串中指定analytics node type in your connection string:analytics节点类型:
mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@db5.example.com/myDatabase?readPreference=secondary&readPreferenceTags=nodeType:ANALYTICS
For more information on using analytics nodes to isolate workloads, see Query using Pre-Defined Replica Set Tags.有关使用分析节点隔离工作负载的更多信息,请参阅使用预定义副本集标记进行查询。