Docs HomeMongoDB Shell

Connect to a Deployment连接到部署

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部署来连接。

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:要连接到使用默认端口27017localhost上运行的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上指定要连接的端口,可以使用以下任一项:

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
Note

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 --username and --authenticationDatabase options. mongosh prompts you for a password, which it hides as you type.要连接到需要身份验证的MongoDB部署,请使用--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的编程使用,就像驱动程序一样。

Tip

See also: 另请参阅:

Connect to a Replica Set连接到副本集

To connect to a replica set, you can either:要连接到复制副本集,您可以:

Option 1: DNS Seedlist Format选项1:DNS种子列表格式

To use the DNS seedlist connection format, include the +srv modifier in your connection string.要使用DNS种子列表连接格式,请在连接字符串中包含+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/"
Note

+srv TLS Behavior

When you use the +srv connection string modifier, MongoDB automatically sets the --tls connection option to true. 当您使用+srv连接字符串修饰符时,MongoDB会自动将--tls连接选项设置为trueTo override this behavior, set --tls to false.若要覆盖此行为,请将--tls设置为false

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"
Note

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

  • The replicaSet 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 the tls option to true 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 to true 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 test database.如果没有在URI路径中指定数据库,则连接到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 Mongo() or connect() method to connect to a different deployment from within the MongoDB Shell.如果您已经连接到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, or exit().键入.exitexitexit()
  • Type quit or quit().键入quitquit()
  • Press Ctrl + D.按下Ctrl + D
  • Press Ctrl + C twice.Ctrl + C两次。

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 allow authMechanismProperties=CANONICALIZE_HOST_NAME:true|false in the connection string. Kerberos身份验证不允许在连接字符串中使用authMechanismProperties=CANONICALIZE_HOST_NAME:true|falseInstead, use either:相反,请使用以下任一选项:

    • authMechanismProperties=CANONICALIZE_HOST_NAME:forward
    • authMechanismProperties=CANONICALIZE_HOST_NAME:forwardAndReverse
    • authMechanismProperties=CANONICALIZE_HOST_NAME:none
  • mongosh currently only supports the zlib compressor. mongosh目前只支持zlib压缩器The following compressors are not supported:不支持以下压缩机:

    • zstd
    • snappy