Redis - Connect to Remote Server

UbuntuConfigurationRedis

Ubuntu Problem Overview


I've just install Redis succesfully using the instructions on the Quick Start guide on http://redis.io/topics/quickstart on my Ubuntu 10.10 server. I'm running the service as dameon (so it can be run by init.d)

The server is part of Rackspace Cluster with Internal and External IPs. The host is running on port 6379 (standard for Redis)

I've added a row in the iptables to allow incoming connections from port 6379 as shown below:

 ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:6379 

In my PHP code on another server, I'm trying to connect to the new Redis server here:

$this->load->helper("iredis");
	
$hostname = "IP ADDRESS HERE";
	
$redis = new iRedis(array('hostname' => $hostname, 'port' => 6379));

Once I do this - I always get a connection refused. In my redis.conf file, I have the local bind command commented out, so it should be listening on more than the localhost IP. I can connect to the database on the local machine just not on another server. I've tried the external and internal IPs with no luck.

Any suggestions on getting this to work?

Ubuntu Solutions


Solution 1 - Ubuntu

I've been stuck with the same issue, and the preceding answer did not help me (albeit well written).

The solution is here : check your /etc/redis/redis.conf, and make sure to change the default

bind 127.0.0.1

to

bind 0.0.0.0

Then restart your service (service redis-server restart)

You can then now check that redis is listening on non-local interface with

redis-cli -h 192.168.x.x ping

(replace 192.168.x.x with your IP adress)

Important note : as several users stated, it is not safe to set this on a server which is exposed to the Internet. You should be certain that you redis is protected with any means that fits your needs.

Solution 2 - Ubuntu

First I'd check to verify it is listening on the IPs you expect it to be:

netstat -nlpt | grep 6379

Depending on how you start/stop you may not have actually restarted the instance when you thought you had. The netstat will tell you if it is listening where you think it is. If not, restart it and be sure it restarts. If it restarts and still is not listening where you expect, check your config file just to be sure.

After establishing it is listening where you expect it to, from a remote node which should have access try:

redis-cli -h REMOTE.HOST ping

You could also try that from the local host but use the IP you expect it to be listening on instead of a hostname or localhost. You should see it PONG in response in both cases.

If not, your firewall(s) is/are blocking you. This would be either the local IPTables or possibly a firewall in between the nodes. You could add a logging statement to your IPtables configuration to log connections over 6379 to see what is happening. Also, trying he redis ping from local and non-local to the same IP should be illustrative. If it responds locally but not remotely, I'd lean toward an intervening firewall depending on the complexity of your on-node IP Tables rules.

Solution 3 - Ubuntu

In addition to the excellent answer given by Orabîg:

I resolved this issue by removing the bind section entirely and setting protected-mode to no.

#bind 127.0.0.1
protected-mode no

Never use this method on publicly exposed servers.

Solution 4 - Ubuntu

I was struggling with the remote connection to Redis for some days. Finally I made it. Here is the full check list I put together to follow to get connected. Some of solutions are given in the answers above. Yet I wanted my answer to be a nano-wiki on the subject:) I added some useful links too.

If redis works locally:
$ redis-cli
127.0.0.1:6379>ping
PONG
127.0.0.1:6379>
If the password is not set

See /etc/redis/redis.conf config (this is default locaion for Ubuntu 18.04, you may have it in the different location):

# The following line should be commented
# requirepass <some pass if any>
If the protected mode is set to 'no' in the config:
# The following line should be uncommented
protected-mode no
if the IP binding is open for an access from internet in the config:
# The following line should be commented
# bind 127.0.0.1 ::1
If the Linux firewall allows connections

(here for Ubuntu 18.04) Check it allows for incoming internet traffic to go to port 6379 (the Redis default port)

# To check if it the port is open
$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
...
6379/tcp                   ALLOW       Anywhere
6379/tcp (v6)              ALLOW       Anywhere (v6)
...

# To open the port
$ sudo ufw allow 6379/tcp
Restart Redis service

Do not forget to restart the Redis service for changes to take effect and see it is running:

$ sudo systemctl restart redis.service
$ sudo systemctl status redis
Check if it works as a remote server

from your command line use redis-cli as if Redis server were on the remote server:

$ redis-cli -h <your-server-ip>
<your-server-ip>:6379> ping
PONG
<your-server-ip>:6379> exit
$

If you can ping-PONG your Redis server via your internet server connected as a remote server than the remote Redis connection works.

Security Warning

All the above makes your Redis data to be completely open to anybody from the internet.

To basically secure Redis use requirepass and protected-mode yes settings in Redis config (see above) and block the dangerous Redis commands (see the link above), for a deeper understanding see this article and Redis site security section ).

Some links to help How to install and secure Redis on Ubuntu 18.04 and how to setup Ubuntu 18.04 firewall.

Hope it helps.

Solution 5 - Ubuntu

Orabig is correct.

You can bind 10.0.2.15 in Ubuntu (VirtualBox) then do a port forwarding from host to guest Ubuntu.

in /etc/redis/redis.conf

bind 10.0.2.15

then, restart redis:

sudo systemctl restart redis

It shall work!

Solution 6 - Ubuntu

  • if you downloaded redis yourself (not apt-get install redis-server) and then edited the redis.conf with the above suggestions, make sure your start redis with the config like so: ./src/redis-server redis.conf

  •                                                                            also side note i am including a screenshot of virtual box setting to
    

    connect to redis, if you are on windows and connecting to a virtualbox vm.

enter image description here

Solution 7 - Ubuntu

Setting tcp-keepalive to 60 (it was set to 0) in server's redis configuration helped me resolve this issue.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongregavolaView Question on Stackoverflow
Solution 1 - UbuntuOrabîgView Answer on Stackoverflow
Solution 2 - UbuntuThe Real BillView Answer on Stackoverflow
Solution 3 - UbuntuzenView Answer on Stackoverflow
Solution 4 - UbuntuValentine ShiView Answer on Stackoverflow
Solution 5 - UbuntuMichael QinView Answer on Stackoverflow
Solution 6 - UbuntuRobot70View Answer on Stackoverflow
Solution 7 - UbuntuSaurabhView Answer on Stackoverflow