主redis:192.168.1.155:6379
从redis:192.168.1.155:6380
sentinel:192168.1.155:26379
将/usr/local/redis目录复制为如下图所示
修改里面的配置文件,数据文件目录等等
主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
可以看出从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