首页 > 解决方案 > Redis cli 工作 - 两者都不是任何 NodeJS Redis 客户端

问题描述

  1. 我已按照本指南:https ://redislabs.com/blog/redis-on-windows-10/安装以下内容:Windows 子系统、Ubuntu 和 Redis 服务器,一切似乎都可以通过 Ubuntu 终端和Windows 中的 redis-cli。我可以设置和获取值。但是我的 NodeJS 客户端无法工作/无法连接(只有第一次?)。

  2. 我已经安装了以下 NPM Redis 客户端:redis ( https://www.npmjs.com/package/redis )。

const redis = require("redis");
const client = redis.createClient();
 
client.on("error", function(error) {
  console.error(error);
});
 
client.set("key", "value", redis.print);
client.get("key", redis.print);

第一次成功(它可以设置和读取名为“key”的密钥)!但从那以后再也没有!?我尝试关闭并打开 Ubuntu 终端,重新启动 redis 服务器等,但似乎没有任何帮助。redis-cli 一直在 ubuntu 终端上工作——只有 NodeJS 客户端突然不工作了。

  1. 我还尝试安装另一个客户端:https ://www.npmjs.com/package/ioredis但问题仍然存在并且是相同的。我这样连接这个客户端:
const Redis = require("ioredis");
const redis = new Redis({
    port: 6379, // Redis port
    host: "127.0.0.1", // Redis host
    family: 4, // 4 (IPv4) or 6 (IPv6)
    db: 0,
});

// ioredis supports all Redis commands:
redis.set("foo", "bar"); // returns promise which resolves to string, "OK"

// Or ioredis returns a promise if the last argument isn't a function
redis.get("foo").then(function (result) {
    console.log(result); // Prints "bar"
});

再次,它第一次与第一个 Redis 客户端一起工作,我没有设置任何身份验证/凭据......

  1. 我没有更改默认 conf 文件中的任何一行。

  2. 我刚刚尝试安装 Ubuntu 20.04(在我运行 18.04 之前)并再次从头安装 Redis - 但结果是一样的:Redis cli 工作,但没有一个 NodeJS 客户端(现在甚至不是第一次)。

它会在我捕捉到“重新连接”和“连接”事件之后立即引发异常 - 然后它会一遍又一遍地循环运行这些步骤。所以它似乎能够连接但之后立即断开连接?

6a。我收到此错误消息(NPM:Redis):

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:209:20) {
  errno: -4077,
  code: 'ECONNRESET',
  syscall: 'read'
}
AbortError: Ready check failed: Redis connection lost and command aborted. It might have been processed.
    at RedisClient.flush_and_error (D:\Workspace\GitKraken\tradebutlercommander\node_modules\redis\index.js:297:23)
    at RedisClient.connection_gone (D:\Workspace\GitKraken\tradebutlercommander\node_modules\redis\index.js:602:14)
    at RedisClient.on_error (D:\Workspace\GitKraken\tradebutlercommander\node_modules\redis\index.js:345:10)
    at Socket.<anonymous> (D:\Workspace\GitKraken\tradebutlercommander\node_modules\redis\index.js:222:14)
    at Socket.emit (events.js:315:20)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'UNCERTAIN_STATE',
  command: 'INFO',
  origin: Error: read ECONNRESET
      at TCP.onStreamRead (internal/stream_base_commons.js:209:20) {
    errno: -4077,
    code: 'ECONNRESET',
    syscall: 'read'
  }
}

6b。或者现在我在使用 NPM 时收到此错误消息:ioredis:

[ioredis] Unhandled error event: Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:209:20)
    at TCP.callbackTrampoline (internal/async_hooks.js:129:14)
  1. 我的整个 Redis 日志如下所示:

14151:C 09 Nov 20:32:56.542 # oO0OoO0OoO0Oo Redis 正在启动 oO0OoO0OoO0Oo 14151:C 11 月 9 日 20:32:56.543 # Redis 版本=4.0.9, bits=64, commit=00000000, modified=0, pid=14151,刚开始 14151:C 11 月 9 日 20:32:56.543 # 配置已加载

14152:M 09 Nov 20:32:56.546 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
14152:M 09 Nov 20:32:56.546 # Server initialized
14152:M 09 Nov 20:32:56.546 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
14152:M 09 Nov 20:32:56.546 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
14152:M 09 Nov 20:32:56.546 * Ready to accept connections
14152:M 09 Nov 20:47:57.089 * 1 changes in 900 seconds. Saving...
14152:M 09 Nov 20:47:57.092 * Background saving started by pid 14157
14157:C 09 Nov 20:47:57.103 * DB saved on disk
14157:C 09 Nov 20:47:57.104 * RDB: 2 MB of memory used by copy-on-write
14152:M 09 Nov 20:47:57.193 * Background saving terminated with success
14152:M 09 Nov 22:52:57.784 * 1 changes in 900 seconds. Saving...
14152:M 09 Nov 22:52:57.785 * Background saving started by pid 14158
14158:C 09 Nov 22:52:57.794 * DB saved on disk
14158:C 09 Nov 22:52:57.794 * RDB: 2 MB of memory used by copy-on-write
14152:M 09 Nov 22:52:57.886 * Background saving terminated with success
14152:M 09 Nov 23:07:58.080 * 1 changes in 900 seconds. Saving...
14152:M 09 Nov 23:07:58.081 * Background saving started by pid 14159
14159:C 09 Nov 23:07:58.089 * DB saved on disk
14159:C 09 Nov 23:07:58.090 * RDB: 2 MB of memory used by copy-on-write
14152:M 09 Nov 23:07:58.182 * Background saving terminated with success
14152:signal-handler (1604949420) Received SIGTERM scheduling shutdown...
14152:M 09 Nov 23:17:00.243 # User requested shutdown...
14152:M 09 Nov 23:17:00.243 * Saving the final RDB snapshot before exiting.
14152:M 09 Nov 23:17:00.248 * DB saved on disk
14152:M 09 Nov 23:17:00.248 * Removing the pid file.
14152:M 09 Nov 23:17:00.248 # Redis is now ready to exit, bye bye...
14182:C 09 Nov 23:17:01.275 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14182:C 09 Nov 23:17:01.275 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=14182, just started
14182:C 09 Nov 23:17:01.275 # Configuration loaded  

