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
- 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.
- Install the MongoDB client.
- You have created and logged in to an ECS. For details, see Purchasing an ECS and Logging In to an ECS.
- 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.
- Log in to the management console.
- In the service list, choose Databases > GeminiDB Mongo API.
- On the Instance Management page, click the instance.
- In the navigation pane on the left, choose Connections.
- In the Basic Information area, click next to the SSL field to download the root certificate.
- 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.
- In Linux, run the following command:
- 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
- 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>
- The primary node of the replica set is connected.
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.
- Connect the ECS.
- 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
- 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>
- The primary node of the replica set is connected.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot