Could not connect to Redis at 127.0.0.1:6379 Connection refused

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Table of Contents

  1. Could not connect to Redis at 127.0.0.1:6379 Connection refused
  2. Start your Redis server using brew on macOS
  3. Start your Redis server on Linux
  4. Make sure that the 6379 port is not blocked by your firewall
  5. Make sure that your user has the necessary permissions to read and write Redis files

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

The error "Could not connect to Redis at 127.0.0.1:6379: Connection refused" occurs when you try to connect to your Redis server without having started it first.

To resolve the issue, open your terminal and run the redis-server connect to start your Redis server before connecting to it.

could not connect to redis connection refused

Here is the complete error message.

bash:shell
Could not connect to Redis at 127.0.0.1:6379: Connection refused not connected>

Open your terminal and run the following command to start your Redis server.

shell
redis-server

start redis server

If you are on Windows, you might have to cd into the Redis directory before issuing the redis-server command.

cmd
cd "C:\Program Files\Redis" redis-server

If you want to start the Redis server in the background, issue the following command instead.

shell
redis-server &

start redis server in the background

You can also try to run the command with the --daemonize flag if that didn't work.

shell
redis-server --daemonize yes

start redis server with daemonize flag

Run the redis-cli command to verify the Redis server has started.

shell
redis-cli set name "bobby hadz" get name

verify redis server has started

You can also use the redis-cli ping command to verify that your Redis server is running.

shell
redis-cli ping

redis cli ping command

If you get a PONG response back, then the issue is resolved.

If you get a permissions error when running the redis-server command on macOS or Linux, rerun the command with sudo.

shell
sudo redis-server # or sudo redis-server --daemonize yes

# Start your Redis server using brew on macOS

Make sure you've installed Redis.

shell
brew install redis

If you are on macOS and the issue persists, try using Homebrew to start your Redis server.

shell
brew services start redis
  1. If the issue persists, check if you have a Redis config file at /usr/local/etc/redis.conf.
shell
cat /usr/local/etc/redis.conf
  1. If you don't have a config file at /usr/local/etc/redis.conf, then copy the default Redis config file over.
shell
cp /usr/local/etc/redis.conf.default /usr/local/etc/redis.conf
  1. Run the following command to create the /usr/local/var/db/redis directory on your machine.
shell
mkdir -p /usr/local/var/db/redis
  1. Run the following command to restart your Redis server.
shell
brew services restart redis

# Start your Redis server on Linux

Make sure you have Redis installed.

shell
# prerequisite package sudo apt install lsb-release # add the repository to the apt index curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list # install Redis sudo apt-get update sudo apt-get install redis

You can also install Redis from Snapcraft.

shell
sudo snap install redis

If you are on Linux, run one of the following commands to start your Redis server.

shell
sudo service redis start # or sudo service redis-server start # or sudo systemctl start redis # or sudo systemctl start redis-server.service

start redis server linux

You can also try to restart your Redis server.

shell
sudo service redis restart # or sudo systemctl restart redis # or sudo service redis-server restart # or sudo systemctl restart redis-server.service

If you get an error when starting your server, issue the following command

shell
ps aux | grep redis

find process identifier

The command will return the process ID of Redis.

Run the following command to stop the process.

shell
kill -9 <YOUR_PROCESS_ID>

In my case, the process ID is 1014628, so I would issue the following command.

shell
kill -9 1014628

After you stop the process, start your Redis server with one of the following commands.

shell
sudo service redis start # or sudo service redis-server start # or sudo systemctl start redis # or sudo systemctl start redis-server.service

# Make sure that the 6379 port is not blocked by your firewall

If the issue persists, make sure that the 6379 port is not blocked by your firewall.

Open your terminal and run the following command.

shell
sudo ufw allow 6379

# Make sure that your user has the necessary permissions to read and write Redis files

If the issue persists, make sure that your user has the necessary permissions to read, write and execute Redis files.

Your user should have access to the files in the following directories:

  • /etc/redis
  • /run/redis
  • /var/log/redis
  • /var/lib/redis

If you need to change the permissions of the directories to the current user, run the following commands.

shell
sudo chown -R redis:redis /etc/redis. sudo chown -R redis:redis /run/redis sudo chown -R redis:redis /var/log/redis sudo chown -R redis:redis /var/lib/redis

Try to restart your Redis server after making the change.

shell
sudo service redis start # or sudo service redis-server start # or sudo systemctl start redis # or sudo systemctl start redis-server.service

If you get the error MISCONF Redis is configured to save RDB snapshots, click on the link and follow the instructions.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.