Could not connect to Redis at 127.0.0.1:6379: Connection refused with homebrew

334,008

Solution 1

After installing redis, type from terminal:

redis-server

And Redis-Server will be started

Solution 2

I found this question while trying to figure out why I could not connect to redis after starting it via brew services start redis.

tl;dr

Depending on how fresh your machine or install is you're likely missing a config file or a directory for the redis defaults.

  1. You need a config file at /usr/local/etc/redis.conf. Without this file redis-server will not start. You can copy over the default config file and modify it from there with

    cp /usr/local/etc/redis.conf.default /usr/local/etc/redis.conf
    
  2. You need /usr/local/var/db/redis/ to exist. You can do this easily with

    mkdir -p /usr/local/var/db/redis
    

Finally just restart redis with brew services restart redis.

How do you find this out!?

I wasted a lot of time trying to figure out if redis wasn't using the defaults through homebrew and what port it was on. Services was misleading because even though redis-server had not actually started, brew services list would still show redis as "started." The best approach is to use brew services --verbose start redis which will show you that the log file is at /usr/local/var/log/redis.log. Looking in there I found the smoking gun(s)

Fatal error, can't open config file '/usr/local/etc/redis.conf'

or

Can't chdir to '/usr/local/var/db/redis/': No such file or directory

Thankfully the log made the solution above obvious.

Can't I just run redis-server?

You sure can. It'll just take up a terminal or interrupt your terminal occasionally if you run redis-server &. Also it will put dump.rdb in whatever directory you run it in (pwd). I got annoyed having to remove the file or ignore it in git so I figured I'd let brew do the work with services.

Solution 3

If after install you need to run redis on all time, just type in terminal:

redis-server &

Running redis using upstart on Ubuntu

I've been trying to understand how to setup systems from the ground up on Ubuntu. I just installed redis onto the box and here's how I did it and some things to look out for.

To install:

sudo apt-get install redis-server

That will create a redis user and install the init.d script for it. Since upstart is now the replacement for using init.d, I figure I should convert it to run using upstart.

To disable the default init.d script for redis:

sudo update-rc.d redis-server disable

Then create /etc/init/redis-server.conf with the following script:

description "redis server"

start on runlevel [23]
stop on shutdown

exec sudo -u redis /usr/bin/redis-server /etc/redis/redis.conf

respawn

What this is the script for upstart to know what command to run to start the process. The last line also tells upstart to keep trying to respawn if it dies.

One thing I had to change in /etc/redis/redis.conf is daemonize yes to daemonize no. What happens if you don't change it then redis-server will fork and daemonize itself, and the parent process goes away. When this happens, upstart thinks that the process has died/stopped and you won't have control over the process from within upstart.

Now you can use the following commands to control your redis-server:

sudo start redis-server
sudo restart redis-server
sudo stop redis-server

Hope this was helpful!

Solution 4

This work for me :

sudo service redis-server start

Solution 5

It's the better way to connect to your redis.

At first, check the ip address of redis server like this.

ps -ef | grep redis

The result is kind of " redis 1184 1 0 .... /usr/bin/redis-server 172.x.x.x:6379

And then you can connect to redis with -h(hostname) option like this.

redis-cli -h 172.x.x.x

Share:
334,008

Related videos on Youtube

bufei
Author by

bufei

Updated on December 15, 2021

Comments

  • bufei
    bufei over 2 years

    Using homebrew to install Redis but when I try to ping Redis it shows this error:

    Could not connect to Redis at 127.0.0.1:6379: Connection refused
    

    Note : I tried to turn off firewall and edit conf file but still cannot ping. I am using macOS Sierra and homebrew version 1.1.11

  • Afolabi Olaoluwa
    Afolabi Olaoluwa over 6 years
    However, how do I stop it from listening to that port/host?
  • LuFFy
    LuFFy over 6 years
    @AfolabiOlaoluwaAkinwumi Please Refer How can I stop redis-server
  • Pradeep Kumar
    Pradeep Kumar about 6 years
    For windows from Command Prompt, cd C:\Program Files\Redis and then redis-server
  • luckyguy73
    luckyguy73 about 6 years
    thanks, this solved my problem on a laravel project where i was getting connection refused in abstractconnection.php line 155
  • Kevin Gagnon
    Kevin Gagnon almost 6 years
    I was having an error in abstractconnection.php line 155 too! Crazy.
  • SangamAngre
    SangamAngre about 5 years
    Start with verbose did the thing for me. I was having an issue in redis.conf. Thanks
  • AATHITH RAJENDRAN
    AATHITH RAJENDRAN almost 5 years
    how to run redis-server continuously in the background, I'm using ubuntu @LuFFy
  • LuFFy
    LuFFy almost 5 years
    @AATHITHRAJENDRAN, Check this answer, I hope it will help
  • mwilson
    mwilson over 4 years
    It helps to run brew install redis first (fyi)
  • Jhune Carlo Trogelio
    Jhune Carlo Trogelio almost 4 years
    I am using Windows WSL Ubuntu, and keeps getting errors in my terminal. npm ERR! code ELIFECYCLE npm ERR! errno 1 So tried everything on the top, it does not work especially in Windows. That's why tried restarting the redis service on the Ubuntu and works perfectly fine! That's all.
  • Arash Yazdani
    Arash Yazdani over 3 years
    You have to add redis.exe path after installed it. You can add it from Control panel> System>A dvanced System Settings> Environment Variables> Path> Edit> New and then restart your system and enjoy it. My Redis path was: C:\Program Files\Redis
  • jmcgrory
    jmcgrory over 3 years
    brew services restart redis was key for me here, all other conf files etc. were set up correctly- the service apparently was just in some trouble!
  • Iman
    Iman over 3 years
    set & at the end of the command to run it in background
  • salimsaid
    salimsaid over 3 years
    this still works on ubuntu 16.04 and redis 6.0.6, great
  • zypherman
    zypherman over 3 years
    This is on a new install though, so I don't think the issue is a weird redis.conf.
  • Kingston Fortune
    Kingston Fortune about 2 years
    Thanks, I am on an Intel MacBook and this worked for me. first, run redis-server --daemonize yes and then redis-cli ping for a pong or just redis-cli to run quick commands in the terminal.
  • Frank Guo
    Frank Guo about 2 years
    yeas, this one works great!! thanks. Macos apple M1, moncentary