Redis error: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379

12,072

You have provided mutually exclusive parameters together in options for redisStore. You either pass the host and port or just existing redis client.

Quoting connect-redis docs -

A Redis client is required. An existing client can be passed directly using the client param or created for you using the host, port, or socket params

Also there were some reference errors which I have corrected in this snippet.

var express = require('express');
var redis = require("redis");
var session = require('express-session');
var redisStore = require('connect-redis')(session);
var parser = require('body-parser');
var RedisClient= redis.createClient();

var app = express();

app.use(session({
    secret: 'mysecret',
    store: new redisStore({client: RedisClient,ttl : 260}),
    saveUninitialized: false,
    resave: false,
    cookie: { secure: true }
}));

RedisClient.on('error', function(err) {
    console.log('Redis error: ' + err);
});

RedisClient.on("ready",function () {
    console.log("Redis is ready");
});
Share:
12,072
fyeah
Author by

fyeah

Updated on June 04, 2022

Comments

  • fyeah
    fyeah almost 2 years

    When I checked my redis-server status , it says redis server is running but I got this error when I log RedisClient.on('error', function(err) { console.log('Redis error: ' + err);// Redis error: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 }); and when I try to console the req.session it returns undefined. any clue about this issue? please help me I am new to this. thank you

    heres my setup:

    var express = require('express');
    var redis = require("redis");
    var session = require('express-session');
    var redisStore = require('connect-redis')(session);
    var parser = require('body-parser');
    var RedisClient= redis.createClient();
    
    app.use(session({
    secret: 'myseceret',
    store: new redisStore({host: '127.0.0.1', port: 6379, client: client,ttl : 260}),
    saveUninitialized: false,
    resave: false,
    cookie: { secure: true }
    }));
    client.on('error', function(err) {
    console.log('Redis error: ' + err);
    });

  • fyeah
    fyeah about 7 years
    hi thank you. I've tried the code you provide and still got the same issue :(
  • fyeah
    fyeah about 7 years
    when I check redis server by redis-cli ping. it says: Could not connect to Redis at 127.0.0.1:6379: Connection refused
  • Tabish Rizvi
    Tabish Rizvi about 7 years
    Hi @fyeah , can you tell me your os environment, so I can help you further
  • fyeah
    fyeah about 7 years
    I was trying to setup redis in my live production server using ubuntu 16. its really my first time to set things up.please help me
  • Tabish Rizvi
    Tabish Rizvi about 7 years
    Check whether redis server is running by using this command sudo service redis status. If it says stopped, start it by sudo service redis start else check the redis configuration file to check on which port it is running.
  • fyeah
    fyeah about 7 years
    hey, I tried the command sudo service redis status and give me this response: sudo: unable to resolve host api
  • fyeah
    fyeah about 7 years
    ● redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-serve Active: active (running) since Fri 2017-02-17 0 Docs: redis.io/documentation,
  • fyeah
    fyeah about 7 years
    man:redis-server(1) Process: 1143 ExecStartPost=/bin/run-parts --ver Process: 1138 ExecStart=/usr/bin/redis-server /e Process: 1075 ExecStartPre=/bin/run-parts --verb Main PID: 1140 (redis-server) CGroup: /system.slice/redis-server.service └─1140 /usr/bin/redis-server 127.0.0.1: Feb 17 02:53:36 api systemd[1]: Starting Advanced Feb 17 02:53:36 api run-parts[1075]: run-parts: ex Feb 17 02:53:36 api run-parts[1143]: run-parts: ex Feb 17 02:53:36 api systemd[1]: Started Advanced k lines 1-16/16 (
  • fyeah
    fyeah about 7 years
    hi, I restarted my server and rerun sudo service redis start, I think I am not connected with redis (its weird bec I din't do anything) and seems like everything went fine bec I dont get Connection refused err.
  • fyeah
    fyeah about 7 years
    and when I run redis-cli its says : 127.0.0.1:6379> am I on the right track?
  • Tabish Rizvi
    Tabish Rizvi about 7 years
    @fyeah , so are you saying you are not getting any further Connection refused error?
  • fyeah
    fyeah about 7 years
    yep i don't. Redis is ready is now logging. but one more thing, I use passport for authentication. authentication went well, but it seem doesn't store data on req.session
  • Tabish Rizvi
    Tabish Rizvi about 7 years
    @fyeah sorry I dont have much expertise in passportJS. Why dont you ask this as a separate question? You may be able to get relevant answers from passport JS community.
  • fyeah
    fyeah about 7 years
    alright thank you @TabishRizvi much help. thank you so much