Connect to Your MongoDB Deployment连接到MongoDB部署

On this page本页内容

Note
Preview预览

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.您可以连接到独立、副本集或分片群集主机。

Note
MongoDB Atlas

If you need to create a MongoDB host, consider using MongoDB Atlas.如果需要创建MongoDB主机,可以考虑使用MongoDB AtlasAtlas 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获取说明。

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:利用默认连接保存位置设置来定义连接如何保存。

Setting设置 Description描述
Global Save your connection globally in VS Code, so it can be accessed from any workspace.在VS代码中全局保存连接,以便可以从任何工作区访问它。
Workspace Save your connection in your workspace.在工作区中保存连接。You cannot access the connection from a different workspace.无法从其他工作区访问连接。
Session Save the connection for only this VS Code session.仅针对此VS Code会话保存连接。The connection is lost when you close VS Code.关闭VS代码时,连接将丢失。

When you provide a connection string, MongoDB for VS Code supports most Connection String Options supported by MongoDB.当您提供连接字符串时,MongoDB for VS Code支持MongoDB支持的大多数连接字符串选项

1

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群集的连接字符串,请执行以下操作:

    1. Navigate to your Atlas Clusters view.导航到Atlas群集视图。
    2. Click Connect for your desired cluster.针对想要的群集点击连接
    3. Click Connect with MongoDB Compass.单击使用MongoDB Compass连接
    4. 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格式
2
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
3
Tip

Enter mongodb in the Command Palette to display all of the MongoDB for VS Code commands you can use.在命令面板中输入mongodb以显示你可以使用的所有的MongoDB for VS Code命令。

4
Important

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.如果没有这些凭据,连接可能会成功,但您将无法与部署中的数据交互。

5
1
2
  1. Click the Plus icon.点击加号图标。
  2. Expand the More Actions menu (...), then click Add MongoDB Connection.展开更多操作菜单(...),然后点击添加MongoDB连接
3
Note

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:主机名称对话框包含以下连接字段选项:

Option选项 Description描述
Hostname主机名

Hostname of the machine where the deployment is running.部署正在运行的机器的主机名称。

If you are running your deployment locally, this value is localhost.如果你正在本地运行部署,则该值为localhostIf you are connecting to an Atlas cluster, you can get your hostname from your cluster detail view in Atlas.如果你正在连接到某个Atlas群集,你可以从Atlas的群集详情视图取得主机名称。

If you are not sure of your hostname, contact your Database Administrator for information.如果你搞不清主机名称,请联系你的数据库管理员以获得信息。

Port端口

Port on which the deployment is running.正在运行部署的端口。Not required if you are using an SRV Record to connect to your MongoDB deployment.如果你在使用SRV记录连接到MongoDB部署,则不需要它。

By default, a standalone deployment operates on port 27017.默认情况下,独立运行的部署在端口27017上操作。If you are connecting to a replica set, your port may also be 27018 or 27019.如果连接到副本集,端口可能是2701827019For more information on the default ports MongoDB operates on, see Default MongoDB Port.关于默认端口MongoDB操作方面的更多信息,请参阅默认MongoDB端口。

If you are not sure of the port where your deployment operates, contact your Database Administrator for information.如果不确定部署操作的端口,请与数据库管理员联系以获取信息。

SRV Record记录

Indicates whether the provided Hostname is an SRV Record.表示提供的Hostname是否是一个SRV记录If this toggle is enabled, you do not need to specify a port.如果启用此切换,则不需要指定端口。

Note

You can only use SRV connection strings to connect to deployments running MongoDB 3.6 and later.只能使用SRV连接字符串连接到运行MongoDB 3.6及更高版本的部署。

SRV connection strings have a prefix of mongodb+srv:.SRV连接字符串的前缀为mongodb+srv:If you are using an SRV connection string, you do not need to include mongodb+srv in your Hostname.如果使用一个SRV连接字符串,你不需要在Hostname中包含mongodb+srv

Example

If the following is your SRV connection string for your MongoDB deployment:如果以下是MongoDB部署的SRV连接字符串:

mongodb+srv://<username>:<password>@gettingstarted-7q2cs.mongodb.net/test

Then, in MongoDB for VS Code, you would specify your Hostname as:则,在MongoDB for VS Code中,你需要将Hostname指定为:

gettingstarted-7q2cs.mongodb.net
Authentication身份验证

Authentication to use if the deployment requires authentication.如果部署需要身份验证,就必须使用Authentication。Atlas clusters use Username / Password authentication.Atlas群集使用用户名/密码身份验证。

Select your authentication method from the tabs below for specific instructions:从下面的选项卡中选择您的身份验证方法以获取特定说明:

Select Username / Password if the deployment uses either MongoDB-CR or SCRAM-SHA-1 as its authentication mechanism.如果部署使用MOngoDB-CR或SCRAM-SHA-1作为它的身份验证机制,则选择用户名/密码

