edit-icon download-icon

Connect to a database through the Internet

Last Updated: May 14, 2018

Prerequisites

To access an ApsaraDB for Redis instance from a local PC to operate data, configure the port forwarding on ECS. However, the following prerequisites must be met:

  • If the ApsaraDB for Redis instance is in a VPC, ECS and the ApsaraDB for Redis instance must be in the same VPC.

  • If the ApsaraDB for Redis instance is in a classic network, ECS and the ApsaraDB for Redis instance must be in the same node (region).

  • If an IP address whitelist is configured for the ApsaraDB for Redis instance, the ECS Intranet address must be added to the whitelist.

ECS Windows

Currently, ApsaraDB for Redis is accessible through ECS Intranet. To locally access ApsaraDB for Redis through a public network, perform port mapping using netsh on the ECS Windows server.

  1. Log on to the ECS Windows server and run the following command in CMD:

    1. netsh interface portproxy add v4tov4 listenaddress=ECS public IP address listenport=6379 connectaddress=connection address of ApsaraDB for Redis connectport=6379

    To view all port forwarding rules on the server, run netsh interface portproxy show all.

  2. Perform a verification test locally after configuration is complete.

    Local verification

    1. Run redis-cli locally to connect to the ECS Windows server. For example, if the IP address of the ECS Windows server is 1.1.1.1, you can telnet to 1.1.1.1 6379.

    2. After the ECS Windows server is connected, enter the password to connect to ApsaraDB for Redis: auth Redis connection password.

    3. Write data and perform query and verification.

      After performing the preceding steps, you can use a local PC or server to connect to port 6379 of the ECS Windows server through a public network and access ApsaraDB for Redis.

      Note: As portproxy is provided by Microsoft rather than open source software, read the netsh documentation on portproxy or consult Microsoft engineers if you have any problems in the configuration or usage process. Alternatively, use another scheme, for example, use portmap to configure proxy mappings.

  3. After finishing related operations, you can run netsh interface portproxy delete v4tov4 listenaddress=ECS public IP address listenport=6379 to delete the public network forwarding rule.

ECS Linux

Currently, ApsaraDB for Redis is accessible through ECS Intranet. To locally access ApsaraDB for Redis through a public network, install rinetd on the ECS Linux server to perform forwarding.

  1. Install rinetd on the ECS Linux server.

    1. wget http://www.boutell.com/rinetd/http/rinetd.tar.gz&&tar -xvf rinetd.tar.gz&&cd rinetd
    2. sed -i 's/65536/65535/g' rinetd.c (Modify the port range.)
    3. mkdir /usr/man&&make&&make install

    Note: The rinetd installation package obtained from the download URL may be unavailable. You can find and download the rinetd installation package from other sources.

  2. Open the configuration file rinetd.conf.

    1. vi /etc/rinetd.conf
  3. Add the following content to the configuration file:

    1. 0.0.0.0 6379 Connection address of port 6379 of ApsaraDB for Redis
    2. logfile /var/log/rinetd.log

    Note: You can run cat /etc/rinetd.conf to check whether the configuration file is correctly modified.

  4. Run the following command to start rinetd.

    1. rinetd

    Notes:

    • You can run echo rinetd >>/etc/rc.local to set auto startup for rinetd.

    • If a binding error is reported, run pkill rinetd to terminate the process and run rinetd to start the rinetd process.

    • After rinetd is started normally, run netstat -anp | grep 6379 to check whether the service works properly.

    netstat verification

  5. Perform a verification test locally.

    1. You can run redis-cli locally to connect to the ECS Linux server for logon verification. For example, if the IP address of the server with rinetd installed is 1.1.1.1, run redis-cli -h 1.1.1.1 -a ApsaraDB for Redis instance ID:ApsaraDB for Redis password. Alternatively, telnet to the ECS Linux server and perform the operation verification. For example, if the IP address of the ECS Linux server is 1.1.1.1, you can telnet to 1.1.1.1 6379.

    2. After the ECS Linux server is connected, enter the password to connect to ApsaraDB for Redis: auth Redis connection password.

    3. Write data and perform query and verification.

      Local verification

After performing the preceding steps, you can use a local PC or server to connect to port 6379 of the ECS Linux server through a public network and access ApsaraDB for Redis.

Note: You can use the preceding scheme to test and use rinetd. As rinetd is open source software, read its official documentation or contact rinetd engineers for help if you have any problems in use.

Thank you! We've received your feedback.