Connecting to Read Replicas Using Mongo Shell
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. DDS is compatible with MongoDB. Mongo Shell is a part of the MongoDB client. 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 to connect to a read replica over a private network.
You can connect to a read replica 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
- For details about how to create and log in to an ECS, see Purchasing an ECS and Logging In to an ECS.
- Install the MongoDB client on the ECS. To ensure successful authentication, install the MongoDB client of the same version as the target instance.
For details about how to install a MongoDB client, see How Can I Install a MongoDB Client?
- The ECS can communicate with the DDS instance. For details, see Configuring Security Group Rules.
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 Enabling and Disabling SSL.
- On the Instances page, click the instance name.
- In the navigation pane on the left, choose Connections.
- In the Basic Information area, click next to the SSL field.
- 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 Windows 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.
- In Linux, run the following command:
- Connect to a DDS instance. The DDS console provides the read replica connection address. You can use this address to connect to the read replica.
Example command:
./mongo "<Read replica connection address>" --ssl --sslCAFile <FILE_PATH> --sslAllowInvalidHostnames
Parameter description:
- Read Replica Connection Address: On the Instances page, click the instance to go to the Basic Information page. Choose Connections. Click the Private Connection tab. In the Address area, obtain the connection address of the read replica instance.
Figure 1 Obtaining the read replica connection address
The format of the read replica 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 read replica connection address:
Table 1 Parameter description Parameter
Description
rwuser
Account name, that is, the database username.
<password>
Password for the database account. Replace it with the actual password.
If the password contains at signs (@), exclamation marks (!), dollar signs ($), or percent signs (%), replace them with hexadecimal URL codes (ASCII) %40, %21, %24, and %25 respectively.
For example, if the password is ****@%***!$, the corresponding URL code is ****%40%25***%21%24.
192.168.xx.xx:8635
IP address and port of the read replica of the replica set instance.
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: The replica set certificate is generated using the internal management IP address to ensure that internal communication does not occupy resources such as the user IP address and bandwidth. --sslAllowInvalidHostnames is needed for the SSL connection through a private network.
Command example:
./mongo "mongodb://rwuser:<password>@192.168.xx.xx:8635/test?authSource=admin" --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidHostnames
When connecting to an instance using the read replica connection address, add double quotation marks (") before and after the connection information.
If the following information is displayed, the instance is successfully connected:replica:SECONDARY>
- Read Replica Connection Address: On the Instances page, click the instance to go to the Basic Information page. Choose Connections. Click the Private Connection tab. In the Address area, obtain the connection address of the read replica instance.
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.
- Log in to the ECS.
- Connect to a DDS instance. The DDS console provides the read replica connection address. You can use this address to connect to the read replica.
Example command:
./mongo "<Read replica connection address>"
Read Replica Connection Address: On the Instances page, click the instance to go to the Basic Information page. Choose Connections. Click the Private Connection tab. In the Address area, obtain the connection address of the read replica instance.
Figure 2 Obtaining the read replica connection address
The format of the read replica 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 HA address:
Table 2 Parameter description Parameter
Description
rwuser
Account name, that is, the database username.
<password>
Password for the database account. Replace it with the actual password.
If the password contains at signs (@), exclamation marks (!), dollar signs ($), or percent signs (%), replace them with hexadecimal URL codes (ASCII) %40, %21, %24, and %25 respectively.
For example, if the password is ****@%***!$, the corresponding URL code is ****%40%25***%21%24.
192.168.xx.xx:8635
IP address and port of the read replica of the replica set instance.
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"
If the following information is displayed, the instance is successfully connected:replica:SECONDARY>
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