Redis: How to set the path to redis.sock

I was experimenting with a Python program the other day, which had me pulling my hair out. The program was simple enough, it’s purpose being to trace retweets of a given tweet via the Twitter API. 

For efficiency, the program connected to a Redis database within which followers of each user on the retweet trace path were stored, thereby allowing the program to jump straight into crawling next followers on subsequent executions.

The connection to the Redis instance was managed like this:

red = redis.Redis(unix_socket_path="/tmp/redis.sock")

I installed Redis on my Mac, together with the redis Python module. Everything until this point had gone swimmingly. 

I started an instance of Redis with $ redid-server and ran the Python program to take it for a test drive. The program started to work it’s magic, but failed half way through with an error saying that the path to unixsocket was incorrect and that redis.sock could not be found. 

After quite a bit of Googling around (and, probably only half reading the stuff that seemed remotely relevant to the issue), I resorted to some pretty crappy attempts to overcome the problem, all of which were hopeless. Then, I had a rummage in the Redis root directory and stumbled across the redis.conf file and the solution to the problem was revealed. If you’re having a similar problem, I hope this helps:

  1. Open redis.conf in a text editor.
  2. Scroll down the file until you get to the section dealing with Unix Sockets (around line 95 of the file in my version of Redis).
  3. At or around lines 101 and 102, you should see the following:
# unixsocket /tmp/redis.sock
# unixsocketperm 700

4. Uncomment both of these lines, so that they look like this:

unixsocket /tmp/redis.sock
unixsocketperm 700

5. Save the changes and exit.

6. Go to the terminal and start a new Redis instance with the following command:

$ redis-server redis.conf 

You should see this as the Redis server boots up:

32151:M 25 Jan 11:24:27.268 * The server is now ready to accept connections at /tmp/redis.sock

Success!