Updated on 2022-09-20 GMT+08:00

Connecting to a Single Node Instance Using Mongo Shell (Private Network)

Mongo shell is the default client for the MongoDB database server. You can use Mongo Shell to connect to DB instances, and query, update, and manage data in databases. To use Mongo Shell, download and install the MongoDB client first, and then use the Mongo shell to connect to the DB instance.

By default, a DDS instance provides a private IP address. If your applications are deployed on an ECS and are in the same region and VPC as DDS instances, you can connect to DDS instances using a private IP address to achieve a fast transmission rate and high security.

This section describes how to use Mongo Shell installed on a Linux ECS to connect to a single node instance over a private 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. Install the MongoDB client on the ECS.

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

  2. The ECS can communicate with the DDS instance. For details, see ECS.

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 Instances 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 to the ECS to be connected to the instance.

    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 a DDS instance.

    Method 1: Private HA connection address (recommended)

    Example command:

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

    Parameter description:

    • Private HA Connection Address: On the Instances page, click the instance name. The Basic Information page is displayed. Choose Connections. Click the Private Connection tab and obtain the connection address of the current instance from the Private Network Connection Address field.
      Figure 1 Obtaining the private HA connection address

      The format of the private connection address is as follows. The database username rwuser and authentication database admin cannot be changed.

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

      Pay attention to the following parameters in the private network connection address:

      Table 1 Parameter description

      Parameter

      Description

      rwuser

      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 (ASCII) %40, %21, and %25 respectively.

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

      192.168.xx.xx:8635

      IP address and port of the single node instance to be connected

      test

      The name of the test database. You can set this parameter based on your service requirements.

      authSource=admin

      The authentication database of user rwuser must be admin. authSource=admin is fixed in the command.

    • FILE_PATH is the path for storing the root certificate.
    • --sslAllowInvalidHostnames: To ensure that the internal communication of the single nodes does not occupy resources such as the user IP address and bandwidth, the single node certificate is generated using the internal management IP address. --sslAllowInvalidHostnames is needed for the SSL connection over private networks.

    Command example:

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

    Method 2: Private HA connection (user-defined database and account)

    Example command:

    ./mongo <Private network HA connection address>

    Parameter description:

    • Private HA Connection Address: On the Instances page, click the instance name. The Basic Information page is displayed. Choose Connections. Click the Private Connection tab and obtain the connection address of the current instance from the Private HA Connection Address field.
      Figure 2 Obtaining the private HA connection address

      The format of the obtained private HA connection address is as follows:

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

      The following table lists the required parameters in the private HA address.

      Table 2 Parameter information

      Parameter

      Description

      rwuser

      Database username. The default value is rwuser. You can change the value to the username based on your service requirements.

      <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 (ASCII) %40, %21, and %25 respectively.

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

      192.168.xx.xx:8635

      IP address and port of the single node instance to be connected

      test

      The name of the test database. You can set this parameter based on your service requirements.

      authSource=admin

      The authentication database of user rwuser is admin.

      NOTE:

      If you use a user-defined database for authentication, change the authentication database in the HA connection address to the name of the user-defined database. In addition, replace rwuser with the username created in the user-defined database.

    • FILE_PATH is the path for storing the root certificate.
    • --sslAllowInvalidHostnames: To ensure that the internal communication of the single nodes does not occupy resources such as the user IP address and bandwidth, the single node certificate is generated using the internal management IP address. --sslAllowInvalidHostnames is needed for the SSL connection over private networks.

    For example, if you create a user-defined database Database and user test1 in the database, the connection command is as follows:

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

    Method 3: Using a private IP address

    Example command:

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

    Parameter description:

    • DB_HOST is the private IP address of the instance to be connected.

      On the Instances page, click the instance name. The Basic Information page is displayed. Choose Connections. On the Private Connection tab, obtain the IP address of the corresponding node. Obtaining the private IP address

    • DB_PORT is the database port. The default port number is 8635.

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

      Figure 3 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 single nodes does not occupy resources such as the user IP address and bandwidth, the single node certificate is generated using the internal management IP address. --sslAllowInvalidHostnames is needed for the SSL connection over private networks.

    Command example:

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

    Enter the database password when prompted:

    Enter password:

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

    replica:PRIMARY>

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 a DDS instance.

    Method 1: Private HA connection address (recommended)

    Example command:

    ./mongo <Private network HA connection address>

    Private Network Connection Address: On the Instances page, click the instance to switch to the Basic Information page. Choose Connections. Click the Private Connection tab and obtain the connection address of the current instance from the Private HA Connection Address field.

    Figure 4 Obtaining the private HA connection address

    The format of the private connection address is as follows. The database username rwuser and authentication database admin cannot be changed.

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

    Pay attention to the following parameters in the private network connection address:

    Table 3 Parameter description

    Parameter

    Description

    rwuser

    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 (ASCII) %40, %21, and %25 respectively.

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

    192.168.xx.xx:8635

    IP address and port of the single node instance to be connected

    test

    The name of the test database. You can set this parameter based on your service requirements.

    authSource=admin

    The authentication database of user rwuser must be admin. authSource=admin is fixed in the command.

    Command example:

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

    Method 2: Private HA connection (user-defined database and account)

    Example command:

    ./mongo <Private network HA connection address>

    Private HA Connection Address: On the Instances page, click the instance name. The Basic Information page is displayed. Choose Connections. Click the Private Connection tab and obtain the connection address of the current instance from the Private HA Connection Address field.

    Figure 5 Obtaining the private HA connection address

    The format of the obtained private HA connection address is as follows:

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

    The following table lists the required parameters in the private HA address.

    Table 4 Parameter information

    Parameter

    Description

    rwuser

    Database username. The default value is rwuser. You can change the value to the username based on your service requirements.

    <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 (ASCII) %40, %21, and %25 respectively.

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

    192.168.xx.xx:8635

    IP address and port of the single node instance to be connected

    test

    The name of the test database. You can set this parameter based on your service requirements.

    authSource=admin

    The authentication database of user rwuser is admin.

    NOTE:

    If you use a user-defined database for authentication, change the authentication database in the HA connection address to the name of the user-defined database. In addition, replace rwuser with the username created in the user-defined database.

    For example, if you create a user-defined database Database and user test1 in the database, the connection command is as follows:

    ./mongo mongodb://test1:<password>@192.168.xx.xx:8635/Database?authSource=Database

    Method 3: Using a private IP address

    Example command:

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

    Parameter description:

    • DB_HOST is the private IP address of the instance to be connected.

      On the Instances page, click the instance name. The Basic Information page is displayed. Choose Connections. On the Private Connection tab, obtain the IP address of the corresponding node. Obtaining the private IP address

    • DB_PORT is the database port. The default port number is 8635.

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

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

    Command example:

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

    Enter the database password when prompted:

    Enter password:

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

    replica:PRIMARY>