CentOS 6.5安装Redis及开机启动脚本
一:CentOS 6.5下载安装Redis
1、下载源码,解压缩后编译源码
# wget http://download.redis.io/releases/redis-2.8.3.tar.gz
# tar xzf redis-2.8.3.tar.gz
# cd redis-2.8.3
# make
2、进入安装目录的src文件夹下,有四个可执行文件redis-server、redis-benchmark、redis-cli和redis.conf,复制到同一个目录下
# mkdir /usr/redis
# cp redis-server /usr/redis
# cp redis-benchmark /usr/redis
# cp redis-cli /usr/redis
# cp redis.conf /usr/redis
# cd /usr/redis
3、启动Redis服务。
# cd /usr/redis
# ./redis-server redis.conf
启动异常:
情况一:
[17496] 08 Oct 11:48:09.153 # Server started, Redis version 2.8.17
[17496] 08 Oct 11:48:09.153 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[17496] 08 Oct 11:48:09.153 * The server is now ready to accept connections on port 6379
解决办法:编辑 /etc/sysctl.conf , 添加一项 vm.overcommit_memory = 1,重启生效。
4、客户端测试。
redis 127.0.0.1:6379> #显示此行意味着安装成功。
二:设置redis开机启动
环境:Linux系统为CentOS 6.6
1.编写启动脚本
注意:默认的redis.conf文件参数是前台启动的,修改daemonize no为daemonize yes则为后台启动。
脚本的编码格式在Windows上编码放在Linux可能不识别,可以用UltraEdit转换下格式“文件-->转换-->DOS 转 UNIX“
#!/bin/sh
#chkconfig: 345 86 14
#description: Startup and shutdown script for Redis
PROGDIR=/usr/redis #安装路径
PROGNAME=redis-server
DAEMON=$PROGDIR/$PROGNAME
CONFIG=/usr/redis/redis.conf
PIDFILE=/var/run/redis.pid
DESC="redis daemon"
SCRIPTNAME=/etc/rc.d/init.d/redis
start()
{
if test -x $DAEMON
then
echo -e "Starting $DESC: $PROGNAME"
if $DAEMON $CONFIG
then
echo -e "OK"
else
echo -e "failed"
fi
else
echo -e "Couldn't find Redis Server ($DAEMON)"
fi
}
stop()
{
if test -e $PIDFILE
then
echo -e "Stopping $DESC: $PROGNAME"
if kill `cat $PIDFILE`
then
echo -e "OK"
else
echo -e "failed"
fi
else
echo -e "No Redis Server ($DAEMON) running"
fi
}
restart()
{
echo -e "Restarting $DESC: $PROGNAME"
stop
start
}
list()
{
ps aux | grep $PROGNAME
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
list)
list
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|list}" >&2
exit 1
;;
esac
exit 0
把redis脚本文件放在 /etc/rc.d/init.d/ 目录下
2、增加服务并开机启动
# chmod +x /etc/rc.d/init.d/redis
# chkconfig --add redis
# chkconfig --level 345 redis on
# chkconfig --list redis
3、重启测试。
下面关于Redis的文章您也可能喜欢,不妨参考下:
Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里