[英]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.