This topic describes how to connect a local client to an ApsaraDB for MongoDB instance over the Internet.
Prerequisites
A public endpoint is applied for the ApsaraDB for MongoDB instance. For more information, see the following topics:
Usage notes
Read this topic only if you want to connect a local client to an ApsaraDB for MongoDB instance. If you want to connect an Elastic Compute Service (ECS) instance to an ApsaraDB for MongoDB instance, you can obtain the public and private IP addresses of the ECS instance from the ECS instance details page in the ECS console.
If you connect a local client to an ApsaraDB for MongoDB instance over the Internet, security risks may arise. We recommend that you connect an ECS instance to an ApsaraDB for MongoDB instance.
Method 1: Query an IP address library to obtain the public IP address of the local client and connect to an ApsaraDB for MongoDB instance
You can query an IP address library to obtain the public IP address of the local client and connect to an ApsaraDB for MongoDB instance.
Query the public IP address of the local client.
Add the returned public IP address to an IP address whitelist of the ApsaraDB for MongoDB instance. For more information, see Modify an IP address whitelist of an ApsaraDB for MongoDB instance.
Log on to the ApsaraDB for MongoDB instance by using the mongo shell on the local client. For more information, see Connect to an ApsaraDB for MongoDB instance.
NoteYou can also log on to the ApsaraDB for MongoDB instance by using other client tools.
After you add the public IP address of the local client to the IP address whitelist of the ApsaraDB for MongoDB instance, you may fail to connect to the instance. However, after you add the IP address 0.0.0.0/0 to the IP address whitelist, you can connect to the instance. In this case, we recommend that you query the connection information to obtain the public IP address of the local client. For more information, see Method 2: Query the connection information to obtain the public IP address of the local client and connect to an ApsaraDB for MongoDB instance.
Method 2: Query the connection information to obtain the public IP address of the local client and connect to an ApsaraDB for MongoDB instance
You can query the connection information to obtain the public IP address of the local client and connect to an ApsaraDB for MongoDB instance.
Add the IP address 0.0.0.0/0 to an IP address whitelist of the ApsaraDB for MongoDB instance. For more information, see Modify an IP address whitelist of an ApsaraDB for MongoDB instance.
WarningIf you add the IP address 0.0.0.0/0 to the IP address whitelist of the ApsaraDB for MongoDB instance, all servers are allowed to access the instance. In this case, security risks may occur. Exercise caution when you add the IP address 0.0.0.0/0 to the IP address whitelist. If you no longer require the IP address 0.0.0.0/0, remove the IP address from the IP address whitelist.
Log on to the ApsaraDB for MongoDB instance by using the mongo shell on the local client. For more information, see Connect to an ApsaraDB for MongoDB instance.
Run the following command to query information about the client where you log on to the ApsaraDB for MongoDB instance:
db.currentOp({"appName" : "MongoDB Shell","active" : true})
Sample result
NoteIf you log on to the ApsaraDB for MongoDB instance by using other methods, you can run the following command to query information about all clients:
db.runCommand({currentOp: 1, "active" : true})
Add the IP address obtained in the preceding step to an IP address whitelist of the ApsaraDB for MongoDB instance, and then remove the IP address 0.0.0.0/0 added in Step 1 from the IP address whitelist.
References
If the public IP address of the local client frequently changes, you can connect to an ApsaraDB for MongoDB instance by using an SSL-VPN tunnel. For more information, see Connect to an ApsaraDB for MongoDB instance by using an SSL-VPN tunnel.