Help Center > > Getting Started> Getting Started with Clusters> Connecting to a Cluster Instance> Connecting to a Cluster Instance over a Public Network> Connecting to a Cluster Instance Using Mongo Shell

Connecting to a Cluster Instance Using Mongo Shell

Updated at: Jan 24, 2022 GMT+08:00

You can bind an EIP to a DDS instance so that you can access the instance from the Internet.

Scenario 1: If your applications are deployed on an ECS and are not in the same region as the DDS instance, you are advised to connect to the DDS instance from the ECS.

Figure 1 Accessing DDS from ECS across regions

Scenario 2: If your applications are deployed on a cloud server provided by other vendors, you are advised to connect your applications to a DDS instance using EIP.

Figure 2 Accessing DDS from other cloud servers

This section describes how to use Mongo Shell to connect to a cluster instance over a public network.

You can connect to an instance using an SSL connection or an unencrypted connection. The SSL connection is encrypted and more secure. To improve data transmission security, connect to instances using SSL.

Prerequisites

  1. For details about how to create and log in to an ECS, see Purchasing an ECS and Logging In to an ECS.
  2. Bind an EIP to the cluster instance and set security group rules to ensure that the instance can be accessed from the ECS.
  3. Install the MongoDB client on the ECS.

    For details about how to install a MongoDB client, see How Can I Install a MongoDB Client?

SSL

If you connect to an instance over the SSL connection, enable SSL first. Otherwise, an error is reported. For details about how to enable SSL, see Enabling and Disabling SSL.

  1. Log in to the management console.
  2. Click in the upper left corner and select a region and a project.
  3. Click in the upper left corner of the page and choose Databases > Document Database Service.
  4. On the Instance Management page, click the instance name.
  5. In the navigation pane on the left, choose Connections.
  6. In the Basic Information area, click next to the SSL field.
  7. Upload the root certificate obtained in 6 to the ECS.

    The following describes how to upload the certificate to a Linux and Window ECS:

    • In Linux, run the following command:
      scp<IDENTITY_FILE><REMOTE_USER>@<REMOTE_ADDRESS>:<REMOTE_DIR>
      • IDENTITY_FILE is the directory where the root certificate resides. The file access permission is 600.
      • REMOTE_USER is the ECS OS user.
      • REMOTE_ADDRESS is the ECS address.
      • REMOTE_DIR is the directory of the ECS to which the root certificate is uploaded.
    • In Windows, upload the root certificate using a remote connection tool.

  8. Connect to the instance in the directory where the MongoDB client is located.

    Method 1: Using the public network connection address

    Example command:

    ./mongo <Public network connection address> --ssl --sslCAFile <FILE_PATH> --sslAllowInvalidHostnames

    Parameter description:

    • Public Network Connection Address
      On the Instance Management page, click the instance name. The Basic Information page is displayed. Choose Connections in the navigation pane on the left, click the Public Connection tab, and obtain the address from Public Network Connection Address. The connection address format cannot be changed.
      Figure 3 Public Network Connection Address

      The format of the obtained public network connection address is as follows:

      mongodb://rwuser:<password>@192.168.xx.xx:8635/test?authSource=admin

      Pay attention to the following parameters in the public connection address:

      Table 1 Parameter description

      Parameter

      Description

      rwuser

      Account name, that is, the database username.

      <password>

      Password for the database username. Replace it with the actual password.

      If the password contains at signs (@),exclamation marks (!), or percent signs (%), replace them with hexadecimal URL codes %40, %21, and %25 respectively.

      For example, if the password is ****@ %***!, the corresponding URL code is **** %40%25*** %21.

      192.168.xx.xx:8635

      EIP and port number bound to the mongos node of the cluster instance

      authSource=admin

      authSource must be set to admin. The format of this address is fixed and cannot be changed.

    • FILE_PATH is the path for storing the root certificate.
    • --sslAllowInvalidHostnames: To ensure that the internal communication of the cluster does not occupy resources such as the user IP address and bandwidth, the cluster certificate is generated using the internal management IP address. --sslAllowInvalidHostnames is needed for the SSL connection through a public network.

    Command example:

    ./mongo mongodb://rwuser:<password>@192.168.xx.xx:8635/test?authSource=admin --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidHostnames

    Method 2: Connect to an instance using an EIP.

    Example command:

    ./mongo --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p --authenticationDatabaseadmin --ssl --sslCAFile <FILE_PATH> --sslAllowInvalidHostnames

    Parameter description:

    • DB_HOST is the EIP bound to the instance to be connected.

      You can click the instance name to go to the Basic Information page. In the navigation pane on the left, choose Connections. On the Public Connection tab, obtain the EIP bound to the mongos node in the EIP column.

      If there are multiple MongoDB nodes, the EIP of any node can be used to connect to the instance.

      Figure 4 Obtaining an EIP
    • DB_PORT is the port of the instance to be connected. The default port number is 8635.

      You can click the instance to go to the Basic Information page. In the navigation pane on the left, choose Connections. On the displayed page, click the Public Connection tab and obtain the port from the Database Port field in the Basic Information area.

      Figure 5 Obtaining the port
    • DB_USER is the database user. The default value is rwuser.
    • FILE_PATH is the path for storing the root certificate.
    • --sslAllowInvalidHostnames: To ensure that the internal communication of the cluster does not occupy resources such as the user IP address and bandwidth, the cluster certificate is generated using the internal management IP address. --sslAllowInvalidHostnames is needed for the SSL connection through a public network.

    Enter the database account password when prompted:

    Enter password:

    Command example:

    ./mongo --host 192.168.xx.xx --port 8635 -u rwuser -p --authenticationDatabase admin --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidHostnames

  9. Check the connection result. If the following information is displayed, the connection is successful.

    mongos>