14183:M 09 Nov 23:17:01.276 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
14183:M 09 Nov 23:17:01.276 # Server initialized
14183:M 09 Nov 23:17:01.277 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
14183:M 09 Nov 23:17:01.277 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
14183:M 09 Nov 23:17:01.277 * DB loaded from disk: 0.000 seconds
14183:M 09 Nov 23:17:01.277 * Ready to accept connections
14183:M 09 Nov 23:32:02.032 * 1 changes in 900 seconds. Saving...
14183:M 09 Nov 23:32:02.033 * Background saving started by pid 14195
14195:C 09 Nov 23:32:02.037 * DB saved on disk
14195:C 09 Nov 23:32:02.037 * RDB: 2 MB of memory used by copy-on-write
14183:M 09 Nov 23:32:02.134 * Background saving terminated with success
14183:signal-handler (1604976838) Received SIGTERM scheduling shutdown...
14183:M 10 Nov 06:53:58.489 # User requested shutdown...
14183:M 10 Nov 06:53:58.489 * Saving the final RDB snapshot before exiting.
14183:M 10 Nov 06:53:58.500 * DB saved on disk
14183:M 10 Nov 06:53:58.500 * Removing the pid file.
14183:M 10 Nov 06:53:58.500 # Redis is now ready to exit, bye bye...
14218:C 10 Nov 06:53:59.528 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14218:C 10 Nov 06:53:59.528 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=14218, just started
14218:C 10 Nov 06:53:59.528 # Configuration loaded

14219:M 10 Nov 06:53:59.530 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
14219:M 10 Nov 06:53:59.530 # Server initialized
14219:M 10 Nov 06:53:59.530 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
14219:M 10 Nov 06:53:59.530 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
14219:M 10 Nov 06:53:59.530 * DB loaded from disk: 0.000 seconds
14219:M 10 Nov 06:53:59.530 * Ready to accept connections
14219:signal-handler (1604985108) Received SIGTERM scheduling shutdown...
14219:M 10 Nov 09:11:48.604 # User requested shutdown...
14219:M 10 Nov 09:11:48.604 * Saving the final RDB snapshot before exiting.
14219:M 10 Nov 09:11:48.612 * DB saved on disk
14219:M 10 Nov 09:11:48.612 * Removing the pid file.
14219:M 10 Nov 09:11:48.612 # Redis is now ready to exit, bye bye...
14252:C 10 Nov 09:11:49.645 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14252:C 10 Nov 09:11:49.645 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=14252, just started
14252:C 10 Nov 09:11:49.645 # Configuration loaded

14253:M 10 Nov 09:11:49.648 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
14253:M 10 Nov 09:11:49.648 # Server initialized
14253:M 10 Nov 09:11:49.648 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
14253:M 10 Nov 09:11:49.648 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
14253:M 10 Nov 09:11:49.648 * DB loaded from disk: 0.000 seconds
14253:M 10 Nov 09:11:49.648 * Ready to accept connections
14253:signal-handler (1604987303) Received SIGTERM scheduling shutdown...
14253:M 10 Nov 09:48:24.076 # User requested shutdown...
14253:M 10 Nov 09:48:24.076 * Saving the final RDB snapshot before exiting.
14253:M 10 Nov 09:48:24.089 * DB saved on disk
14253:M 10 Nov 09:48:24.089 * Removing the pid file.
14253:M 10 Nov 09:48:24.089 # Redis is now ready to exit, bye bye...
14280:C 10 Nov 09:48:25.139 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14280:C 10 Nov 09:48:25.139 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=14280, just started
14280:C 10 Nov 09:48:25.139 # Configuration loaded

14281:M 10 Nov 09:48:25.144 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
14281:M 10 Nov 09:48:25.144 # Server initialized
14281:M 10 Nov 09:48:25.144 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
14281:M 10 Nov 09:48:25.144 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
14281:M 10 Nov 09:48:25.144 * DB loaded from disk: 0.000 seconds
14281:M 10 Nov 09:48:25.144 * Ready to accept connections

标签: node.jsrediswindows-subsystem-for-linuxnode-redisioredis

解决方案


推荐阅读