All Products
Search
Document Center

ApsaraDB for MongoDB:Use the mongo shell to connect to a database in SSL encryption mode

Last Updated:Mar 28, 2026

Connect to an ApsaraDB for MongoDB database using the mongo shell with SSL encryption to secure data in transit.

Prerequisites

Before you begin, ensure that you have:

When to use SSL encryption

  • Internet connections: Enable SSL encryption when connecting over the Internet. Internal endpoint connections are generally secure and do not require SSL.

  • CPU impact: Enabling SSL encryption significantly increases CPU utilization. Enable it only when necessary.

  • New endpoints: After enabling SSL encryption, any new endpoint you add (such as a new node endpoint or public endpoint) does not support SSL encryption. To enable SSL for the new endpoint, update the SSL certificate. See Update an SSL certificate.

Connect using SSL

The following steps use a local Linux server as an example.

SSL connection parameters

The following table lists all SSL-related parameters for the mongo shell connection command.

ParameterDescription
--sslEnables SSL/TLS encryption for the connection.
--sslCAFile <path>Path to the SSL CA certificate file (.pem).
--sslAllowInvalidHostnamesSkips hostname verification in the TLS/SSL CA certificate. Omit this parameter if your SSL setup requires hostname verification.

Steps

  1. Download the SSL CA certificate. See Configure SSL encryption for an instance.

  2. Decompress the downloaded package and upload the certificate files to your local server or ECS instance.

    In this example, the .pem file is uploaded to /test/sslcafile/ on the local server.
  3. Run the following command to connect to the database:

    mongo --host <host> -u <username> -p --authenticationDatabase <database> --ssl --sslCAFile <sslCAFile_path> --sslAllowInvalidHostnames

    Replace the placeholders with your actual values:

    PlaceholderDescription
    <host>Connection string (including port number) of the primary or secondary node. See Connect to a replica set instance. For Internet connections, use the public endpoint. For internal network connections, make sure the instance and ECS instance share the same network type — if Virtual Private Cloud (VPC), they must be in the same VPC.
    <username>Database account. The initial account is root. Avoid using root in production — create dedicated accounts with appropriate permissions instead. See Manage the permissions of MongoDB database users.
    <database>Authentication database. Enter admin if the username is root.
    <sslCAFile_path>Path to the SSL CA certificate file.

    Example (username: test, authentication database: admin):

    mongo --host dds-bp19f409d7512****-pub.mongodb.rds.aliyuncs.com:3717 -u test -p --authenticationDatabase admin --ssl --sslCAFile /test/sslcafile/ApsaraDB-CA-Chain.pem --sslAllowInvalidHostnames
  4. When Enter password appears, type the password for the database account and press Enter.

    The password is not displayed as you type. If you forget the root password, reset it. See (Optional) Reset a password.

What's next