Unencrypted Connection

If you connect to an instance over an unencrypted connection, disable SSL first. Otherwise, an error is reported. For details about how to disable SSL, see Enabling and Disabling SSL.

  1. Log in to the ECS.
  2. Connect to the instance in the directory where the MongoDB client is located.

    Method 1: Using the public network connection address

    Example command:

    ./mongo <Public network address>

    Parameter description:

    Public Network Connection Address: You can click the instance name to go to the Basic Information page. Choose Connections in the navigation pane on the left, click the Public Connection tab, and obtain the address from Public Network Connection Address. The connection address format cannot be changed.

    Figure 6 Public Network Connection Address

    Pay attention to the following parameters in the obtained public network connection address:

    Table 2 Parameter description

    Parameter

    Description

    rwuser

    Account name, that is, the database username.

    <password>

    Password for the database username. Replace it with the actual password.

    If the password contains at signs (@),exclamation marks (!), or percent signs (%), replace them with hexadecimal URL codes %40, %21, and %25 respectively.

    For example, if the password is ****@ %***!, the corresponding URL code is **** %40%25*** %21.

    192.168.xx.xx:8635

    EIP and port number bound to the mongos node of the cluster instance

    authSource=admin

    authSource must be set to admin. The format of this address is fixed and cannot be changed.

    Command example:

    ./mongo mongodb://rwuser:<password>@192.168.xx.xx:8635/test?authSource=admin

    Method 2: Using an EIP

    Example command:

    ./mongo --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p --authenticationDatabase admin

    Parameter description:

    • DB_HOST is the EIP bound to the instance to be connected.

      You can click the instance name to go to the Basic Information page. In the navigation pane on the left, choose Connections. On the Public Connection tab, obtain the EIP bound to the mongos node in the EIP column.

      If there are multiple MongoDB nodes, the EIP of any node can be used to connect to the instance.

      Figure 7 Obtaining an EIP
    • DB_PORT is the port of the instance to be connected. The default port number is 8635.

      You can click the instance to go to the Basic Information page. In the navigation pane on the left, choose Connections. On the displayed page, click the Public Connection tab and obtain the port from the Database Port field in the Basic Information area.

      Figure 8 Obtaining the port
    • DB_USER is the database user. The default value is rwuser.

    Enter the database account password when prompted:

    Enter password:

    Command example:

    ./mongo --host 192.168.xx.xx --port 8635 -u rwuser -p --authenticationDatabase admin

  3. Check the connection result. If the following information is displayed, the connection is successful.

    mongos>

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel