Connecting to a Replica Set Instance Using Mongo Shell (Public Network)
In the following scenarios, you can access a DDS instance from the Internet by binding an EIP to the instance.
Scenario 1: Your applications are deployed on an ECS and are not in the same region as the DDS instance.
Scenario 2: Your applications are deployed on a cloud server provided by other vendors.
This section describes how to use Mongo Shell to connect to a replica set instance through an EIP.
You can connect to an 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.
- Bind an EIP to the replica set instance and configure security group rules to ensure that the replica set instance can be accessed from an ECS.
- Install the MongoDB client on the ECS.
For details about how to install a MongoDB client, see How Can I Install a MongoDB Client?
The version of the installed MongoDB client must be the same as the instance version.
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.
- 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 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 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.
- In Linux, run the following command:
- Connect to the instance in the directory where the MongoDB client is located.
Method 1: Using a public network connection address
Example command:
./mongo "<Public network connection address>" --ssl --sslCAFile<FILE_PATH> --sslAllowInvalidHostnames
Parameter description:
- Public Network Connection Address: On the Instances page, click the instance to switch to the Basic Information page. In the navigation pane on the left, choose Connections. Click the Public Connection tab and obtain the public network connection address.
The format of the public 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 public network 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 (!), or percent signs (%), replace them with hexadecimal URL codes (ASCII) %40, %21, and %25 respectively.
For example, if the password is ****@ %***!, the corresponding URL code is **** %40%25*** %21.
192.168.xx.xx:8635
The EIP and port bound to the node of the replica set instance.
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 public network.
Command example:
./mongo "mongodb://rwuser:<password>@192.168.xx.xx:8635/test?authSource=admin" --ssl --sslCAFile/tmp/ca.crt --sslAllowInvalidHostnames- If you connect to an instance over a public HA address, add double quotation marks before and after the connection information.
- To improve read and write performance and prevent errors from being reported when data is written from the client after a primary/standby switchover. For details about how to connect to an instance in HA mode, see Connecting to a Replica Set Instance for Read and Write Separation and High Availability.
Method 2: Using an EIP
Example command:
./mongo --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p --authenticationDatabaseadmin --ssl --sslCAFile<FILE_PATH> --sslAllowInvalidHostnames
Parameter description:
- DB_HOST is the EIP bound to the instance node to be connected.
On the Instances page, click the instance to go to the Basic Information page. Choose Connections> Public Connection and obtain the EIP of the corresponding node.
- DB_PORT is the database port. The default port number is 8635.
You can click the instance to go to the Basic Information page. In the navigation pane on the left, choose Connections. On the displayed page, click the Public Connection tab and obtain the port from the Database Port field in the Basic Information area.
Figure 3 Obtaining the port
- DB_USER is the database user. The default value is rwuser.
- 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 public network.
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
- Public Network Connection Address: On the Instances page, click the instance to switch to the Basic Information page. In the navigation pane on the left, choose Connections. Click the Public Connection tab and obtain the public network connection address.
- Check the connection result. 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.
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.
Method 1: Using a public network connection address
Example command:
./mongo "<Public network address>"
Public Network Connection Address: On the Instances page, click the instance to switch to the Basic Information page. In the navigation pane on the left, choose Connections. Click the Public Connection tab and obtain the public network connection address.
The format of the public 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 public connection 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 (!), or percent signs (%), replace them with hexadecimal URL codes (ASCII) %40, %21, and %25 respectively.
For example, if the password is ****@ %***!, the corresponding URL code is **** %40%25*** %21.
192.168.xx.xx:8635
The EIP and port bound to the node of the replica set instance.
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 you connect to an instance over a public HA address, add double quotation marks before and after the connection information.
- To improve read and write performance and prevent errors from being reported when data is written from the client after a primary/standby switchover. you are advised to connect to an instance using the HA connection address. For details, see Connecting to a Replica Set Instance for Read and Write Separation and High Availability.
Method 2: Using an EIP
Example command:
./mongo --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p --authenticationDatabase admin
Parameter description:
- DB_HOST is the EIP bound to the instance node to be connected.
On the Instances page, click the instance to go to the Basic Information page. Choose Connections> Public Connection and obtain the EIP of the corresponding node.
- DB_PORT is the database port. The default port number is 8635.
You can click the instance to go to the Basic Information page. In the navigation pane on the left, choose Connections. On the displayed page, click the Public Connection tab and obtain the port from the Database Port field in the Basic Information area.
Figure 4 Obtaining the port
- DB_USER is the database user. The default value is rwuser.
Enter the database account password when prompted:
Enter password:
Command example:
./mongo --host 192.168.xx.xx --port 8635 -u rwuser -p --authenticationDatabase admin
- Check the connection result. 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.