On this page本页内容
MongoDB for VS Code is currently available as a Preview in the Visual Studio Marketplace.MongoDB for VS Code目前在 Visual Studio Marketplace中作为预览版提供。The product, its features, and the corresponding documentation may change during the Preview stage.在预览阶段,产品、其功能和相应的文档可能会更改。
This page outlines how to use MongoDB for VS Code to connect to a MongoDB host.本页概述如何使用MongoDB forvs代码连接到MongoDB主机。You can connect to a standalone, replica set, or sharded cluster host.您可以连接到独立、副本集或分片群集主机。
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 VS for Code到您的部署。
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从模板创建Atlas集群。
If you need to install MongoDB for VS Code , see Install MongoDB for VS Code for instructions.如果需要安装MongoDB for VS代码,请参阅安装MongoDB for VS Code获取说明。
appName
connection string option with a value of mongodb-vscode <version>
for all deployment connections.appName
连接字符串选项追加到mongodb-vscode <version>
后面用于所有部署连接。MongoDB for VS Code provides two methods to create a connection to a deployment.MongoDB for VS Code提供了两种方法来创建到部署的连接。You can:你可以这样做:
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支持的大多数连接字符串选项。
You can use either the Standard Connection String Format or the DNS Seedlist Connection Format.您可以使用 标准连接字符串格式或DNS种子列表连接格式。
Any | Visual Studio Code |
View > Command Palette |
MacOS | Command + Shift + P | |
Windows and Linux | Control + Shift + P |
Enter 在命令面板中输入mongodb
in the Command Palette to display all of the MongoDB for VS Code commands you can use.mongodb
以显示你可以使用的所有的MongoDB for VS Code命令。
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.如果没有这些凭据,连接可能会成功,但您将无法与部署中的数据交互。
Enter
或Return
键。¶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 Hostname dialog contains the following connection field options:主机名称对话框包含以下连接字段选项:
| |
| |
SRV |
Note
Example
|
Note
Note
|
You can specify the following security connection options:可以指定以下安全连接选项:
|
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.此选项指示MongoDB for VS Code通过SSH隧道连接到MongoDB 部署。The tunnel automatically starts when you connect to MongoDB and stops when you disconnect.当您连接到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 | |
| |
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 service sshd restart
)。
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 如果你有一个运行在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
端口27017上的部署,并且堡垒主机是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
,以连接运行在hostname-a.com
上的部署。
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隧道与多个服务器建立连接。
You can connect MongoDB for VS Code to only one deployment at a time.您一次只能将MongoDB for VS代码连接到一个部署。To change the active connection to a different deployment, or to connect to a deployment from which you were disconnected:要将活动连接更改为其他部署,或连接到断开连接的部署,请执行以下操作:
You can connect the MongoDB Shell or legacy 你可以连接MongoDB Shell或传统的mongo
shell to your active deployment.mongo
shell来激活你的部署。
mongo
shell.mongo
shell。PATH
.PATH
中。PATH
, the operation errors.PATH
中没有它,操作将出错。To connect the shell to your active deployment:要将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连接到你选中的部署。
To rename a connection to a deployment in MongoDB for VS Code :若要重命名MongoDB for VS Code中的一个部署的连接:
MongoDB for VS Code provides two methods to disconnect from a deployment.MongoDB for VS Code提供了两种与部署断开连接的方法。You can:你可以选择:
Any | Visual Studio Code |
View > Command Palette |
MacOS | Command + Shift + P | |
Windows and Linux | Control + Shift + P |
Enter 在命令面板中输入mongodb
in the Command Palette to display all of the MongoDB for VS Code commands you can use.mongodb
以显示你可以使用的所有MongoDB for VS Code命令。
Enter
。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.必须再次在部署中重新连接数据。
MongoDB for VS Code provides two methods to remove a connection to a deployment from Visual Studio Code .MongoDB for VS Code提供了两种方法来从Visual Studio Code中删除到部署的连接。You can:你可以这样做:
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中全局删除该连接。
Any | Visual Studio Code |
View > Command Palette |
MacOS | Command + Shift + P | |
Windows and Linux | Control + Shift + P |
Enter 在命令面板中输入mongodb
in the Command Palette to display all of the MongoDB for VS Code commands you can use.mongodb
以显示你可以使用的所有MongoDB for VS Code命令。
If you have more than one connection, select the connection you want to remove from the Command Palette.如果有多个连接,请从命令选项板中选择要删除的连接。