系統環境:centos7 redis版本:4.0.8 主服務器IP:192.168.20.101 6379 從服務器IP:192.168.20.99 6379 主服務器sentinel端口:26379 從服務器sentinel端口:26379
[root] # wget http://download.redis.io/releases/redis-4.0.8.tar.gz [root] # tar zxvf redis-4.0.8.tar.gz [root] # cd redis-4.0.8 [root] # make [root] # mkdir -p /usr/local/redis [root] # cp /usr/local/src/redis-4.0.8/src/redis-server /usr/local/redis/ [root] # cp /usr/local/src/redis-4.0.8/src/redis-cli /usr/local/redis/ [root] # cp /usr/local/src/redis-4.0.8/redis.conf /usr/local/redis/ [root] # cd /usr/local/redis [root] # vim /usr/local/redis/redis.conf
修改如下四項:html
bind 0.0.0.0 #接受全部來自於可用網絡接口的鏈接
daemonize yes #啓用後臺守護進程
protected-mode no #禁用保護模式
requirepass 123456 #設置密碼redis
[root] # vim /etc/init.d/redis
#!/bin/sh # description: Start and Stop redis #PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=6379 EXEC=/usr/local/redis/redis-server REDIS_CLI=/usr/local/redis/redis-cli PIDFILE=/var/run/redis_6379.pid CONF="/usr/local/redis/redis.conf" AUTH="123456" 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 "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
[root] # chmod 755 /etc/init.d/redis #可執行 [root] # chkconfig --add redis #添加系統服務
[root] # vim /etc/rc.local
在文件末尾添加如下內容vim
service redis start #開機啓動centos
開啓redis服務服務器
[root] # service redis start
查看進程網絡
[root] # ps aux | grep redis
主(master):192.168.20.101ide
從(slave):192.168.20.99ui
slaveof 192.168.20.101 6379
masterauth 123456centos7
查看主從信息3d
[root] # cd /usr/local/redis [root] # ./redis-cli -a 123456 [root] > info replication
主-中止redis
從-變主
從-保存數據,遷移數據到主redis
主-重啓redis服務
從-從新切換成從redis
從-又成爲只讀redis服務
[root] # cp /usr/local/src/redis-4.0.8/sentinel.conf /usr/local/redis/ #複製sentinel配置文件 [root] # vim /usr/local/redis/sentinel.conf
修改以下配置
daemonize yes
protected-mode no
sentinel monitor mymaster 192.168.20.101 6379 1
sentinel down-after-milliseconds mymaster 1000
sentinel failover-timeout mymaster 5000
sentinel auth-pass mymaster 123456
詳細配置及解釋見redis中文網
[root] # netstat -lap |grep 26379
[root] # ./redis-cli -h 192.168.20.101 -p 26379 [root] # >info
kill主redis進程
自動切換成功