My notes from FreeBSD redis install.
# Redis and FreeBSD ## Installing Install redis FreeBSD port collection: $ cd /usr/ports/databases/redis/ && make install clean # or with portmaster $ portmaster databases/redis These following programs will be installed with redis: - `/usr/local/bin/redis-sentinel` the Redis Sentinel executable (monitoring and failover). - `/usr/local/bin/redis-cli` the command line interface utility to talk with Redis. - `/usr/local/bin/redis-server` the Redis Server itself. - `/usr/local/bin/redis-benchmark` used to check Redis performances. **Redis configuration file** /usr/local/etc/redis.conf **Auto-start Redis at Boot** To run redis from startup, add `redis_enable="YES"` in your `/etc/rc.conf`. **Bind Redis to IP** To bind Redis to a single interface, add the following line to `/usr/local/etc/redis.conf`: bind 127.0.0.1 ## Commands **Stop/Start Redis** $ service redis stop $ service redis start **Accessing Redis** $ telnet localhost 6397 or the Redis CLI client $ redis-cli **Monitor Redis Transactions** $ redis-cli monitor **Server Statistics** $ redis-cli INFO **Changing Runtime Configuration** $ redis-cli CONFIG GET * gives you a list of all active configuration variables you can change. The output might look like this: redis 127.0.0.1:6379> CONFIG GET * 1) "dir" 2) "/var/lib/redis" 3) "dbfilename" 4) "dump.rdb" 5) "requirepass" 6) (nil) 7) "masterauth" 8) (nil) ... CONFIG SET timeout 900 Such a change will be effective instantly. When changing values consider also updating the redis configuration file. **Multiple Databases** Redis has a concept of separated namespaces called `databases`. You can select the database number you want to use with `SELECT`. By default the database with index 0 is used. So issuing: redis 127.0.0.1:6379> SELECT 1 OK redis 127.0.0.1:6379> switches to the second database. Note how the prompt changed and now has a `` to indicate the database selection. To find out how many databases there are you might want to run `redis-cli` from the shell: $ redis-cli INFO | grep ^db db0:keys=91,expires=88 db1:keys=1,expires=0 **Dropping Databases** To drop the currently selected database run: FLUSHDB to drop all databases at once run: FLUSHALL **Checking for Replication** To see if the instance is a replication slave or master issue: redis 127.0.0.1:6379> INFO [...] role:master and watch for the "role" line which shows either `master` or `slave`. Starting with version 2.8 the `INFO` command also gives you per slave replication status looking like this: slave0:ip=127.0.0.1,port=6380,state=online,offset=281,lag=0 **Enabling Replication** If you quickly need to set up replication just issue: SLAVEOF <IP> <port> on a machine that you want to become slave of the given IP. It will immediately get values from the master. Note that this instance will still be writable. If you want it to be read-only change the redis config file (only available in most recent version, e.g. not on Debian). To revert the slave setting run: SLAVEOF NO ONE **Dump Database Backup** As Redis allows RDB database dumps in background, you can issue a dump at any time. Just run: BGSAVE When running this command Redis will fork and the new process will dump into the `dbfilename` configured in the Redis configuration without the original process being blocked. Of course the fork itself might cause an interruption. Use `LASTSAVE` to check when the dump file was last updated. For a simple backup solution just backup the dump file. If you need a synchronous save run `SAVE` instead of `BGSAVE`. **Listing Connections** Starting with version 2.4 you can list connections with: CLIENT LIST and you can terminate connections with: CLIENT KILL <IP>:<port> **Monitoring Traffic** The propably most useful command compared to memcached where you need to trace network traffic is the `MONITOR` command which will dump incoming commands in real time. redis 127.0.0.1:6379> MONITOR OK 1371241093.375324 "monitor" 1371241109.735725 "keys" "*" 1371241152.344504 "set" "testkey" "1" 1371241165.169184 "get" "testkey" **Checking for Keys** If you want to know if an instance has a key or keys matching some pattern use `KEYS` instead of `GET` to get an overview. redis 127.0.0.1:6379> KEYS test* 1) "testkey2" 2) "testkey3" 3) "testkey" On production servers use `KEYS` with care as you can limit it and it will cause a full scan of all keys! ## PHP5-Extension for Redis cd /usr/ports/databases/php5-redis/ && make install clean # or pkg install databases/php5-redis ---- ## Resources [Most Important Redis Commands for Sysadmins](http://lzone.de/Most%20Important%20Redis%20Commands%20for%20Sysadmins)