Redis密码丢失重启后的困境(redis重启后密码丢失)
Redis会将用户设置的密码存储在内存中,在Redis重启后,这些密码就无从获取了。当Redis的密码丢失时,情况就非常棘手了。
我们知道,如果Redis没有相应的密码控制,就谈不上安全了。因此,当密码丢失重启后,我们必须立即解决问题,恢复Redis的原始密码。
第一步:重启Redis应用服务,使其具有操作权限,然后用”CONFIG GET”查看当前Redis是否设置了密码:
redis 127.0.0.1:6379> CONFIG GET requirepass
若返回结果为空,则无需处理,表明Redis是没有设置密码;若返回结果中有密码内容,则表明此密码仍然存在,此时应将Redis内部的密码内容使用 “CONFIG SET” 设置为当前需要使用的密码:
redis 127.0.0.1:6379> CONFIG SET requirepass 123456
若返回OK表示设置成功,则可以正常使用Redis服务了。若返回结果不为OK表示操作失败,可以使用” SHUTDOWN SAVE”命令将数据保存至本地文件,然后重启服务:
redis 127.0.0.1:6379> SHUTDOWN SAVE
第二步:进入Redis的安装目录,找到redis.windows.conf文件,修改里面的两个关键参数:
# Require clients to issue AUTH before processing any other commands.
# This might be useful in environments in which you do not trust others# with access to the host running redis-server.
## requirepass foobared
## Command Renaming.
## It is possible to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something# hard to guess so that it will be still avlable for internal-use tools
# but not avlable for general clients.#
# Example:#
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
将requirepass参数改为你想设置的密码,可以自定义,比如:
# Require clients to issue AUTH before processing any other commands.
# This might be useful in environments in which you do not trust others# with access to the host running redis-server.
#requirepass 123456
## Command Renaming.
## It is possible to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something# hard to guess so that it will be still avlable for internal-use tools
# but not avlable for general clients.#
# Example:#
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
保存文件,重启Redis服务后即可按照新设置的密码登录Redis。
当Redis的密码丢失重启后,上述方法可以帮助我们解决困境,恢复Redis的原始密码。