简体   繁体   English

我正在尝试从 heroku 连接到在线 redis 服务器,但我的连接被拒绝

[英]I am trying to connect to an online redis server from heroku but my connection is getting refused

I am using the Redis package for Node.js ( ioredis ), I have a redis cluster hosted on ScaleGrid that I am trying to connect to from heroku but i keep getting the error [ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14) Redis Error: { ReplyError: NOAUTH Authentication required. I am using the Redis package for Node.js ( ioredis ), I have a redis cluster hosted on ScaleGrid that I am trying to connect to from heroku but i keep getting the error [ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14) Redis Error: { ReplyError: NOAUTH Authentication required. . . This is my code这是我的代码

//This is what my redis uri provided by ScaleGrid uri looks like, this is not exact string though
REDIS_URI=SG-Stack-12345.servers.mongodirector.com:6379

import session from 'express-session';
import Redis from 'ioredis';
import connectRedis from 'connect-redis';

const redisClient = process.env.REDIS_URI;

const redis = new Redis(redisClient);
const redisStore = connectRedis(session);

redis.on('error', (err) => {
  console.log('Redis Error:', err);
});

app.use(session({
  secret: process.env.SESSION_SECRET,
  name: '_redisSession',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: false },
  store: new redisStore({ client: redis, ttl: 86400 }),
}));

Please how do i solve this issue?请问我该如何解决这个问题? and also why do i still get this Error: connect ECONNREFUSED 127.0.0.1:6379还有为什么我仍然会收到此Error: connect ECONNREFUSED 127.0.0.1:6379

PS Redis works perfectly on my localhost PS Redis 在我的本地主机上完美运行

I realized all i needed was to send the password for authentication immediately after the redis client connects (client.auth(password) . I had to settle for redislabs enterprise for my redis hosting and also had to change from ioredis package to redis package because of some strange errors. This is the code below I realized all i needed was to send the password for authentication immediately after the redis client (client.auth(password) . I had to settle for redislabs enterprise for my redis hosting and also had to change from ioredis package to redis package because of一些奇怪的错误。这是下面的代码

redis.js redis.js

import { createClient } from 'redis';

const {
  PASSWORD: password,
  REDIS_HOST: host, // On localhost, set to 'localhost' or '127.0.0.1'
  REDIS_PORT: port, // On localhost,  set to 6379
} = process.env;

// connect to Redis host with port and host set as environment variables
const client = createClient(port, host, { no_ready_check: true });

// If password is in environment variable, send password to the host for authentication
if (password) {
  client.auth(password, (err) => {
    if (err) throw err;
  });
}

client.on('connect', () => console.log('connected to Redis'));

client.on('error', (err) => {
  console.log('Redis Error:', err);
});

export default client;

This is optional, it only shows how the connected redis client is utilized in the index file.这是可选的,它只显示连接的 redis 客户端如何在索引文件中使用。

index.js index.js

import session from 'express-session';
import connectRedis from 'connect-redis';
import client from './redis'

const redisStore = connectRedis(session);

app.use(session({
  secret: 'IwontTell',
  name: '_stackOverflow',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: false },
  store: new redisStore({ client, ttl: 86400 }),
}));

This works perfecly both on localhost and online.这在本地主机和在线上都可以完美运行。 Hope this helps someone希望这可以帮助某人

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试连接实时服务器。 在iOS中获取连接拒绝错误 - I am trying to connect with my live server. getting Connection refused error in iOS 我正在尝试从Express应用程序连接到MongoDB。 我认为我的连接字符串抛出语法错误 - I am trying to connect to connect to MongoDB from my Express Application. It is throwing a syntax error at my connection string I think Redis Heroku Node.JS连接被拒绝 - Redis Heroku Node.JS Connection Refused 我正在尝试将node js应用程序部署到heroku,但是在推送到master时仍然出现错误 - I am trying to deploy my node js app to heroku but I keep getting an error when pushing to master SocketCluster - 无法从 Android 应用程序连接到服务器 - 连接被拒绝 - SocketCluster - Unable to connect to server from Android app - Connection refused 尝试从 kubernetes pod 访问服务时连接被拒绝 - Getting Connection refused while trying to access service from kubernetes pod 我正在尝试从 mongodb 连接我的 server.js express api 但它显示一些错误我不明白 - I am trying to connect my server.js express api from mongodb but it showing some error i don't understand it 我正在尝试将 mongodb 服务器与节点连接 - I am trying to connect mongodb server with node Docker-Compose:无法将NodeJS与Mongo和Redis连接[连接被拒绝] - Docker-Compose: Unable to connect NodeJS with Mongo & Redis [Connection Refused] 错误:无法在 127.0.0.1:6379 连接到 Redis:连接被拒绝 - Error: Could not connect to Redis at 127.0.0.1:6379:connection refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM