Connecting to a Cluster Instance Using Mongo Shell (Public Network)
Scenarios
In the following scenarios, you can access a DDS instance from the Internet by binding a public gateway or an EIP to the instance.
Scenario 1: If your application is running on an ECS that is in a different region from the one where the DDS instance is located, connect to the DDS instance using an EIP.
Scenario 2: If your application is deployed on a cloud server provided by other vendors, connect to the DDS instance using an EIP.
This section describes how to use Mongo Shell to connect to a cluster instance over a public network.
You can connect to a cluster 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.
- You have bound a public gateway or an EIP to the cluster instance and configured security group rules to ensure that the instance can be accessed through the EIP.
- You have installed a MongoDB client on the ECS.
For details about how to install a 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 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 obtained in 6 to the ECS.
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 the instance in the directory where the MongoDB client is located.
- Method 1: Using a public network connection address
./mongo <Public network connection address> --ssl --sslCAFile <FILE_PATH> --sslAllowInvalidHostnames
The parameters are explained as follows:
- 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. On the displayed page, click the Public Connection tab. Find the Address label and obtain the address next to Public Network Connection Address. Figure 3 Obtaining the public network connection address
The format of the public network connection address is provided below. It uses a fixed account and authentication database. Therefore, the username rwuser and the authentication database admin cannot be changed.
mongodb://rwuser:<password>@192.168.xx.xx:8635/test?authSource=admin
Configure the parameters in the address as described in the table below.
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
EIP and port bound to the dds mongos node of the cluster 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 where the root certificate is stored.
- --sslAllowInvalidHostnames: To ensure that the internal communication of the cluster does not occupy resources such as the user IP address and bandwidth, the cluster certificate is generated using the internal management IP address. --sslAllowInvalidHostnames is needed when you use SSL to connect to the cluster instance through a public network.
Example command:
./mongo mongodb://rwuser:<password>@192.168.xx.xx:8635/test?authSource=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. On the displayed page, click the Public Connection tab. Find the Address label and obtain the address next to Public Network Connection Address.
- Method 2: Using an EIP
./mongo --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p --authenticationDatabase admin --ssl --sslCAFile <FILE_PATH> --sslAllowInvalidHostnames
The parameters are explained as follows:
- DB_HOST is the EIP bound to the target instance node.
On the Instances page, click the target instance name. In the navigation tree, choose Connections. On the displayed page, click the Public Connection tab and find the EIP of the target node.
If there are multiple dds mongos nodes, obtain the EIP of any node to connect to the instance.
Figure 4 Obtaining the EIP
- DB_PORT is the port of the instance to be connected. The default port number is 8635.
You can click the instance name 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 5 Obtaining the port
- DB_USER is the database user. The default value is rwuser.
- FILE_PATH is the path where the root certificate is stored.
- --sslAllowInvalidHostnames: To ensure that the internal communication of the cluster does not occupy resources such as the user IP address and bandwidth, the cluster certificate is generated using the internal management IP address. --sslAllowInvalidHostnames is needed when you use SSL to connect to the cluster instance through a public network.
Enter the database account password when the following prompt appears:
Enter password:
Example command:
./mongo --host 192.168.xx.xx --port 8635 -u rwuser -p --authenticationDatabase admin --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidHostnames
- DB_HOST is the EIP bound to the target instance node.
- Method 3: Using the EIP of a public gateway
./mongo --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p --authenticationDatabase admin --ssl --sslCAFile <FILE_PATH> --sslAllowInvalidHostnames
The parameters are explained as follows:
- DB_HOST is the EIP of the public gateway bound to the instance to be connected.
You can click the instance name to go to the Basic Information page. In the navigation pane on the left, choose Connections. On the Public Connection tab, obtain the EIP of the public gateway bound to the dds mongos node in the Public Gateway column.
If there are multiple dds mongos nodes, the EIP of the public gateway of any node can be used to connect to the instance.
Figure 6 Obtaining the EIP of the public gateway
- DB_PORT is the port of the public gateway. Figure 7 Port of the public gateway
- DB_USER is the database account. The default value is rwuser.
- FILE_PATH is the path where the root certificate is stored.
- --sslAllowInvalidHostnames: To ensure that the internal communication of the cluster does not occupy resources such as the user IP address and bandwidth, the cluster certificate is generated using the internal management IP address. --sslAllowInvalidHostnames is needed when you use SSL to connect to the cluster instance through a public network.
Enter the database account password when the following prompt appears:
Enter password:
Example command:
./mongo --host 192.168.xx.xx --port 8635 -u rwuser -p --authenticationDatabase admin --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidHostnames
- DB_HOST is the EIP of the public gateway bound to the instance to be connected.
- Method 1: Using a public network connection address
- Check the connection result. If the following information is displayed, the connection is successful.
mongos>
Unencrypted Connection
If you connect to an instance without SSL, ensure that SSL is disabled. Otherwise, an error will be reported. For details about how to disable SSL, see Enabling and Disabling SSL.
- Log in to the ECS.
- Connect to the instance in the directory where the MongoDB client is located.
- Method 1: Using a public network connection address
./mongo <Public network address>
Public Network Connection Address: You can click the instance name 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. In the Address area, obtain the instance connection address from the Public Network Connection Address field.
Figure 8 Obtaining the public network connection address
The format of the public network connection address is provided below. It uses a fixed account and authentication database. Therefore, the username rwuser and the authentication database admin cannot be changed.
mongodb://rwuser:<password>@192.168.xx.xx:8635/test?authSource=admin
Configure the parameters in the address as described in the table below.
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 ($), 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
EIP and port bound to the dds mongos node of the cluster 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.
Example command:
./mongo mongodb://rwuser:<password>@192.168.xx.xx:8635/test?authSource=admin
- Method 2: Using an EIP (Not Recommended)
./mongo --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p --authenticationDatabase admin
The parameters are explained as follows:
- DB_HOST is the EIP bound to the instance to be connected.
You can click the instance name to go to the Basic Information page. In the navigation pane on the left, choose Connections. On the Public Connection tab, obtain the EIP bound to the dds mongos node in the EIP column.
If there are multiple dds mongos nodes, the EIP of any node can be used to connect to the instance.
Figure 9 Obtaining an EIP
- DB_PORT is the port of the instance to be connected. The default port number is 8635.
You can click the instance name 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 10 Obtaining the port
- DB_USER is the database account. The default value is rwuser.
Enter the database account password when the following prompt appears:
Enter password:
Example command:
./mongo --host 192.168.xx.xx --port 8635 -u rwuser -p --authenticationDatabase admin
- DB_HOST is the EIP bound to the instance to be connected.
- Method 3: Using the EIP of a public gateway
./mongo --host <DB_HOST> --port <DB_PORT> -u <DB_USER> -p --authenticationDatabase admin
The parameters are explained as follows:
- DB_HOST is the EIP of the public gateway bound to the instance to be connected.
You can click the instance name to go to the Basic Information page. In the navigation pane on the left, choose Connections. On the Public Connection tab, obtain the EIP of the public gateway bound to the dds mongos node in the Public Gateway column.
If there are multiple dds mongos nodes, the EIP of the public gateway of any node can be used to connect to the instance.
Figure 11 Obtaining the EIP of the public gateway
- DB_PORT is the port of the public gateway. Figure 12 Port of the public gateway
- DB_USER is the database account. The default value is rwuser.
Enter the database account password when the following prompt appears:
Enter password:
Example command:
./mongo --host 192.168.xx.xx --port 8635 -u rwuser -p --authenticationDatabase admin
- DB_HOST is the EIP of the public gateway bound to the instance to be connected.
- Method 1: Using a public network connection address
- Check the connection result. If the following information is displayed, the connection is successful.
mongos>
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