如何使用Express.js从本地系统上运行的node.js应用程序连接到EC2上运行的redis?

时间:2016-11-11 23:24:10

标签: node.js amazon-ec2 redis ubuntu-server

我正在Express.js上编写Node.js应用程序。我在UBUNTU Server 16.04上运行的EC2实例上安装了Redis服务器。我为Redis安装了npm模块。我已经提到过这样的端口和主机: -

var redis = require('redis');
var client = redis.createClient(6379,"127.0.0.1");

当我尝试连接到Redis的EC2实例时,我收到如下错误: -

events.js:141
  throw er; // Unhandled 'error' event
  ^

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1061:14)

如何连接到ec2上运行的redis?

1 个答案:

答案 0 :(得分:0)

从错误消息中,看起来redis实际上并未在您的主机上运行。我会检查redis确实是这样运行的:

ps auxwg | grep redis

并检查正在运行的redis进程。另一种更简单的方法是使用' telnet'尝试连接本地redis实例的命令,如:

wintermute:~ 07:52:58 melgart$ telnet 127.0.0.1 6379
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

在我的情况下,redis 已经启动并运行,因此我得到了连接'响应。 HTH

相关问题