在学习Redis开发过程中,我们可能需要在 Windows 系统中连接 WSL2 中的 Redis 服务。本文将详细介绍如何解决连接被拒绝的问题,并提供几种常见的解决方法。
问题描述
当你尝试从 Windows 系统使用 redis-cli
连接到 WSL2 中的 Redis 服务时,可能会遇到以下错误:
1、在连接后执行 ping
命令时,收到以下错误:
(error) DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a an authentication password for the default user. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
解决方法
方法 1: 从本地回环接口禁用保护模式
-
连接到本地 Redis 服务:
redis-cli -h 127.0.0.1 -p 6379
-
禁用保护模式:
CONFIG SET protected-mode no
-
使更改永久生效:
CONFIG REWRITE
方法 2: 编辑 Redis 配置文件
-
编辑 Redis 配置文件:
sudo nano /etc/redis/redis.conf
-
找到
protected-mode
这一行,将其设置为no
:protected-mode no
-
保存文件并退出。
-
重启 Redis 服务:
sudo systemctl restart redis-server
或者
sudo service redis-server restart
方法 3: 重启 Redis 服务时禁用保护模式
-
从命令行启动 Redis 服务时添加
--protected-mode no
选项:redis-server --protected-mode no
方法 4: 设置密码
-
编辑 Redis 配置文件:
sudo nano /etc/redis/redis.conf
-
找到
user default
这一部分,设置密码:user default on >allcommands allkeys password <YOUR_PASSWORD>
其中
<YOUR_PASSWORD>
是你希望设置的密码。 -
保存文件并退出。
-
重启 Redis 服务:
sudo systemctl restart redis-server
或者
sudo service redis-server restart
-
连接时使用密码:
redis-cli -h [WSL2_IP] -p 6379 -a <YOUR_PASSWORD>
选择适合你的方法
-
如果你只是在本地开发环境中使用 Redis,可以选择方法 1 或方法 2 来禁用保护模式。
-
如果你希望在生产环境中使用 Redis,建议选择方法 4 设置密码,以确保 Redis 服务的安全性。
小结
通过以上方法,你应该能够解决连接被拒绝的问题,并成功从 Windows 系统连接到 WSL2 中的 Redis 服务。如果遇到问题,可以检查 Redis 服务的配置文件和日志,确保所有设置都正确无误。
问题二:运行
D:\DataFile\tools\redis>redis-cli -h 172.33.22.111 -p 6379
报错
Could not connect to Redis at 172.33.22.111:6379: Connection refused not connected
在连接时遇到:Could not connect to Redis at [WSL2_IP]:6379: Connection refused
1. 检查 Redis 服务是否启动
确保 Redis 服务已经在 WSL2 中启动。可以使用以下命令检查 Redis 服务的状态:
sh复制
sudo systemctl status redis-server
如果服务未启动,可以使用以下命令启动 Redis 服务:
sudo systemctl start redis-server
2. 检查 Redis 端口配置
确保 Redis 服务正在监听正确的 IP 地址和端口。可以查看 Redis 配置文件 /etc/redis/redis.conf
,确保 bind
设置为 0.0.0.0
,port
设置为 6379
:
bind 0.0.0.0
port 6379
3. 检查防火墙设置
确保 Windows 防火墙允许连接到 WSL2 的 IP 地址和端口 6379。可以使用以下命令临时关闭防火墙进行测试:
netsh advfirewall set allprofiles state off
连接成功后,可以重新启用防火墙并添加相应的规则:
netsh advfirewall set allprofiles state on
netsh advfirewall firewall add rule name="Redis" dir=in action=allow protocol=TCP localport=6379
4. 确认 Redis 版本和日志
检查 Redis 日志来获取错误详细信息:
sudo tail -f /var/log/redis/redis-server.log
日志文件通常会记录导致服务启动失败的原因,帮助进一步排查。