Connect to MongoDB连接到MongoDB
On this page本页内容
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主机。您可以连接到独立的、副本集或分片集群主机。
MongoDB Atlas
If you need to create a MongoDB host, consider using MongoDB Atlas如果您需要创建一个MongoDB主机,请考虑使用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.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.将Compass连接到副本集时,建议不要直接连接到单个副本集成员。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可能会强制关闭连接或显示过时的数据。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实例不同;例如,缺失或不完整的特征、不同的特征行为等。
Connect连接
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.要了解更多信息,请参阅高级连接选项。
Paste 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. 默认情况下,Compass的默认socketTimeoutMS值为60000,即60秒。If you are frequently experiencing timeouts in Compass, consider setting this option to a higher value in your connection string.如果您在Compass中经常遇到超时,请考虑在连接字符串中将此选项设置为更高的值。
For a complete list of the connection string options that Compass supports, see the Compass Connection README有关Compass支持的连接字符串选项的完整列表,请参阅GitHub上的Compass connection自述 on GitHub.
。
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.单击“使用MongoDBCompass连接”。Copy the provided connection string.复制提供的连接字符串。
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格式。
Connect to your cluster.连接到您的群集。
Click Connect or Save & Connect to navigate to the Compass Home Page.单击“连接”或“保存并连接”导航到Compass主页。
The Save & Connect button prompts you to save your connection string as a Favorite Connection and then navigates to the home page.“保存并连接”按钮会提示您将连接字符串保存为收藏夹连接,然后导航到主页。
Required Access必需的访问权限
Once you are connected to your MongoDB deployment, you may require specific user roles to access various Compass features. 一旦连接到MongoDB部署,您可能需要特定的用户角色来访问各种Compass功能。For more information on the required roles for Compass features, see Required Access.有关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可以在跳转主机上运行。
For details, see Start Compass from the Command Line.有关详细信息,请参阅从命令行启动Compass。
Advanced Connection Options高级连接选项
The advanced connection options provide additional ways to connect Compass to MongoDB. 高级连接选项提供了将Compass连接到MongoDB的其他方法。Advanced connection options allow you to specify authentication, TLS/SSL, and SSH to securely connect to your deployment.高级连接选项允许您指定身份验证、TLS/SSL和SSH以安全地连接到部署。
For details, see Advanced Connection Options.有关详细信息,请参阅高级连接选项。
See also:
To learn how to disconnect your deployment, see Disconnect from MongoDB要了解如何断开部署,请参阅断开与MongoDB的连接