我通过在我的wsl2 Ubuntu发行版中构建redis
来运行我的redis-server
。但不幸的是,我无法使用ioredis
包连接到它。以下是我的代码(与ioredis
提供的代码相同):
const Redis = require("ioredis");
const redis = new Redis({
port: 6379,
host: '127.0.0.1'
});
redis.set("foo", "bar");
redis.get("foo", function (err, result) {
if (err) {
console.error(err);
} else {
console.log(result); // Promise resolves to "bar"
}
});
每次我收到以下错误信息时
[ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1146:16)
我能够从我的wsl2终端使用redis-client
连接到redis-server
,但不是从我的代码。我没有改变Redis的默认配置,所以我不确定这是从哪里来的。任何类型的线索都会很有帮助。谢谢。
发布于 2021-10-05 10:05:26
经过几个小时的尝试不同的解决方案,我能够解决这个问题。由于wsl2上的redis-server
是在一个单独的网络上运行的,所以通过127.0.0.1
访问它不起作用。我需要知道我的wsl2实例的IP地址,并在ioredis构造函数中传递正确的连接细节。
sudo apt install net-tools
ifconfig
以获取IP地址。它应该位于eth0
inet
section.时粘贴IP地址
const redis = new Redis({
port: 6379,
host: '<your-wsl2-ip-here>'
});
redis-cli
,然后使用CONFIG SET protected-mode no
.关闭保护模式
希望这能帮上忙。
发布于 2021-10-06 10:06:56
解决上述问题的另一种方法是加载位于redis-server
可执行文件上一级的默认配置文件redis.conf
,该文件具有允许外部流量进入wsl2进程的必要配置。
./redis-server ../redis.conf
https://stackoverflow.com/questions/69447386
复制相似问题