Docs Home → MongoDB for VS Code
Connect to Your MongoDB Deployment连接到您的MongoDB部署
On this page本页内容
This page outlines how to use MongoDB for VS Code to connect to a MongoDB host. 本页概述了如何使用MongoDB for VS Code连接到MongoDB主机。You can connect to a standalone, replica set, or sharded cluster host.您可以连接到独立的、副本集或分片集群主机。
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 MongoDB for VS Code to your deployment.Atlas是一个云托管的数据库即服务,无需安装,提供免费的入门层,并提供可复制的URI,可以轻松地将MongoDB for VS Code连接到您的部署。
To create a cluster on the Atlas free tier, run the command 要在Atlas免费层上创建集群,请在Visual Studio Code命令面板中运行命令MongoDB: Open Overview Page
in the Visual Studio Code Command Palette and then click Create free cluster.MongoDB:Open Overview Page
,然后单击创建免费集群。
To create an Atlas cluster using a Terraform template included with MongoDB for VS Code, see Create an Atlas Cluster from a Template using Terraform.要使用MongoDB for VS Code中包含的Terraform模板创建Atlas集群,请参阅使用Terraform从模板创建阿特拉斯集群。
If you need to install MongoDB for VS Code, see Install MongoDB for VS Code for instructions.如果您需要安装MongoDB for VS Code,请参阅install MongoDB for VSCode以获取说明。
Considerations注意事项
When connecting MongoDB for VS Code to a replica set, use either the replica set SRV record or Replica Set Name when filling in your connection information.将MongoDB for VS Code连接到副本集时,请在填写连接信息时使用副本集SRV记录或副本集名称。It is not recommended to connect directly to an individual replica set member.不建议直接连接到单个复制副本集成员。You can't connect MongoDB for VS Code directly to an analytics node.您不能将MongoDB for VS Code直接连接到分析节点。MongoDB for VS Code appends theMongoDB for VS Code为所有部署连接添加值为MongoDB vscodeappName
connection string option with a value ofmongodb-vscode <version>
for all deployment connections.<version>
的appName
连接字符串选项。
Create a Connection to a Deployment创建到展开的连接
MongoDB for VS Code provides two methods to create a connection to a deployment. You can:MongoDB for VS Code提供了两种方法来创建到部署的连接。您可以:
Provide a deployment connection string, or提供部署连接字符串,或Fill in a deployment information in specific fields.在特定字段中填写部署信息。
Providing a connection string is faster and easier than filling in a deployment's details. 提供连接字符串比填写部署的详细信息更快、更容易。Use this option if Atlas hosts your deployment or if you have a connection string for a deployment available.如果Atlas托管您的部署,或者您有可用的部署连接字符串,请使用此选项。
Define how your connection is saved with the Default Connection Saving Location setting:定义如何使用“默认连接保存位置”设置保存连接:
Global | |
Workspace | |
Session |
When you provide a connection string, MongoDB for VS Code supports most Connection String Options supported by MongoDB.当您提供连接字符串时,MongoDB for VS Code支持MongoDB支持的大多数连接字符串选项。
Obtain your connection string.获取您的连接字符串。
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 Clusters视图。Click Connect for your desired cluster.单击所需集群的“连接”。Click Connect with MongoDB Compass.单击“使用MongoDB Compass连接”。Copy the provided connection string.复制提供的连接字符串。
To learn how to format the connection string for a deployment which is not hosted on Atlas, see Connection String URI Format.要了解如何格式化未托管在Atlas上的部署的连接字符串,请参阅连接字符串URI格式。
Open the Visual Studio Code Command Palette.打开Visual Studio Code命令面板。
Open the Visual Studio Code Command Palette to paste your connection string and connect to your cluster. 打开Visual Studio Code命令面板以粘贴连接字符串并连接到集群。You can open the Command Palette in one of the following ways:可以通过以下方式之一打开命令面板:
Perform the action described in the following table for your operating system and method:针对您的操作系统和方法执行下表中描述的操作:Operating System操作系统Method方法Actions操作Any Visual Studio Code Menu View > Command Palette MacOS Keyboard Shortcut Press Command
+Shift
+P
Windows and Linux Keyboard Shortcut Press Control
+Shift
+P
From the Command Palette, select MongoDB: Open Overview Page.从命令调色板中,选择“MongoDB:打开概览页面”。TipEnter在命令调色板中输入mongodb
in the Command Palette to display all of the MongoDB for VS Code commands you can use.mongodb
,以显示所有可以使用的mongodb for VS Code命令。In the Overview page, click Connect under the Connect with Connection String.在“概述”页面中,单击“使用连接字符串连接”下的“连接”。
Expand the Connections pane in the left navigation if it is collapsed.如果左侧导航中的“连接”窗格已折叠,请展开该窗格。Click the More Actions menu (...) and select Add MongoDB Connection with Connection String....单击“更多操作”菜单(…),然后选择“添加带有连接字符串的MongoDB连接…”。
Perform the action described in the following table for your operating system and method:针对您的操作系统和方法执行下表中描述的操作:Operating System操作系统Method方法Actions操作Any Visual Studio Code Menu View > Command Palette MacOS Keyboard Shortcut Press Command
+Shift
+P
Windows and Linux Keyboard Shortcut Press Control
+Shift
+P
From the Command Palette, select MongoDB: Connect with Connection String....从命令调色板中,选择“MongoDB:使用连接字符串连接”。TipEnter在命令调色板中输入mongodb
in the Command Palette to display all of the MongoDB for VS Code commands you can use.mongodb
,以显示所有可以使用的mongodb for VS Code命令。
Paste your connection string into the Command Palette.将连接字符串粘贴到命令面板中。
If your deployment requires authentication, ensure that you update the connection string with credentials of a database user for your deployment. 如果部署需要身份验证,请确保使用部署的数据库用户的凭据更新连接字符串。The connection might succeed without these credentials, but you won't be able to interact with data in your deployment.如果没有这些凭据,连接可能会成功,但您将无法与部署中的数据交互。
Open the MongoDB for VS Code Overview page from the Command Palette or from the MongoDB view in the Activity Bar.从命令面板或活动栏中的“MongoDB”视图打开MongoDB for VS Code “概览”页面。
Perform the action described in the following table for your operating system and method:针对您的操作系统和方法执行下表中描述的操作:Operating System操作系统Method方法Actions操作Any Visual Studio Code Menu View > Command Palette MacOS Keyboard Shortcut Press Command
+Shift
+P
Windows and Linux Keyboard Shortcut Press Control
+Shift
+P
From the Command Palette, select MongoDB: Open Overview Page.从命令调色板中,选择“MongoDB:打开概览页面”。TipEnter在命令调色板中输入mongodb
in the Command Palette to display all of the MongoDB for VS Code commands you can use.mongodb
,以显示所有可以使用的mongodb for VS Code命令。
Expand the Connections pane in the left navigation if it's collapsed.如果左侧导航中的“连接”窗格已折叠,请展开该窗格。Click the More Actions menu (...), then click Add MongoDB Connection to open the MongoDB for VS Code Overview page.单击更多操作菜单(…),然后单击“添加MongoDB连接”以打开MongoDB for VS Code“概览”页面。
Enter your connection information.输入您的连接信息。
If you want to connect to your deployment using a connection string, click Connect with a connection string, then paste your connection string into the Command Palette.如果要使用连接字符串连接到部署,请单击“使用连接字符串进行连接”,然后将连接字符串粘贴到命令面板中。
For more information, see the Paste Connection String tab in Create a Connection to a Deployment.有关详细信息,请参阅创建到部署的连接中的“粘贴连接字符串”选项卡。
The General tab contains the following options:“常规”选项卡包含以下选项:
| |
Note
|
If you are connecting to your deployment using TLS/SSL or an SSH tunnel, refer to the following tabs for specific instructions:如果使用TLS/SSL或SSH隧道连接到部署,请参阅以下选项卡以获取具体说明:
This option directs MongoDB for VS Code to connect to your MongoDB deployment via TLS/SSL. 此选项指示MongoDB for VS Code通过TLS/SSL连接到您的MongoDB部署。You can select one of the following SSL options:您可以选择以下SSL选项之一:
| |||||||||||
For production use, your MongoDB deployment should use valid certificates generated and signed by a single certificate authority. 对于生产使用,您的MongoDB部署应该使用由单个证书颁发机构生成和签名的有效证书。You or your organization can generate and maintain an independent certificate authority, or use certificates generated by a third-party TLS/SSL vendor.您或您的组织可以生成和维护独立的证书颁发机构,也可以使用第三方TLS/SSL供应商生成的证书。
This option directs MongoDB for VS Code to connect to your MongoDB deployment via an SSH tunnel. The tunnel automatically starts when you connect to MongoDB and stops when you disconnect.此选项指示MongoDB for VS Code通过SSH隧道连接到您的MongoDB部署。当您连接到MongoDB时,隧道会自动启动,当您断开连接时会停止。
In the SSH Tunnel dropdown, choose either a password or an identity file to provide authentication. 在“SSH隧道”下拉列表中,选择密码或身份文件以提供身份验证。Once you select your identification method, specify the following options:选择识别方法后,请指定以下选项:
SSH Hostname | |
SSH Tunnel Port | |
SSH Username | |
SSH Identity File | ~/.ssh directory. ~/.ssh 目录中可以找到标识文件。
|
SSH Passphrase | |
SSH Password |
To use an SSH tunnel through a bastion host, the SSH configuration on the bastion host must allow TCP port forwarding; i.e., the 要使用通过堡垒主机的SSH隧道,堡垒主机上的SSH配置必须允许TCP端口转发;也就是说,AllowTcpForwarding
directive in the /etc/ssh/sshd_config
file is set to yes
. /etc/ssh/sshd_config
文件中的AllowTcpForwarding
指令被设置为yes
。If 如果AllowTcpForwarding
is not set to yes
, set to yes
and restart the SSH daemon (sudo service sshd restart
).AllowTcpForwarding
未设置为yes
,请设置为yes
并重新启动SSH守护进程(sudo服务sshd重新启动)。
Instead of creating the SSH tunnel using MongoDB for VS Code, you can also create the tunnel manually from the command line:您也可以从命令行手动创建隧道,而不是使用MongoDB for VS Code创建SSH隧道:
ssh -L <local_port>:<mongodb_hostname>:<mongodb_port> \
<user>@<bastion_hostname> -fN
If you have a deployment running on 如果您在端口27017上的hostname-a.com
on port 27017, and the bastion host is hostname-b.com
with user name ec2-user
, you can build the tunnel viahostname-a.com
上运行部署,并且bastion主机是hostname-b.com
,用户名为ec2-user
,则可以通过
ssh -L 27000:hostname-a.com:27017 ec2-user@hostname-b.com -fN
Using this SSH tunnel, you can now connect MongoDB for VS Code (or the 使用这个SSH隧道,您现在可以将MongoDB for VS Code(或mongo
shell) to localhost:27000
to connect to the deployment running on hostname-a.com
.mongo
shell)连接到localhost:27000
,以连接到在主机名acom上运行的部署。
You cannot connect to a replica set via an SSH tunnel. 您无法通过SSH隧道连接到复制副本集。MongoDB for VS Code cannot establish a connection to multiple servers across the same SSH tunnel.MongoDB for VS Code无法通过同一SSH隧道建立到多个服务器的连接。
Optional: Specify read preference in the Advanced tab.可选:在“高级”选项卡中指定读取首选项。
You can select the Read Preference, which specifies how MongoDB for VS Code directs read operations. Options are:您可以选择“读取首选项”,它指定MongoDB for VS Code如何指导读取操作。选项包括:
Primary | |
Primary Preferred | |
Secondary | |
Secondary Preferred | |
Nearest |
If omitted, defaults to Primary. 如果忽略,则默认为“主要”。To learn more about read preferences, see Read Preference.要了解有关阅读首选项的更多信息,请参阅阅读首选项。
Activate a Connection激活连接
You can connect MongoDB for VS Code to only one deployment at a time. 一次只能将MongoDB for VS Code连接到一个部署。To change the active connection to a different deployment, or to connect to a deployment from which you were disconnected:要将活动连接更改为其他部署,或连接到断开连接的部署,请执行以下操作:
Launch MongoDB Shell启动MongoDB Shell
You can connect the MongoDB Shell or legacy 您可以将MongoDB Shell或遗留的mongo
shell to your active deployment.mongo
Shell连接到您的活动部署。
Considerations注意事项
The shell that MongoDB for VS Code uses to connect to your deployment is determined by the Shell setting in your extension settings.MongoDB for VS Code用于连接到部署的shell由扩展设置中的shell设置决定。You can choose either the MongoDB Shell or the legacy您可以选择MongoDB Shell或传统的mongo
shell.mongo
Shell。The path to your selected shell must exist in your system's所选shell的路径必须存在于系统的PATH
.PATH
中。If it does not exist in your如果它不存在于您的PATH
, the operation errors.PATH
中,则操作将出错。
Procedure程序
To connect the shell to your active deployment:要将shell连接到活动部署,请执行以下操作:
In the MongoDB for VS Code Connections list, right-click your active deployment.在MongoDB for VS Code“连接”列表中,右键单击您的活动部署。Select Launch MongoDB Shell.选择“Launch MongoDB Shell”。
MongoDB for VS Code opens the Terminal window in VS Code and launches the shell connected to your selected deployment.MongoDB for VS Code打开VS Code中的“终端”窗口,并启动连接到所选部署的shell。
Rename a Connection重命名连接
To rename a connection to a deployment in MongoDB for VS Code:要在MongoDB for VS Code中重命名与部署的连接,请执行以下操作:
Disconnect from a Deployment断开与部署的连接
MongoDB for VS Code provides two methods to disconnect from a deployment. You can:MongoDB for VS Code提供了两种从部署断开连接的方法。您可以:
Disconnect with the Command Palette, or断开与命令面板的连接,或Disconnect from the MongoDB view in the Activity Bar.从“活动栏”中的MongoDB视图断开连接。
Disconnecting from a MongoDB instance closes the MongoDB for VS Code connection to the active instance. 断开与MongoDB实例的连接将关闭与活动实例的MongoDB for VS Code连接。You must reconnect to interact with data in your deployment again.必须重新连接才能再次与部署中的数据交互。
Remove a Connection删除连接
MongoDB for VS Code provides two methods to remove a connection to a deployment from Visual Studio Code. You can:MongoDB for VS Code提供了两种方法来从Visual Studio Code中删除到部署的连接。您可以:
Remove a connection with the Command Palette, or删除与命令面板的连接,或Remove a connection from the MongoDB view in the Activity Bar.从“活动栏”的MongoDB视图中删除连接。
Removing a connection from Visual Studio Code removes it both from the current workspace and from Visual Studio Code globally.从Visual Studio Code中删除连接会将其从当前工作区和Visual Studio Code全局删除。
From the Command Palette, select MongoDB: Remove Connection....从命令调色板中,选择“MongoDB:删除连接...”。
Enter 在命令调色板中输入mongodb,以显示所有可以使用的mongodb for VS Code命令。mongodb
in the Command Palette to display all of the MongoDB for VS Code commands you can use.
If you have more than one connection, select the connection you want to remove from the Command Palette.如果有多个连接,请从命令面板中选择要删除的连接。