Install redis and remote connection on Ubuntu20.04

1. Install Redis5.7

1. Install Redis

apt-get install redis-server

2. After the installation is complete, the Redis server will start automatically. Check whether redis starts successfully

service redis-server status  #查看状态

The Active:active(running) status is displayed as follows: it means that redis is already running and started successfully.

3. Configure Redis

By default, Redis does not allow remote connections. The Redis server can only be connected from 127.0.0.1 (localhost) - the machine where the Redis server is running.

Open the Redis configuration file with an editor, the command is as follows:

sudo vi /etc/redis/redis.conf

3.1. Find the corresponding line and modify it as follows:

#bind 127.0.0.0 ::1  //注释掉这行
protected-mode no   //默认yes改成no

3.2. Save this file, and use the restart command at the beginning of the article to restart the Redis service to make the application take effect

4. Set Redis to start automatically after booting:

systemctl enable redis-server.service

5. Use the command redis process default port 6379

ps -aux|grep redis-server

Use the following command to verify that the Redis server is listening on port 6379:

ss -an | grep 6379

6. When the Redis server is installed, the Redis command line client will be installed by default.

Enter the following command on the command line

redis-cli 

If password 123456 is set then use the following command

redis-cli -a 123456 

Enter exit to exit the command line client

7. Execute sudo vim /etc/redis/redis.conf to modify user passwordinsert image description here

Close redis service and verification

redis-cli -h 127.0.0.1 -p 6379 shutdown               # 没有设置redis密码情况下
redis-cli -h 127.0.0.1 -p 6379 -a 配置密码 shutdown    # 设置redis密码情况下

2. Configure remote access

The directory where the Ubuntu configuration file is located

/etc/reids/redis.conf

1. Open the configuration file via sudo

sudo vim /etc/redis/redis.conf

2. Comment out bind 127.0.0.1 ::1

# bind 127.0.0.1 ::1

3. Change protected-mode to no

protected-mode no

4. Save the configuration file

:wq

5. Restart the Redis server to make it take effect

sudo systemctl restart redis-server

6. Verify that the redis server is listening on port 6379

ss -an | grep 6379

Can see the following information

tcp  LISTEN 0   511   0.0.0.0:6379   0.0.0.0:*
tcp  LISTEN 0   511      [::]:6379      [::]:*  

I can configure it here by myself. The following is a supplement:

Next, you will need to configure your firewall to allow network traffic through TCP port 6379.

Usually you want to allow access to the Redis server from a specific IP address or a specific IP range. For example, to allow connections from 192.168.121.0/24, run the following command:

sudo ufw allow proto tcp from IP地址/24 to any port 6379
确保你的防火墙被配置仅仅接受来自受信任 IP 的连接。

Run a remote connection test

redis-cli -h 10.211.55.7 ping

Success will get:PONG

connect:

redis-cli -h 地址

For example:redis-cli -h 192.168.1.1

Guess you like

Origin blog.csdn.net/m0_68744965/article/details/129017604