All Products
Search
Document Center

ApsaraDB for MongoDB:Connect to an ApsaraDB for MongoDB instance over the Internet

Last Updated:Jul 03, 2024

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.

  1. Query the public IP address of the local client.

  2. 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.

  3. 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.

    Note

    You 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.

  1. 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.

    Warning

    If 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.

  2. 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.

  3. 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

    客户端IP查询

    Note

    If 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})
  4. 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.