Updated on 2024-08-03 GMT+08:00

Connecting to a Replica Set Instance Using the MongoDB Client

If a DB instance cannot be accessed over a private network, you can bind an EIP to the instance.

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

A Linux ECS is used as an example.

The MongoDB client can connect to an instance with an unencrypted connection or an encrypted connection (SSL). To improve data transmission security, you are advised to connect to instances using the SSL connection.

Prerequisites

  1. You have bound an EIP to the instance and configured security group rules. In this way, you can access the instance from the ECS. For details, see Binding and Unbinding an EIP and Configuring Security Group Rules.
  2. Install the MongoDB client.
    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 indicates the OS user.
      • REMOTE_ADDRESS indicates the host address.
      • REMOTE_DIR indicates the directory to which the root certificate is uploaded.
    • In Windows, upload the root certificate using a remote connection tool.

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

    Example command:

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

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

    Enter password:
    Table 1 Parameter description

    Parameter

    Description

    <DB_HOST>

    EIP bound to the instance node 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 EIP of the corresponding node.

    Figure 1 Obtaining the EIP of a Node

    Alternatively, click the instance name and obtain the EIP of the corresponding node in the Node Information area.

    Figure 2 Viewing EIPs

    <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.1.6 --port 8635 -u rwuser -p --authenticationDatabase admin --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidHostnames

  8. Check the results. If the following information is displayed, the connection is successful.

    • 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 the ECS.
  2. Connect to the instance.

    Example command:

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

    Table 2 Parameter description

    Parameter

    Description

    <DB_HOST>

    EIP 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 EIP of the corresponding node from the node information list.

    Figure 3 Obtaining an EIP

    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 EIP of the node.

    Figure 4 Obtaining the EIP of a node

    <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

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

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