变动Redis端口号更改带来的变化(redis 端口后数字)

变动Redis端口号更改带来的变化

在使用Redis作为缓存系统的过程中,有时候需要更改默认的端口号,以增加安全性。但是,更改端口号也会带来一些变化。本文将介绍在更改Redis端口号后可能会出现的问题及解决方案。

我们需要知道如何更改Redis的端口号。在Redis的配置文件redis.conf中,可以找到以下代码:

# If not running redis-server with root privileges you may specify the
# user that should own the Redis process with the following option.
#
# Note that configuring a non-default user requires creating a Redis user
# with the same permissions as redis-server's default user (see above).
#
# user someusername
#
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by "emit"ing the "redis" event
# supervised systemd - signal systemd by writing a "READY=1" message
# to the NOTIFY_SOCKET environment variable when
# the service is ready.
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or SYSTEMD_UNIT environment variables.
# Note that setting "supervised" to "no" can be a good solution for debugging.
# NOTE: These supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
#
# supervised systemd
#
# If you run Redis from another system account you may specify a path
# that contns the directory with the Unix socket used by Redis.
# The default value is /var/run/redis/redis.sock.
# unixsocket /tmp/redis.sock
# unixsocketperm 700

# Redis Port
#
# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
#
port 6379

可以看到,Redis的默认端口号是6379。为了更改端口号,只需要将port 6379行改为其他可用端口即可。例如,将端口号改为6380:

port 6380

然后,重新启动Redis即可生效。可以使用以下命令检查Redis的状态:

$ sudo systemctl status redis

如果Redis正常运行,则可以看到类似以下的输出:

● redis.service - Redis persistent key-value database
Loaded: loaded (/lib/systemd/system/redis.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-11-20 15:04:10 UTC; 10s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Process: 9107 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
Mn PID: 9108 (redis-server)
Tasks: 4 (limit: 2357)
CGroup: /system.slice/redis.service
└─9108 /usr/bin/redis-server 127.0.0.1:6380

可以看到,此时Redis的端口号已经改为6380。

但是,在更改了端口号之后,可能会导致以下的问题:

1. 连接错误

如果使用了旧的端口号连接Redis,将会收到连接错误的提示:

$ redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.

这是因为Redis服务器已经不再监听默认的端口6379,需要将连接信息中的端口号改为新的端口号。例如,连接到端口号为6380的Redis:

$ redis-cli -p 6380
127.0.0.1:6380> ping
PONG

2. 集群配置

如果在Redis集群中更改了部分节点的端口号,可能会导致集群配置错误。例如,本来所有的节点都监听了6379端口,但是在某一节点更改端口为6380后,集群配置就会发生变化。

解决此问题的方法是使用集群命令reshard来重新分配槽位。但是,这个过程需要格外小心,避免数据丢失。

更改Redis的端口号虽然可以增加安全性,但也可能会带来一些影响。在更改之前,需要对影响有足够的准备。同时,如果遇到连接错误或者集群配置错误等问题,需要及时进行排查和解决。


数据运维技术 » 变动Redis端口号更改带来的变化(redis 端口后数字)