CentOS系统redis主从模式实现

安装配置

# 安装
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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值