Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to remote redis server

Tags:

I wanted to make some changes in redis.conf, so that whenever i type redis-cli it connects me to redis installed on remote server.

I know that we can connect to redis installed on remote server by :

redis-cli -h 'IP-Address-Of-Server'.  

But actually, I have some bash scripts and in those scripts i have used redis-cli at many place. So instead of replacing redis-cli with redis-cli -h 'IP-Address-Of-Server' in each file, I wanted to somehow change redis configuration, so that by default it connects me to the remote server. I hope it make sense :)

like image 314
user1304683 Avatar asked May 29 '13 15:05

user1304683


People also ask

How do I access Redis server?

To start Redis client, open the terminal and type the command redis-cli. This will connect to your local server and now you can run any command. In the above example, we connect to Redis server running on the local machine and execute a command PING, that checks whether the server is running or not.


2 Answers

there is no good reason to touch redis conf for this.

just make a script that wraps redis-cli with the desired parameters to connect to the remote host

eg. create a redis-cli-remotename.sh

#!/bin/sh redis-cli -h remote.host_name 

and give it +x permissions (eg. chmod +x redis-cli-remotename.sh)

like image 152
Tommaso Barbugli Avatar answered Oct 22 '22 14:10

Tommaso Barbugli


Like Tommaso said, this is no good reason to touch the redis conf for this purpose. Instead what you can do is use environment variables to in your bash scripts to execute the command and then use that environment variable wherever you've used redis-cli directly.

For eg. $REDIS_CONNECTION="redis-cli -h "

If at any future point in time, you decide to change the host you want to connect to, its simply a matter of changing the env variables value.

Replacing redis-cli with the environment variable is pretty straightforward with sed in all the files. So that shouldn't be much of a hassle.

like image 41
ron Avatar answered Oct 22 '22 14:10

ron