Navigation

    Connect to a Deployment

    important

    Ensure that you have a running MongoDB deployment before attempting to connect with mongosh. The connection method you use depends on your deployment type.

    Local MongoDB Instance on Default Port

    Run mongosh without any command-line options to connect to a MongoDB instance running on your localhost with default port 27017:

    mongosh

    This is equivalent to the following command:

    mongosh "mongodb://localhost:27017"

    Local MongoDB Instance on a Non-Default Port

    To specify a port to connect to on localhost, you can use:

    • A connection string.

      example

      To connect to a MongoDB instance running on localhost with a non-default port 28015:

      mongosh "mongodb://localhost:28015"
    • The command-line option --port.

      example

      To connect to a MongoDB instance running on localhost with a non-default port 28015:

      mongosh --port 28015

    MongoDB Instance on a Remote Host

    To specify a remote host and port, you can use:

    • A connection string.

      example

      To connect to a MongoDB instance running on a remote host on port 28015:

      mongosh "mongodb://mongodb0.example.com:28015"

      Connecting to

      If your remote host is a MongoDB Atlas cluster, you can copy your connection string from the Atlas UI. To learn more, see Connect to a Cluster.

    • The command-line options --host and --port. If you do not include the --port option, mongosh uses the default port 27017.

      example

      To connect to a MongoDB instance running on a remote host on port 28015:

      mongosh --host mongodb0.example.com --port 28015

    Connection Options

    Connect with Authentication

    To connect to a MongoDB instance requires authentication, use the --username and --authenticationDatabase command-line options. mongosh prompts you for a password, which it masks as you type.

    example

    To connect to a remote MongoDB instance and authenticate against the admin database as user alice:

    mongosh "mongodb://mongodb0.example.com:28015" --username alice --authenticationDatabase admin

    note

    To provide a password with the command instead of using the masked prompt, you can use the --password option.

    see also

    Connect to a Replica Set

    To connect to a replica set:

    • If you are using the DNS Seedlist Connection Format, you can include the +srv modifier in your connection string.

      example

      mongosh "mongodb+srv://server.example.com/"

      note

      Using the +srv connection string modifier automatically sets the tls option to true for the connection. You can override this behavior by explicitly setting tls to false.

    • You can specify the replica set name and members in the connection string.

      example

      To connect to a three-member replica set named replA:

      mongosh "mongodb://mongodb0.example.com.local:27017,mongodb1.example.com.local:27017,mongodb2.example.com.local:27017/?replicaSet=replA"

    Connect using TLS

    For tls connections:

    • If you are using the DNS Seedlist Connection Format, the +srv connection string modifier automatically sets the tls option to true for the connection:

      example

      To connect to a DNS seedlist-defined replica set with tls enabled:

      mongosh "mongodb+srv://server.example.com/"
    • You can use the tls option to set tls=true in the connection string:

      example

      To enable tls in the connection string:

      mongosh "mongodb://mongodb0.example.com:28015/?tls=true"
    • You can specify the --tls command-line option.

      example

      To connect to a remote host with tls enabled:

      mongosh "mongodb://mongodb0.example.com:28015" --tls

    Disconnect from a Deployment

    To disconnect from a deployment and exit mongosh, you can:

    • Type .exit, or
    • Press Ctrl + C twice.