Connect to a Deployment连接到部署
On this page本页内容
Prerequisites先决条件Supported MongoDB Versions支持的MongoDB版本Connect to a Local Deployment on the Default Port连接到默认端口上的本地部署Connect to a Local Deployment on a Non-Default Port连接到非默认端口上的本地部署Connect to a Deployment on a Remote Host连接到远程主机上的部署Specify Connection Options指定连接选项Connect With Authentication使用身份验证连接Connect to a Replica Set连接到副本集Connect Using TLS使用TLS连接Connect to a Specific Database连接到特定数据库Connect to a Different Deployment连接到其他部署Verify Current Connection验证当前连接Disconnect from a Deployment断开与部署的连接Non-genuine Deployments非正版部署Limitations局限性
This page shows how to use the MongoDB Shell to connect to a MongoDB deployment.本页展示了如何使用MongoDB Shell连接到MongoDB部署。
Prerequisites先决条件
To use the MongoDB Shell, you must have a MongoDB deployment to connect to.要使用MongoDB Shell,您必须有一个MongoDB部署来连接。
For a free cloud-hosted deployment, you can use MongoDB Atlas.对于免费的云托管部署,您可以使用MongoDB Atlas。
To learn how to run a local MongoDB deployment, see Install MongoDB.要了解如何运行本地MongoDB部署,请参阅安装MongoDB。
Supported MongoDB Versions支持的MongoDB版本
You can use the MongoDB Shell to connect to MongoDB version 4.2 or greater.您可以使用MongoDB Shell连接到MongoDB 4.2或更高版本。
Connect to a Local Deployment on the Default Port连接到默认端口上的本地部署
To connect to a MongoDB deployment running on localhost with default port 27017, run 要连接到使用默认端口mongosh
without any options:27017
在localhost
上运行的MongoDB部署,请在没有任何选项的情况下运行mongosh
:
mongosh
This is equivalent to the following command:这相当于以下命令:
mongosh "mongodb://localhost:27017"
Connect to a Local Deployment on a Non-Default Port连接到非默认端口上的本地部署
To specify a port to connect to on localhost, you can use either:要在localhost上指定要连接的端口,可以使用以下任一项:
A connection string with the chosen port具有所选端口的连接字符串The--port
command-line option--port
命令行选项
For example, the following commands connect to a deployment running on localhost port 28015:例如,以下命令连接到在localhost端口28015上运行的部署:
mongosh "mongodb://localhost:28015"
mongosh --port 28015
Connect to a Deployment on a Remote Host连接到远程主机上的部署
To specify a remote host and port, you can use either:要指定远程主机和端口,可以使用以下任一项:
A connection string with the chosen host and port.具有所选主机和端口的连接字符串。The--host
and--port
command-line options.--host
和--port
命令行选项。If you omit the如果省略--port
option,mongosh
uses the default port 27017.--port
选项,mongosh
将使用默认端口27017
。
For example, the following commands connect to a MongoDB deployment running on host 例如,以下命令连接到运行在主机mongodb0.example.com
and port 28015:mongodb0.example.com
和端口28015上的MongoDB部署:
mongosh "mongodb://mongodb0.example.com:28015"
mongosh --host mongodb0.example.com --port 28015
Connect to MongoDB Atlas连接到MongoDB Atlas
If your remote host is an Atlas cluster, you can copy your connection string from the Atlas UI. 如果您的远程主机是Atlas集群,则可以从Atlas UI复制连接字符串。To learn more, see Connect to a Cluster in the Atlas documentation.要了解更多信息,请参阅Atlas文档中的“连接到群集”。
Specify Connection Options指定连接选项
Specify different connection options to connect to different types of deployments.指定不同的连接选项以连接到不同类型的部署。
Connect With Authentication使用身份验证连接
To connect to a MongoDB deployment that requires authentication, use the 要连接到需要身份验证的MongoDB部署,请使用--username
and --authenticationDatabase
options. mongosh
prompts you for a password, which it hides as you type.--username
和--authenticationDatabase
选项。mongosh
会提示您输入密码,当您键入密码时,它会将其隐藏起来。
For example, to authenticate as user 例如,要在alice
on the admin
database, run the following command:admin
数据库上作为用户alice
进行身份验证,请运行以下命令:
mongosh "mongodb://mongodb0.example.com:28015" --username alice --authenticationDatabase admin
To provide a password as part of the connection command instead of using the prompt, use the 要在连接命令中提供密码而不是使用提示,请使用--password
option. --password
选项。Use this option for programmatic usage of 将此选项用于mongosh
, like a driver.mongosh
的编程使用,就像驱动程序一样。
See also: 另请参阅:
To enforce authentication on a deployment, see Enable Access Control.要对部署强制身份验证,请参阅启用访问控制。To provision access to a MongoDB deployment, see Database Users.要提供对MongoDB部署的访问权限,请参阅数据库用户。
Connect to a Replica Set连接到副本集
To connect to a replica set, you can either:要连接到复制副本集,您可以:
Use the DNS Seedlist Connection Format.使用DNS种子列表连接格式。Explicitly specify the replica set name and members in the connection string.在连接字符串中显式指定副本集名称和成员。
Option 1: DNS Seedlist Format选项1:DNS种子列表格式
To use the DNS seedlist connection format, include the 要使用DNS种子列表连接格式,请在连接字符串中包含+srv
modifier in your connection string.+srv
修饰符。
For example, to connect to a replica set on 例如,要连接到server.example.com
, run the following command:server.example.com
上的复制副本集,请运行以下命令:
mongosh "mongodb+srv://server.example.com/"
Option 2: Specify Members in Connection String选项2:在连接字符串中指定成员
You can specify individual replica set members in the connection string.您可以在连接字符串中指定各个副本集成员。
For example, to connect to a three-member replica set named 例如,要连接到名为replA
, run the following command:replA
的三成员复制集,请运行以下命令:
mongosh "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA"
directConnection Parameter Added AutomaticallydirectConnection参数已自动添加
When you specify individual replica set members in the connection string, 当您在连接字符串中指定单个副本集成员时,mongosh
automatically adds the directConnection=true
parameter, unless at least one of the following is true:mongosh
会自动添加directConnection=true
参数,除非以下至少有一项为true
:
ThereplicaSet
query parameter is present in the connection string.replicaSet
查询参数存在于连接字符串中。The connection string uses the连接字符串使用mongodb+srv://
connection string format.mongodb+srv://
连接字符串格式。The connection string contains a seed list with multiple hosts.连接字符串包含一个包含多个主机的种子列表。The connection string already contains a连接字符串已包含directConnection
parameter.directConnection
参数。
When 当directConnection=true
, all operations are run on the host specified in the connection URI.directConnection=true
时,所有操作都在连接URI中指定的主机上运行。
Connect Using TLS使用TLS连接
To connect to a deployment using TLS, you can either:要使用TLS连接到部署,您可以:
Use the DNS Seedlist Connection Format.使用DNS种子列表连接格式。The+srv
connection string modifier automatically sets thetls
option totrue
for the connection.+srv
连接字符串修饰符会自动将连接的tls
选项设置为true
。For example, to connect to a DNS seedlist-defined replica set with例如,要连接到启用了tls
enabled, run the following command:tls
的DNS种子列表定义的副本集,请运行以下命令:mongosh "mongodb+srv://server.example.com/"
Set the在连接字符串中将--tls
option totrue
in the connection string.--tls
选项设置为true
。For example, to enable例如,要使用连接字符串选项启用tls
with a connection string option, run the following command:tls
,请运行以下命令:mongosh "mongodb://mongodb0.example.com:28015/?tls=true"
Specify the指定--tls
command-line option.--tls
命令行选项。For example, to connect to a remote host with例如,要连接到启用了tls
enabled, run the following command:tls
的远程主机,请运行以下命令:mongosh "mongodb://mongodb0.example.com:28015" --tls
Connect to a Specific Database连接到特定数据库
To connect to a specific database, specify a database in your connection string URI path. 若要连接到特定数据库,请在连接字符串URI路径中指定一个数据库。If you do not specify a database in your URI path, you connect to the 如果没有在URI路径中指定数据库,则连接到test
database.test
数据库。
For example, to connect to a database called 例如,要连接到本地主机上名为qa
on localhost, run the following command:qa
的数据库,请运行以下命令:
mongosh "mongodb://localhost:27017/qa"
Connect to a Different Deployment连接到其他部署
If you are already connected to a deployment in the MongoDB Shell, you can use the 如果您已经连接到MongoDB Shell中的部署,则可以使用Mongo()
or connect() method to connect to a different deployment from within the MongoDB Shell.Mongo()
或connect()方法连接到MongDB Shell中的其他部署。
To learn how to connect to a different deployment using these methods, see Open a New Connection.要了解如何使用这些方法连接到不同的部署,请参阅打开新连接。
Verify Current Connection验证当前连接
To verify your current database connection, use the 要验证当前的数据库连接,请使用db.getMongo()
method.db.getMongo()
方法。
The method returns the connection string URI for your current connection.该方法返回当前连接的连接字符串URI。
Disconnect from a Deployment断开与部署的连接
To disconnect from a deployment and exit 要断开与部署的连接并退出mongosh
, perform one of the following actions:mongosh
,请执行以下操作之一:
Type键入.exit
,exit
, orexit()
..exit
、exit
或exit()
。Type键入quit
orquit()
.quit
或quit()
。Press按下Ctrl + D。Ctrl
+D
.Press按Ctrl + C两次。Ctrl
+C
twice.
Non-genuine Deployments非正版部署
The shell displays a warning message when you connect to non-genuine MongoDB instances. 当您连接到非正版MongoDB实例时,shell会显示一条警告消息。Non-genuine instances may behave differently from the official MongoDB instances due to missing, inconsistent, or incomplete features.由于功能缺失、不一致或不完整,非正版实例的行为可能与官方MongoDB实例不同。
Limitations局限性
Kerberos authentication does not allowKerberos身份验证不允许在连接字符串中使用authMechanismProperties=CANONICALIZE_HOST_NAME:true|false
in the connection string.authMechanismProperties=CANONICALIZE_HOST_NAME:true|false
。Instead, use either:相反,请使用以下任一选项:authMechanismProperties=CANONICALIZE_HOST_NAME:forward
authMechanismProperties=CANONICALIZE_HOST_NAME:forwardAndReverse
authMechanismProperties=CANONICALIZE_HOST_NAME:none
mongosh
currently only supports thezlib
compressor.mongosh
目前只支持zlib
压缩器。The following compressors are not supported:不支持以下压缩机:zstd
snappy