Connecting to a Replica Set Instance Using Mongo Shell (Private Network)
Scenarios
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 Mongo Shell to connect to a DB instance.
By default, a DDS instance provides a private IP address. If your applications are deployed on an ECS that is in the same region and VPC as your DDS instance, you can connect to the DDS instance 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 replica set instance over a private network.
You can connect to a replica set 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
- 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.
Procedure
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.
- Log in to the management console.
- Click
in the upper left corner and select a region and a project. - Click
in the upper left corner of the page and choose Databases > Document Database Service. - On the Instances page, click the instance name.
- In the navigation pane, 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 that needs to connect to the instance.
Choose the corresponding upload method based on whether the ECS runs Linux or Windows.
- In Linux, run the following command: scp<IDENTITY_FILE><REMOTE_USER>@<REMOTE_ADDRESS>:<REMOTE_DIR>
- IDENTITY_FILE indicates the directory where the root certificate is stored. Ensure that the directory's access permission is set to 600.
- REMOTE_USER is the ECS OS user.
- REMOTE_ADDRESS is the ECS address.
- REMOTE_DIR is the directory on the ECS where 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 instance.
Method 1: Using the private HA connection address (recommended)
DDS provides the HA connection address. Using this address to connect to a replica set instance improves data read/write performance and prevents write errors on the client after a primary/secondary switchover.
Example command:
./mongo "<Private 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 HA Connection Address field. Figure 1 Obtaining the private HA connection address
The format of the private HA connection address is as follows. The database username rwuser and authentication database admin cannot be changed.
mongodb://rwuser:<password>@192.168.xx.xx:8635,192.168.xx.xx:8635/test?authSource=admin&replicaSet=replica
The following table lists the parameters in the private HA 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 ($), percent signs (%), or plus signs (+), replace them with hexadecimal URL codes (ASCII) %40, %21, %24, %25, and %2B respectively.
For example, if the password is ****@%***!$+, the corresponding URL code is ****%40%25***%21%24%2B.
192.168.xx.xx:8635,192.168.xx.xx:8635
IP addresses and ports of the nodes of the replica set instance to be connected.
test
The name of the test database. You can set this parameter based on your service requirements.
authSource=admin&replicaSet=replica
- The authentication database of user rwuser must be admin. authSource=admin is fixed in the command.
- replica in replicaSet=replica is the name of a replica set. The default replica set of Huawei Cloud DDS is replica.
- FILE_PATH is the path where the root certificate is stored.
- --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. You must add --sslAllowInvalidHostnames when connecting to a replica set instance over a private network using SSL.
Command example:
./mongo "mongodb://rwuser:<password>@192.168.xx.xx:8635,192.168.xx.xx:8635/test?authSource=admin&replicaSet=replica" --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidHostnames
- If you connect to an instance over a private HA address, add double quotation marks before and after the connection information.
- For details about the HA connection, see Connecting to a Replica Set Instance for Read and Write Separation and High Availability.
If the following information is displayed, the instance is successfully connected:replica:PRIMARY>
Run the following command to access the local database:
use local
Information similar to the following is displayed:
switched to db local
Run the following command to query replica set oplog:
db.oplog.rs.find()
Method 2: Using the private HA connection address (user-defined database and account)
Example command:
./mongo "<Private 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 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,192.168.xx.xx:8635/test?authSource=admin&replicaSet=replica
Pay attention to the following 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 (!), dollar signs ($), percent signs (%), or plus signs (+), replace them with hexadecimal URL codes (ASCII) %40, %21, %24, %25, and %2B respectively.
For example, if the password is ****@%***!$+, the corresponding URL code is ****%40%25***%21%24%2B.
192.168.xx.xx:8635,192.168.xx.xx:8635
IP addresses and ports of the nodes of the replica set instance to be connected.
test
The name of the test database. You can set this parameter based on your service requirements.
authSource=admin&replicaSet=replica
- The authentication database of user rwuser is admin.
- In replica in replicaSet=replica, replica indicates that the instance type is replica set and the format cannot be changed.
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 where the root certificate is stored.
- --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. You must add --sslAllowInvalidHostnames when connecting to a replica set instance over a private network using SSL.
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,192.168.xx.xx:8635/Database?authSource=Database&replicaSet=replica" --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidHostnames
Method 3: Connect to a single node.
You can also use the private IP address of a primary or secondary node to access the replica set instance. This method affects the read/write performance when a primary/secondary switchover occurs.
Example command:
./mongo --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p --authenticationDatabase admin --ssl --sslCAFile<FILE_PATH> --sslAllowInvalidHostnames
Parameter description:
Table 3 Parameters Parameter
Description
DB_HOST
Private IP address of the primary or secondary node of the instance to be connected.
If the primary node is accessed, you can read from and write to the instance.
If a secondary node is accessed, you can only read from the instance.
On the Instances page, click the instance name to go to the Basic Information page. In the navigation pane, choose Connections. On the Private Connection tab page, obtain the IP address of the corresponding node.
Figure 3 Obtaining the IP address of a node
DB_PORT
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, choose Connections. Locate the Private Connection tab and obtain the port number next to the Database Port field in the Basic Information area.
Figure 4 Obtaining the port number
DB_USER
Database account. The default value is rwuser.
FILE_PATH
Path where the root certificate is stored.
--sslAllowInvalidHostnames
To ensure that internal communication within the replica set does not consume user IP addresses or bandwidth resources, the replica set instance's certificate is generated using the internal management IP address. You must add --sslAllowInvalidHostnames when connecting to a replica set instance over a private network using SSL.
Enter the database account password when prompted:
Enter password:
Command example:
./mongo --host 192.168.xx.xx --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 secondary node of the replica set is connected.
replica:SECONDARY>
- 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.
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.
Method 1: Using the private HA connection address (recommended)
DDS provides the HA connection address. Using this address to connect to a replica set instance improves data read/write performance and prevents errors from being reported when data is written from the client after a primary/secondary switchover.
Example command:
./mongo "<Private 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 private HA connection address is as follows. The database username rwuser and authentication database admin cannot be changed.
mongodb://rwuser:<password>@192.168.xx.xx:8635,192.168.xx.xx:8635/test?authSource=admin&replicaSet=replica
The following table lists the parameters in the private HA connection address.
Table 4 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 ($), percent signs (%), or plus signs (+), replace them with hexadecimal URL codes (ASCII) %40, %21, %24, %25, and %2B respectively.
For example, if the password is ****@%***!$+, the corresponding URL code is ****%40%25***%21%24%2B.
192.168.xx.xx:8635,192.168.xx.xx:8635
IP addresses and ports of the nodes of the replica set instance to be connected.
test
The name of the test database. You can set this parameter based on your service requirements.
authSource=admin&replicaSet=replica
- The authentication database of user rwuser must be admin. authSource=admin is fixed in the command.
- replica in replicaSet=replica is the name of a replica set. The default replica set of Huawei Cloud DDS is replica.
Command example:
./mongo "mongodb://rwuser:<password>@192.168.xx.xx:8635,192.168.xx.xx:8635/test?authSource=admin&replicaSet=replica"
If the following information is displayed, the instance is successfully connected:replica:PRIMARY>
Run the following command to access the local database:
use local
Information similar to the following is displayed:
switched to db local
Run the following command to query replica set oplog:
db.oplog.rs.find()
Method 2: Using the private HA connection address (user-defined database and account)
Example command:
./mongo "<Private 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 6 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,192.168.xx.xx:8635/test?authSource=admin&replicaSet=replica
Pay attention to the following parameters in the private HA address:
Table 5 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 (!), dollar signs ($), percent signs (%), or plus signs (+), replace them with hexadecimal URL codes (ASCII) %40, %21, %24, %25, and %2B respectively.
For example, if the password is ****@%***!$+, the corresponding URL code is ****%40%25***%21%24%2B.
192.168.xx.xx:8635,192.168.xx.xx:8635
IP addresses and ports of the nodes of the replica set instance to be connected.
test
The name of the test database. You can set this parameter based on your service requirements.
authSource=admin&replicaSet=replica
- The authentication database of user rwuser is admin.
- In replica in replicaSet=replica, replica indicates that the instance type is replica set and the format cannot be changed.
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,192.168.xx.xx:8635/Database?authSource=Database&replicaSet=replica"
Method 3: Connect to a single node.
You can also use the private IP address of a primary or secondary node to access the replica set instance. This method affects the read/write performance when a primary/secondary switchover occurs.
Example command:
./mongo --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p --authenticationDatabase admin
Parameter description:
Table 6 Parameters Parameter
Description
DB_HOST
Private IP address of the primary or secondary node of the instance to be connected.
If the primary node is accessed, you can read from and write to the instance.
If a secondary node is accessed, you can only read from the instance.
On the Instances page, click the instance name to go to the Basic Information page. In the navigation pane, choose Connections. On the Private Connection tab page, obtain the IP address of the corresponding node.
Figure 7 Obtaining the IP address of a node
DB_PORT
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, choose Connections. Locate the Private Connection tab and obtain the port number next to the Database Port field in the Basic Information area.
Figure 8 Obtaining the port number
DB_USER
Database account. The default value is rwuser.
Command example:
./mongo --host 192.168.xx.xx --port 8635 -u rwuser -p --authenticationDatabase admin
Enter the database account password when prompted:
Enter password:
If the following information is displayed, the corresponding node is successfully connected:
- The primary node of the replica set is connected.
replica:PRIMARY>
- The secondary node of the replica set is connected.
replica:SECONDARY>
Helpful Links
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