Updated on 2024-05-30 GMT+08:00

Connecting to a Replica Set Instance over a Private Network

If your applications are deployed on an ECS that is in the same region and VPC as your instances, you can connect to the DB instance over a private network.

This section describes how to use the MongoDB client to connect to a GeminiDB Mongo replica set instance over a private network.

The following uses a Linux ECS and a Window client as an example.

The MongoDB client can connect to an instance with an unencrypted connection or an encrypted connection (SSL). To secure data transmission, connect to your instances using SSL.

Precautions

  • The instances must be in the same VPC and subnet as the ECS.
  • The ECS must be in a security group that has access to the instances.
    • If the instance is associated with the default security group, you do not need to configure security group rules.
    • If the instance is not associated with the default security group, check whether the security group rules allow the ECS to connect to the instance.

      If yes, the ECS can connect to the instance.

      If no, add an inbound rule to the security group. For details, see Configuring Security Group Rules.

Prerequisites

  1. You have created and logged in to an ECS. For details, see Purchasing an ECS and Logging In to an ECS.
  2. You have installed the MongoDB client on the ECS.

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

SSL Connection

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 Configuring an SSL Connection.

  1. Log in to the management console.
  2. In the service list, choose Databases > GeminiDB Mongo API.
  3. On the Instance Management page, click the instance.
  4. In the navigation pane on the left, choose Connections.
  5. In the Basic Information area, click next to the SSL field to download the root certificate.
  6. 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 a 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.

  7. Connect to a DB instance.

    • Method 1: Use the connection address to connect to a replica set instance (recommended).

      The connection addresses provided on the GeminiDB Mongo console includes the IP addresses and ports of the primary node, standby node, and hidden node by default. You can use this connection address to connect to the primary, standby, and hidden nodes of the instance at the same time. In this way, write errors can be avoided after a primary/standby switchover.

      Example command:

      ./mongo "<Connection address>" --ssl --sslCAFile<FILE_PATH> --sslAllowInvalidHostnames

      Table 1 Parameter description

      Parameter

      Description

      <Connection address>

      Connection address of the instance to be connected.

      On the Instance Management page, click the instance to go to the Basic Information page. Obtain the private IP address on the Connections page.

      Figure 1 Obtaining the private IP address
      • In the preceding command, ***** needs to be replaced with the password of the instance administrator.

        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.

      • replica in replicaSet=replica is the name of a replica set. The name of GeminiDB Mongo replica set is fixed and cannot be changed.

      <FILE_PATH>

      Path for storing the root certificate.

      Example:

      ./mongo "mongodb://rwuser:****@192.168.0.252:8635,192.168.0.31:8635/test?authSource=admin&replicaSet=replica" --ssl --sslCAFile/tmp/ca.crt --sslAllowInvalidHostnames

      • A replica set instance uses the management IP address to generate SSL certificate. --sslAllowInvalidHostnames is required when you connect to a replica set over a private network.
      • If you connect to an instance over a private IP address, add double quotation marks before and after the connection information.
      If the following information is displayed, the instance is successfully connected:
      replica:PRIMARY>
    • Method 2: Connect to a single node.

      Example command:

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

      Enter the password of the database account if the following information is displayed:

      Enter password:
      Table 2 Parameter description

      Parameter

      Description

      <DB_HOST>

      Private IP address of the primary or standby node of the instance to be connected.

      Primary node: You can read and write data.

      Secondary node: You can only read data.

      On the Instance Management page, click the instance to go to the Basic Information page. On the Connections page, obtain the private IP address of the corresponding node.

      Figure 2 Obtaining the node IP address

      Alternatively, obtain the private IP address of a node in the Node Information area on the Basic Information page.

      Figure 3 Obtaining the private IP address

      <DB_PORT>

      Database port

      <DB_USER>

      Username of the instance administrator. The default value is rwuser.

      <FILE_PATH>

      Path for storing the root certificate.

      Example:

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

      If the following information is displayed, the corresponding node is successfully connected:

      • The primary node of the replica set is connected.
        replica:PRIMARY>
      • The standby node of the replica set is connected.
        replica:SECONDARY>

Non-SSL 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 Configuring an SSL Connection.

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

    • Use the connection address to connect to a replica set instance (recommended).

      The connection addresses provided on the GeminiDB Mongo console includes the IP addresses and ports of the primary node, standby node, and hidden node by default. Connecting to a replica set with this method allows access to the primary, standby, and hidden nodes at the same time, so write errors can be avoided after a primary/standby switchover.

      Example command:

      ./mongo "<Connection address>"

      Table 3 Parameter description

      Parameter

      Description

      <Connection address>

      You can obtain the connection address in either of the following ways:

      Click the instance name. On the displayed Basic Information page, the address is displayed in the Address field in the Network Information area.

      Figure 4 Obtaining the connection address

      Alternatively, click the instance name to go to the Basic Information page. In the navigation pane on the left, choose Connections to obtain the address of the instance.

      Figure 5 Obtaining the connection address
      • In the preceding command, ***** needs to be replaced with the password of the instance administrator.

        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.

      • replica in replicaSet=replica is the name of a replica set. The name of GeminiDB Mongo replica set is fixed and cannot be changed.

      Example:

      ./mongo "mongodb://rwuser:****@192.168.0.196:8635,192.168.0.67:8635,192.168.0.32:8635/test?authSource=admin&replicaSet=replica"

      If the following information is displayed, the connection is successful.
      replica:PRIMARY>
    • Connecting to a single node.

      You can also connect to the private IP address of a specified node.

      Example command:

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

      Table 4 Parameter description

      Parameter

      Description

      <DB_HOST>

      Private IP address of the primary or standby node of the instance to be connected.

      • Primary node: You can read and write data.
      • Secondary node: You can only read data.

      On the Instance Management page, click the instance to go to the Basic Information page. Obtain the private IP address of the corresponding node from the node information list.

      Figure 6 Obtaining the private IP address

      Alternatively, click the target instance to go to the Basic Information page. In the navigation pane on the left, choose Connections. On the Connections page, obtain the private IP address of the node.

      Figure 7 Obtaining the private IP address

      <DB_PORT>

      Database port

      <DB_USER>

      Username of the instance administrator. The default value is rwuser.

      Enter the password of the database account if the following information is displayed:

      Enter password:

      Example:

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

      If the following information is displayed, the corresponding node is successfully connected:

      The command output for connecting to the primary node of the replica set is as follows:

      replica:PRIMARY>

      The command output for connecting to the standby node of the replica set is as follows:

      replica:SECONDARY>