安装配置
# 安装
yum install epel-release (centos 7可以直接安装epel源)
yum install redis
# 配置
vi /etc/redis.conf
vi /etc/redis-sentinel.conf
# 启动
redis-server /etc/redis.conf
redis-sentinel /etc/redis-sentinel.conf
redis.conf常用设置
# bind 127.0.0.1
protected-mode no
port 6379
daemonize yes
maxclients 10000
requirepass <password>
appendonly yes
# slaveof及masterauth仅在从节点设置
slaveof <masterip> <masterport>
masterauth <master-password>
redis-sentinel.conf常用设置
protected-mode no
daemonize yes
port 26397
sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel auth-pass <master-name> <password>
开机自启
# 编辑
vi /etc/init.d/redisd
# 赋权
chmod 755 /etc/init.d/redisd
# 尝试
/etc/init.d/redisd start
service redisd start/stop/restart
# 自启
chkconfig redisd on
redisd示例
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
REDISPWD='123456'
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.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
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT -a $REDISPWD shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Please use start|stop|restart|force-reload as first argument"
;;
esac
redis-sentineld示例
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis Sentinel
REDISPORT=26379
EXEC=/usr/bin/redis-sentinel
CLIEXEC=/usr/bin/redis-cli
CONF="/etc/redis-sentinel.conf"
NAME="Redis Sentinel"
PID=$(netstat -lnopt | grep :$REDISPORT | awk '/redis-sentine/{gsub(/\/redis-sentine/,"",$7);print $7;}' | head -1)
case "$1" in
start)
if [ -z ${PID} ]
then
echo "Starting $NAME server..."
$EXEC $CONF
echo "start at port:$REDISPORT"
else
echo "$NAME server already start,pid:$PID"
fi
;;
stop)
if [ -z ${PID} ]
then
echo "not find $NAME server on port:$REDISPORT"
else
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -z ${PID} ]
do
echo "Waiting for $NAME server to shutdown ..."
sleep 1
done
echo "$NAME server stopped"
fi
;;
status)
if [ -z ${PID} ]
then
echo "not find $NAME server on port:$REDISPORT"
else
echo "$NAME server is running,pid:$PID"
fi
;;
restart)
${0} stop
${0} start
;;
*)
echo "Please use start|stop|restart|status as first argument"
;;
esac