Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setting up Redis on OSX and CentOS (UNIX sockets)

Redis Setup

OSX + Homebrew

https://jeremy.wordpress.com/2012/07/05/multiple-redis-instances-on-mac-os-x-with-homebrew/

Conf files

Make a copy of redis.conf as redis-common.conf:

cp /usr/local/etc/redis{,-common}.conf

Use UNIX sockets instead of TCP. Edit redis-common.conf and change to port 0:

port 0

Create as many conf files as necessary for additional redis instances:

nano /usr/local/etc/redis-1.conf

Include these lines in conf files, changing numbers as necessary:

include /usr/local/etc/redis-common.conf
pidfile /usr/local/var/run/redis-1.pid
unixsocket /tmp/redis-1.sock
dbfilename dump-1.rdb

Launchd

Make a copy of redis.plist:

cp ~/Library/LaunchAgents/homebrew.mxcl.redis.plist ~/Library/LaunchAgents/custom1.redis.plist

Update its contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>KeepAlive</key>
    <false/>
    <key>Label</key>
    <string>custom1.redis</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/bin/redis-server</string>
      <string>/usr/local/etc/redis-1.conf</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>WorkingDirectory</key>
    <string>/usr/local/var</string>
    <key>StandardErrorPath</key>
    <string>/usr/local/var/log/redis-1.log</string>
    <key>StandardOutPath</key>
    <string>/usr/local/var/log/redis-1.log</string>
  </dict>
</plist>

Rinse and repeat for subsequent redis-* conf files you have.

Start, stop

Now load these Launchd files:

launchctl load -w ~/Library/LaunchAgents/custom*.redis.plist

You should have a bunch of redis-*.sock files created in /tmp:

ls /tmp

Use redis-cli to check status of each of your instances:

redis-cli -s /tmp/redis-1.sock

If you messed up somewhere, you might need to launchctl unload your .plist file.

CentOS

https://github.com/NodeRedis/node-redis/issues/204

Shorter way

Also take note of SELinux; make sure to set to permissive otherwise unix sockets won't work

In redis.conf:

# 0 = do not listen on a port
port 0

# listen on localhost only
bind 127.0.0.1

# create a unix domain socket to listen on
unixsocket /tmp/redis.sock

# set permissions for the socket
unixsocketperm 755

In your Node.js app:

var redis = require('redis');

var redisClient = redis.createClient('/tmp/redis.sock');

===

Longer way

Make a copy of redis.conf:

cp /etc/redis.conf /usr/local/etc/redis-common.conf

Give ownership of file to Redis and change permissions:

chown redis:redis /usr/local/etc/redis-common.conf
chmod 770 /usr/local/etc/redis-common.conf

Use unix sockets instead of TCP. Edit redis-common.conf and change to port 0:

port 0

Create additional conf files as necessary for additional instances:

nano /usr/local/etc/redis-1.conf

Add these lines and make necessary adjustments:

include /usr/local/etc/redis-common.conf
pidfile /var/run/redis-1.pid
unixsocket /tmp/redis-1.sock
unixsocketperm 777
dbfilename dump-1.rdb

Systemd

Make a copy of redis.service as a systemd template file. Take note of the @!

sudo cp /etc/systemd/system/multi-user.target.wants/redis.service /etc/systemd/system/custom-redis@.service

Notes:

  • We're putting it in /usr/local/ because it's for personal use. /etc is system default for OS confi files.
  • We're using a systemd template file because we only want to use one .service file to start multiple instances.

Edit your new service file. Take note of %i:

[Unit]
Description=Custom Redis Template
After=network.target

[Service]
ExecStart=/usr/bin/redis-server /usr/local/etc/redis-%i.conf --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

With a template file named as template@.service, you can use parameters in [Service] when launching it. Launch with systemctl:

sudo systemctl start /etc/systemd/system/custom-redis@1.service

Your parameters that come after @ will be used within the template service file as %i.

Check status:

sudo systemctl status custom-redis@1.service -l

Sock file should exist in /tmp:

ls /tmp

Once everything's running okay, enable start on boot:

sudo systemctl start custom-redis@1.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.