Detailed explanation of remote connection to Redis database on public network

foreword

Jiejie's personal homepage
I just ask you if you have played!
Unity of knowledge and action, lofty aspirations.


As a cached key-value key-value database, Redis is widely used in many scenarios. Since the data is stored in memory, the reading and writing efficiency is extremely high.

The following describes how to build redis in linux of the internal network virtual machine and realize public network access through cpolar internal network penetration

1. Linux (centos8) install redis database

  • Enter the local directory under user, usually the external software is installed in this directory
cd /usr/local
  • download redis
wget https://download.redis.io/releases/redis-6.2.6.tar.gz

20230203175201

  • After downloading, unzip redis
tar xzf redis-6.2.6.tar.gz
  • After decompression, enter the redis directory to compile
cd redis-6.2.6/
  • Enter after entering make, wait for the compilation to complete after entering

20230203175202

After compiling, enter the following command to install redis

make install PREFIX=/usr/local/redis-6.2.6

20230203175203

After installation, enter the redis directory and execute the following command to start redis, the default port is 6379

./bin/redis-server redis.conf

20230203175204

then Ctrl+Cexit

2. Configure redis database

Configure redis to run in the background, edit the configuration fileredis.conf

vim /usr/local/redis-6.2.6/redis.conf 

daemonize noChange todaemonize yes

20230203175205

Also modify the following two parameters separately to enable remote access

20230203175206

After the modification is completed, save and exit the configuration file, restart redis and then execute the following startup command again, and find that the redis graphic is gone, which is the background startup

./bin/redis-server redis.conf

3. Intranet penetration

cpolar is an intranet penetration software, easy to use, safe and reliable. We can expose the local port 6379 to the public network through cpolar, and then use tools for remote access.

3.1 Install cpolar intranet penetration

  • Linux supports installation using one-click script commands
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash
  • token authentication

Log in to the background of the cpolar official website, click the verification on the left to view your own authentication token, and then execute the following command, where xxxx is the token code

cpolar authtoken xxxxxxx(您的token)

20230206180545

  • Add services to the system (to facilitate self-starting at boot)
sudo systemctl enable cpolar
  • Start the cpolar service
sudo systemctl start cpolar

3.2 Create a tunnel mapping local port

After cpolar is successfully installed, enter the following command on the command line to expose the local redis on port 6369 to the public network

cpolar tcp 6379

The following interface appears to indicate success, and then copy the public network address

20230203175207

Use the redis graphical interface to remotely connect to redis, enter the copied tcp address, and click Test Connection

20230203175208

If successful appears, it means success

20230203175209

Note: If you want to make the tunnel accessible for a long time, you can save it to the configuration file to implement background services. Please refer to cpolar official website - documentation - command line to create a tunnel - configure the tunnel as a background service.

4. Configure a fixed TCP port address

The above steps use a random temporary tcp port address, and the generated public network address is a random temporary address, which will change randomly within 24 hours. And we can configure a fixed public network address for it to facilitate subsequent long-term remote.

4.1 Reserve a fixed tcp address

Log in to the cpolar official website, click Reserve on the left, and find the reserved tcp address. Let's reserve a fixed tcp address for the remote Redis database:

  • Region: Select China VIP
  • Description: It is a note, which can be customized

click保留

20230203175210

After the address is successfully reserved, the system will generate a corresponding fixed public network address and copy it down

20230203175211

4.2 Configure fixed TCP address

Edit the cpolar configuration file

vim /usr/local/etc/cpolar/cpolar.yml

Copy and add the following information, the remote_addr parameter is the reserved tcp address copied above

redis:
    addr: 6379
    proto: tcp
    remote_addr: 3.tcp.vip.cpolar.cn:11506

20230203175212

After modifying, save and exit, then restart the cpolar service

sudo systemctl start cpolar

4.3 Use a fixed tcp address to connect

20230203175213

20230203175214·

If successful appears, it means success

20230203175215

Guess you like

Origin blog.csdn.net/2202_75623950/article/details/131494813