我正在使用Redis包用于Node.js(ioredis
),我在ScaleGrid上托管了一个Redis集群,我试图从heroku连接到该集群,但是我一直收到错误[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 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 }),
}));
请问我该如何解决这个问题?以及为什么我仍然得到这个Error: connect ECONNREFUSED 127.0.0.1:6379
PS Redis可以在我的本地主机上完美运行
答案 0 :(得分:0)
我意识到我所需要的只是在Redis客户端连接(client.auth(password)
之后立即发送用于身份验证的密码。
我必须为redislabs enterprise
做好自己的Redis托管服务,并且由于一些奇怪的错误,还不得不从ioredis
软件包更改为redis
软件包。这是下面的代码
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;
这是可选的,仅显示索引文件中如何使用连接的Redis客户端。
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 }),
}));
这在localhost和联机上均有效。希望这对某人有帮助