Redis chapter: How to set up access to redis from the external network

1. Preface
When we deploy the redis service, redis itself only allows local access by default.
But if we want to access redis on the external network, how should we achieve it?

Implementation method: through simple configuration, access from the external network is allowed.
There is a limitation here: Redis version problem, the version is too low.
The protected-mode configuration has been added after Redis3.2, and the default is yes, which means it is enabled.
The effect when protected-mode is set to different values ​​is as follows:
insert image description here
Note: If you want to implement Redis in the external access server, in addition to setting protected-mode no, you also need to comment out bind 127.0.0.1 in the redis.conf file .

2. Specific implementation steps
1. Find the redis configuration file redis.conf file in your server.
If you are not sure, you can execute find / -name redis.conf.
If you installed redis through yum, the default configuration file of redis is: / etc/redis/redis.conf

vim /opt/redis/conf/redis.conf

insert image description here
Note: Comment out all the binds here.
2. If your server has enabled a firewall policy, you need to configure the firewall and open the corresponding redis port. This port is the corresponding port number in the redis configuration file you started.

# 1、开放redis的6379端口【假设redis端口为6379】
firewall-cmd --zone=public --add-port=6379/tcp --permanent

# 2、重启防火墙使得配置生效
systemctl restart firewalld

# 3、查看系统所有开放的端口
firewall-cmd --zone=public --list-ports

3. Restart the redis service

# 1、查看redis进程是否存在
ps -ef | grep redis

# 2、关闭redis
# 找到自己redis服务中的redis-cli,
./opt/redis/bin/redis-cli shutdown

#3、启动redis 【加&表示以后台程序方式运行,不加也可以】
./opt/redis/bin/redis-server &
# 使用指定配置文件启动redis
./opt/redis/bin/redis-server /opt/redis/conf/redis.conf

4. Test

#1、进入redis服务
./opt/redis/bin/redis-cli -h IP地址 -p 端口
# 通过执行下面的命令,看看是不是都为no,如果不是,就用config set 配置名 属性 改为no。
config get daemonize
config get protected-mode 

insert image description here
OK~

Guess you like

Origin blog.csdn.net/m0_67394006/article/details/126495657