目录
1、OS X 系统安装
$ brew install redis
安装完成
2、启动redis
-
直接启动
根据提示可以有两种:
$ brew services start redis
$ redis-server /path/to/redis.conf (推荐)
- 初始化脚本启动(推荐)
在redis源码的util目录下有一个redis_init_script的文件,将其拷贝至本地目录
/usr/local/etc/init.d
修改redis_init_script为redis_6379,修改内容如下
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/usr/local/etc/redis/run/redis_${REDISPORT}.pid # 配置pid文件位置
CONF="/usr/local/etc/redis/conf/${REDISPORT}.conf" # 配置redis配置文件
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
修改配置文件
参数 | 值 | 说明 |
daemonize | yes | 使redis以守护进程运行 |
pidfile | /usr/local/etc/redis/conf/redis_6379.pid | 设置redis的PID文件位置,与初始化脚本一致 |
port | 6379 | 设置redis监听的端口号 |
dir | /usr/local/var/db/redis/ | 设置持久化文件存放位置 |
启动
$ /usr/local/et/init.d/redis_6379 start # 启动
$ /usr/local/et/init.d/redis_6379 stop # 停止
$ redis-cli SHUTDOWN # 使用命令行停止
3、测试客户端连接
$ redis-cli PING
PONG
$ redis-cli
127.0.0.1:6379> echo hi
"hi"
127.0.0.1:6379> ping
$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379>