Provide the Username, Password, and Authentication Database to authenticate the user.提供用户名密码身份验证数据库以验证用户。

Note

Starting in MongoDB version 4.0, MongoDB removes support for the deprecated MongoDB Challenge-Response (MONGODB-CR) authentication mechanism.从MongoDB v 4.0开始,MongoDB删除了对已建议弃用的MongoDB挑战-应答(MONGODB-CR)身份验证机制的支持。

Select SCRAM-SHA-256 if the deployment uses SCRAM-SHA-256 as its authentication mechanism (New in MongoDB 4.0).如果部署使用SCRAM-SHA-256作为它的身份验证机制(在MongoDB 4.0中新增),请选择SCRAM-SHA-256If selected, you must provide the Username, Password, and Authentication Database to authenticate the user.如果选择了它,你必须提供用户名密码身份验证数据库以验证用户。

For more information on the SCRAM authentication mechanism, see SCRAM.有关SCRAM身份验证机制的更多信息,请参阅SCRAM

Select LDAP if the deployment uses LDAP as its authentication mechanism.如果部署使用LDAP作为它的身份验证机制,请选择LDAPIf selected, you must provide the Username and Password to authenticate the user.如果选择了它,你必须提供用户名密码以验证用户。

Select X.509 if the deployment uses X.509 as its authentication mechanism.如果部署使用X.509作为它的身份验证机制,请选择X.509If selected, you must provide the Username to authenticate the user.如果选择了它,你必须提供用户名以验证用户。

Note

If you are using Atlas-managed certificates , your username must be prefaced by "CN=" per RFC 2253 <https://tools.ietf.org/html/rfc2253>__.如果你使用Atlas托管凭据,你的用户名必须"CN="开头,根据RFC 2253 <https://tools.ietf.org/html/rfc2253>__For example, the username "X509User" must be provided as "CN=X509User".例如,用户名“X509User”必须提供为“CN=X509User”。

4

You can specify the following security connection options:可以指定以下安全连接选项:

Option选项 Description描述
Replica Set Name副本集名称 If your MongoDB deployment is a replica set, specify the replica set name.如果MongoDB部署是副本集,请指定副本集名称。Not required if you specify your replica set Hostname as an SRV Record.如果将副本集主机名指定为SRV记录,则不需要。
Read Preference读取首选模式

Specifies how MongoDB for VS Code directs read operations.指定MongoDB for VS Code如何指导读取操作。Options are:选项包括:

Read Preference Mode读取首选项模式 Description描述
primary Default mode.默认模式。All operations read from the current replica set primary.从当前主副本集读取的所有操作。
primaryPreferred In most situations, operations read from the primary but if it is unavailable, operations read from secondary members.在大多数情况下,操作从主成员读取,但如果主成员不可用,则从辅助成员读取操作。
secondary All operations read from the secondary members of the replica set.从副本集的辅助成员读取的所有操作。
secondaryPreferred In most situations, operations read from secondary members but if no secondary members are available, operations read from the primary.在大多数情况下,操作从辅助成员读取,但如果没有辅助成员可用,则从主成员读取操作。
nearest Operations read from member of the replica set with the least network latency, irrespective of the member's type.从具有最小网络延迟的副本集成员读取的操作,与成员的类型无关。

To learn more about read preferences, see Read Preference.要了解有关读取首选项的详细信息,请参阅读取首选项

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选项之一:

Option选项 Description描述
System CA/Atlas Deployment系统CA/Atlas部署 Connect to MongoDB Atlas.连接到MongoDB Atlas
Server Validation服务器验证

MongoDB for VS Code validates the identity of the deployment.MongoDB for VS Code校验部署的身份。If selected, you must provide:如果选择了它,必须提供:

Certificate Authority证书颁发机构 One or more certificate files from trusted Certificate Authorities to validate the certificate provided by the deployment.来自受信任的证书颁发机构的一个或多个证书文件,以验证部署提供的证书。
Server and Client Validation服务器和客户端验证

The deployment performs certificate validation from its clients and MongoDB for VS Code validates the identity of the deployment.部署从其客户端执行证书验证,MongoDB for VS Code验证部署的标识。If selected, you must provide:如果选择了它,必须提供:

