为了系统的安全给Redis增加密码很有必要。
步骤 1:找到 Redis 配置文件
Redis 配置文件通常名为redis.conf,常见路径:
- Linux/Mac:
/etc/redis/redis.conf或/usr/local/etc/redis/redis.conf - Windows:Redis 安装目录下的
redis.windows.conf
步骤 2:修改配置文件
-
打开配置文件(以 Linux 为例):
vi /etc/redis/redis.conf -
找到
requirepass配置项(默认被注释),取消注释并设置密码:# 找到这行(默认是 # requirepass foobared) requirepass your_secure_password # 替换为你的密码
步骤 3:重启 Redis 服务
-
Linux(systemd):
sudo systemctl restart redis -
Linux(service):
sudo service redis restart -
Windows:在 Redis 服务窗口重启,或停止后重新启动 Redis 进程。
步骤 4:验证永久密码
重启后连接 Redis,仍需输入密码才能操作:
127.0.0.1:6379> CONFIG SET requirepass ""
OK