Redis 安装步骤
1. 下载Redis
(1)获取权限:su root
(2)规划好下载的目录:mkdir /usr/local/soft/
(3)进入新建的目录,准备下载:cd /usr/local/soft/
(4)下载redis最新稳定版:
wget http://download.redis.io/releases/redis-3.0.0.tar.gz
2. 解压并安装redis
(1)解压tar包:tar zxvf redis-3.0.0.tar.gz
(2)进入解压后出现的新目录:cd redis-3.0.0
(3)编译源码:make
(4)校验编译是否正确:make test
3. 手工启动redis,测试redis是否运行正常
(1)在当前redis目录中输入命令启动Redis:src/redis-server
(2)检查进程是否有Redis了:ps aux | grep redis,可以看见进程列表中有一个叫“src/redis-server”的进程了
(3)启动redis自带的客户端,进行测试:
启动客户端并连接本地Redis: src/redis-cli
> set foo bar -- 提示 “ok”说明设置键值正常。
> get foo -- 能够提示返回正确的“bar”,说明运行正常。
> quit -- 退出客户端
4. 将Redis加入开机自启动序列
(1)redis.conf 配置文件里面的daemonize no 改成 daemonize yes
(2)编写脚本/etc/init.d/redis:
###########################
#chkconfig: 2345 10 90
#description: Start and Stop redis
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379 #实际环境而定
EXEC=/usr/local/soft/redis-3.0.0/src/redis-server #实际环境而定
REDIS_CLI=/usr/local/soft/redis-3.0.0/src/redis-cli #实际环境而定
PIDFILE=/var/run/redis.pid
CONF="/usr/local/soft/redis-3.0.0/redis.conf" #实际环境而定
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
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Redis is stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
##############################
(3)执行权限: chmod +x /etc/init.d/redis
(4)开机启动 :
# 尝试启动或停止redis
service redis start
service redis stop
# 开启服务自启动
sudo chkconfig redis on