Certificate Authority File证书颁发机构文件 One or more certificate files from trusted Certificate Authorities to validate the certificate provided by the deployment.来自受信任的证书颁发机构的一个或多个证书文件,以验证部署提供的证书。
Client Certificate客户端证书 A TLS/SSL certificate to present to the deployment to verify the identity of the connecting client.要呈现给部署的TLS/SSL证书,以验证连接客户端的身份。Typical file extensions for the certificate are .crt or .pem.通常证书的文件扩展名是.crt.pem
Client Private Key客户端私钥 A TLS/SSL key file to the deployment to verify the identity of the connecting client.部署的TLS/SSL密钥文件,以验证连接客户端的身份。Typical file extensions for the private key are .key or .pem.通常私钥的文件扩展名是.key.pem
Client Key Password客户端密钥密码 If the Client Private Key is protected with a password/passphrase, you must provide the password/passphrase.如果客户端私钥受密码/密码短语的保护,则必须提供密码/密码短语。
Unvalidated (Insecure)未验证(不安全) Use TLS/SSL without any identity validation.在没有任何身份验证的情况下使用TLS/SSL。
Tip

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 Enter the bastion (jumpbox) hostname.输入bastion(jumpbox)主机名。This is the unique identifier (Fully Qualified Domain Name, or FQDN) for the machine to be accessed.这是要访问的计算机的唯一标识符(完全限定的域名或FQDN)。
SSH Tunnel PortSSH隧道端口 Provide the port used for the SSH connection.提供用于SSH连接的端口。This defaults to 22, the standard port for SSH.默认为22,SSH的标准端口。
SSH UsernameSSH用户名 The username of the profile to log into on the remote system.要登录到远程系统的配置文件的用户名。This will be the user for which you want to establish the SSH connection.这将是您要为其建立SSH连接的用户。
SSH Identity FileSSH标识文件

Select the file from which the identity (private key) for SSH public key authentication is read.选择从中读取SSH公钥身份验证的标识(私钥)的文件。

Unix or OS X: If using OpenSSH, identity files are found in the ~/.ssh directory.Unix或OS X:如果使用OpenSSH,则标识文件位于~/.ssh目录中。By default, the private key files have one of the following file names:默认情况下,私钥文件具有以下文件名之一:

  • id_dsa
  • id_ecdsa
  • id_ed25519
  • id_rsa

On Windows, the location of the identify files depends on your choice of SSH client, such as PuTTY.在Windows上,标识文件的位置取决于您选择的SSH客户机,例如PuTTY。

SSH PassphraseSSH密码短语 If your private key is encrypted, provide the passphrase used to decrypt your private key (stored in the specified identity file).如果您的私钥已加密,请提供用于解密私钥的密码短语(存储在指定的标识文件中)。A passphrase provides an extra layer of security for an SSH connection.密码短语为SSH连接提供了额外的安全层。
SSH PasswordSSH密码 The password used to secure the SSH connection.用于保护SSH连接的密码。This is required if you are not using an identity file.如果不使用标识文件,则必须提供它。

To use an SSH tunnel through a bastion host, the SSH configuration on the bastion host must allow TCP port forwarding; i.e., the AllowTcpForwarding directive in the /etc/ssh/sshd_config file is set to yes.要使用通过堡垒主机的SSH隧道,堡垒主机上的SSH配置必须允许TCP端口转发;即,/etc/ssh/sshd_config文件中的AllowTcpForwarding指令设置为yesIf 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
Example

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 via如果你有一个运行在hostname-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 mongo shell) to localhost:27000 to connect to the deployment running on hostname-a.com.使用此SSH隧道,现在你可以把MongoDB for VS Code(或mongo shell)连接到localhost:27000,以连接运行在hostname-a.com上的部署。

Note

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隧道与多个服务器建立连接。

5

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:要将活动连接更改为其他部署,或连接到断开连接的部署,请执行以下操作:

1
2

You can connect the MongoDB Shell or legacy mongo shell to your active deployment.你可以连接MongoDB Shell或传统的mongo shell来激活你的部署。

  • 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 legacy mongo shell.你既可以选择MongoDB Shell,也可以选择传统的mongo shell。
  • The path to your selected shell must exist in your system's PATH.选中的壳的路径必须存在于系统的PATH中。If it does not exist in your PATH, the operation errors.如果系统PATH中没有它,操作将出错。

To connect the shell to your active deployment:要将shell连接到活动部署,请执行以下操作:

  1. In the MongoDB for VS Code Connections list, right-click your active deployment.在MongoDB for VS Code 连接列表中,右键单击活动部署。
  2. Select Launch MongoDB Shell.选择启动 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连接到你选中的部署。

To rename a connection to a deployment in MongoDB for VS Code :若要重命名MongoDB for VS Code中的一个部署的连接:

1
2
3
  1. In the Command Palette, type a new name for the connection.在命令选项板中,键入连接的新名称。
  2. Press the Enter or Return key to rename the connection.EnterReturn键重命名连接。

MongoDB for VS Code provides two methods to disconnect from a deployment.MongoDB for VS Code提供了两种与部署断开连接的方法。You can:你可以选择:

1
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
2
  1. From the Command Palette, select MongoDB: Disconnect.从命令面板中,选择MongoDB: Disconnect

    Tip

    Enter mongodb in the Command Palette to display all of the MongoDB for VS Code commands you can use.在命令面板中输入mongodb以显示你可以使用的所有MongoDB for VS Code命令。

  2. Press Enter.接下Enter
1
2

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:你可以这样做:

Important

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中全局删除该连接。

1
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
2
Tip

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.如果有多个连接,请从命令选项板中选择要删除的连接。

3
1
2
←  Install安装 MongoDB for VS CodeNavigate Your Data导航数据  →