照着书本,学习node.js连接redis,报告下述错误
$ npm start
> 4-2-1@0.0.0 start C:\Users\DW\Dev\Study\web\nodejs\4-2-1
> node ./bin/www
events.js:377
throw er; // Unhandled ‘error‘ event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:209:20)
Emitted ‘error‘ event on RedisClient instance at:
at RedisClient.on_error (C:\Users\DW\Dev\Study\web\nodejs\4-2-1\node_modules\redis\index.js:342:14)
at Socket.<anonymous> (C:\Users\DW\Dev\Study\web\nodejs\4-2-1\node_modules\redis\index.js:223:14)
at Socket.emit (events.js:400:28)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
errno: -4077,
code: ‘ECONNRESET‘,
syscall: ‘read‘
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! 4-2-1@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the 4-2-1@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
看上去是拒绝连接了,原因未知。
在redis-server上抓包:
[root@centos7 ~]# tcpdump -i eth0 -enn tcp port 6379
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
14:35:02.970452 00:15:5d:4d:52:87 > 00:15:5d:03:2e:01, ethertype IPv4 (0x0800), length 66: 172.17.160.1.61874 > 172.17.161.191.6379: Flags [S], seq 1228743096, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
14:35:02.970531 00:15:5d:03:2e:01 > 00:15:5d:4d:52:87, ethertype IPv4 (0x0800), length 66: 172.17.161.191.6379 > 172.17.160.1.61874: Flags [S.], seq 2651139702, ack 1228743097, win 29200, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
14:35:02.971013 00:15:5d:4d:52:87 > 00:15:5d:03:2e:01, ethertype IPv4 (0x0800), length 54: 172.17.160.1.61874 > 172.17.161.191.6379: Flags [.], ack 1, win 8212, length 0
14:35:02.971441 00:15:5d:03:2e:01 > 00:15:5d:4d:52:87, ethertype IPv4 (0x0800), length 1155: 172.17.161.191.6379 > 172.17.160.1.61874: Flags [P.], seq 1:1102, ack 1, win 229, length 1101: RESP "DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. 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 bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside."
14:35:02.971592 00:15:5d:03:2e:01 > 00:15:5d:4d:52:87, ethertype IPv4 (0x0800), length 54: 172.17.161.191.6379 > 172.17.160.1.61874: Flags [F.], seq 1102, ack 1, win 229, length 0
14:35:02.972053 00:15:5d:4d:52:87 > 00:15:5d:03:2e:01, ethertype IPv4 (0x0800), length 54: 172.17.160.1.61874 > 172.17.161.191.6379: Flags [.], ack 1103, win 8208, length 0
14:35:02.982617 00:15:5d:4d:52:87 > 00:15:5d:03:2e:01, ethertype IPv4 (0x0800), length 68: 172.17.160.1.61874 > 172.17.161.191.6379: Flags [P.], seq 1:15, ack 1103, win 8208, length 14: RESP "info"
14:35:02.982685 00:15:5d:03:2e:01 > 00:15:5d:4d:52:87, ethertype IPv4 (0x0800), length 54: 172.17.161.191.6379 > 172.17.160.1.61874: Flags [R], seq 2651140805, win 0, length 0
这里就很清晰了,指定启动redis时,指定 --protected-mode no
选项即可。
$ npm start
> 4-2-1@0.0.0 start C:\Users\DW\Dev\Study\web\nodejs\4-2-1
> node ./bin/www
Hello Redis