標籤:redis
主redis:192.168.1.155:6379
從redis:192.168.1.155:6380
sentinel:192168.1.155:26379
將/usr/local/redis目錄複寫為如所示
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/84/94/wKioL1eVl66jNtdDAAArzZwfyQU225.jpg" title="1.jpg" alt="wKioL1eVl66jNtdDAAArzZwfyQU225.jpg" />修改裡面的設定檔,資料檔案目錄等等
主redis port:6379
從redis port:6380
sentinel:26379
將從redis設定檔中進行此處修改:
#slaveof masterip masterport
slaveof  192.168.1.155 6379:在從設定檔中指定主redis的ip以及連接埠
當然你需要加密也可以定義加密登入
#masterauth master_passwd:master需要驗證密碼登入
masterauth  mypass(當master通過redis-cli -p 6379登入時後面需要加上-a mypass參數進行密碼驗證)
登入master redis
redis-server /usr/local/redis/etc/redis.conf
netstat -tunlp:查看連接埠是否已經起來
redis-cli -p 6379:登入redis
>info replication(info命令查看redis的資訊)
# Replication
role:master
connected_slaves:0:由於從沒有上線
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
同理按照方式進入到slave redis服務
redis-server /usr/local/redis-slave/etc/redis-slave.conf
redis-cli -p 6380
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M02/84/94/wKiom1eVm4Cw-XnKAABu1KjROjw011.jpg" title="2.jpg" alt="wKiom1eVm4Cw-XnKAABu1KjROjw011.jpg" />
可以看出從redis也已經起來了,登入slave redis查看replication狀態
# Replication
role:slave
master_host:::1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:169
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
再次查看master redis中的info replication資訊
# Replication
role:master
connected_slaves:1
slave0:ip=::1,port=6380,state=online,offset=309,lag=0
master_repl_offset:309
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:308
由上面資訊可以明顯看出主從已然完成
接下來配置sentinel哨兵模式
由上面已經準備好了redis-sentinel,連接埠已經更改了,於是
將哨兵的設定檔sentinel.conf複製到/usr/local/redis-sentinel/etc/sentinel.conf
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel  monitor mymaster 192.168.1.155 6379 1(quorum:法定票數,用來決定容錯移轉)
然後啟動sentinel
redis-server /usr/local/redis-sentinel/etc/sentinel.conf --sentinel(後面增加model)
登入sentinel:
redis-cli -p 26379
>info利用info命令可以查看master和slave資訊
各連接埠指令碼如下:
redis_6379
#!/bin/bash
#
redis_cmd="/usr/local/redis/bin"
prog=redis
lockfile="/var/lock/subsys/redis"
redis_conf="/usr/local/redis/etc/redis.conf"
redis_port=6379
. /etc/init.d/functions
start() {
$redis_cmd/redis-server $redis_conf
[ $? -eq 0 ] && {
action "Starting $prog" /bin/true
touch $lockfile
}
}
stop() {
$redis-cli -p $redis_port shutdown
[ $? -eq 0 ] && {
action "Stopping $prog" /bin/true
rm -f $lockfile
}
}
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    ;;
esac
redis_6380
#!/bin/bash
#
redis_port=6380
redis_home="/usr/local/redis/bin"
prog="redis_6380"
redis_conf="/usr/local/redis-slave/etc/redis-slave.conf"
lockfile="/var/lock/subsys/redis_6380"
. /etc/init.d/functions
start() {
$redis_home/redis-server $redis_conf
[ $? -eq 0 ] && {
action "Starting $prog" /bin/true
touch $lockfile
}
}
stop() {
$redis_home/redis-cli -p $redis_port
[ $? -eq 0 ] && {
action "Stopping $porg" /bin/true
rm -f $lockfile
}
}
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    ;;
esac
sentinel哨兵啟動指令碼:
#!/bin/bash
#
redis_cmd="/usr/local/redis/bin"
prog="redis_sentinel"
lockfile="/var/lock/subsys/redis_sentinel"
redis_conf="/usr/local/redis-sentinel/etc/sentinel.conf"
redis_port=26379
. /etc/init.d/functions
start() {
$redis_cmd/redis-server $redis_conf --sentinel(一定要帶上哨兵模式)
[ $? -eq 0 ] && {
action "Starting $prog" /bin/true
touch $lockfile
}
}
stop() {
$redis_cmd/redis-cli -p $redis_port
[ $? -eq 0 ] && {
action "Stopping $prog" /bin/true
rm -f $lockfile 
}
}
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    ;;
esac
參考文章:http://blog.csdn.net/lifetragedy/article/details/50660310
redis之主從以及sentinel的建立