CentOS 7 搭建redis 一主多从 哨兵

master:   192.168.43.11
slave:    192.168.43.12
slave:    192.168.43.13
slave:    192.168.43.14

哨兵1:(sentinel1) :192.168.43.11
哨兵2:(sentinel2) :192.168.43.12
哨兵3:(sentinel3) :192.168.43.13
哨兵4:(sentinel4) :192.168.43.14

##环境安装
    yum install gcc gcc-c++ tcl* -y

    cd /usr/local/src
    wget https://raw.githubusercontent.com/ww1230258u/server/master/redis-4.0.6.tar.gz
    tar -zxvf redis-4.0.6.tar.gz
    cd redis-4.0.6
    make && make install PREFIX=/data/server/redis

# 复制解压缩的redis中的redis.conf和sentinel.conf 文件到/data/server/redis/bin
    cp /usr/local/src/redis-4.0.6/redis.conf sentinel.conf /data/server/redis/bin/
##主从配置
master:
        --> vim redis.conf
        
        bind 0.0.0.0
        protected-mode no  
        daemonize yes
        slave-read-only yes
        slave-priority 100
        appendonly yes
        appendfilename "appendonly_7001.aof"
        appendfsync everysec
slave:(1~3)
        --> vim redis.conf
        
        bind 0.0.0.0
        protected-mode no  
        daemonize yes  
        slave-read-only yes
        slave-priority 90
        appendonly yes
        slaveof 192.168.43.11 6379
        appendfilename "appendonly_7001.aof"
        appendfsync everysec
        

##哨兵配置

master:
        --> vim sentinel.conf
        
        daemonize yes
        protected-mode no
        sentinel monitor redis1 192.168.43.11 6379 4
        sentinel down-after-milliseconds mymaster 20
        sentinel failover-timeout mymaster 60

slave:
        --> vim sentinel.conf
        
        daemonize yes
        protected-mode no
        sentinel monitor redis1 192.168.43.11 6379 4
        sentinel failover-timeout mymaster 60

##启动
--启动redis
    ./redis-server redis.conf

--启动哨兵
    ./redis-sentinel sentinel.conf

--关闭服务
    ./redis-cli shutdown (#pkill -9 redis)
      

##查看主从信息
./redis-cli -h 192.168.43.11 -p 6379 info Replication

[root@zs bin]#  ./redis-cli -h 192.168.43.11 -p 6379 info Replication
# Replication
role:master
connected_slaves:3
slave0:ip=192.168.43.14,port=6379,state=online,offset=462712,lag=0
slave1:ip=192.168.43.12,port=6379,state=online,offset=462416,lag=0
slave2:ip=192.168.43.13,port=6379,state=online,offset=462557,lag=0
master_replid:c6bf2b47df50ae1f636784de82d4e1a43faf0ec3
master_replid2:caf44c857ade788b27a71721b6abae3bbb2e99fb
master_repl_offset:462712
second_repl_offset:384453
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:380172
repl_backlog_histlen:82541

##验证
[root@zs bin]#  ./redis-cli -h 192.168.43.11 -p 6379
192.168.43.11:6379> set k1 1111
OK
192.168.43.11:6379> set k2 2222
OK
192.168.43.11:6379> exit
[root@zs bin]#  ./redis-cli -h 192.168.43.12 -p 6379
192.168.43.12:6379> get k1
"1111"
192.168.43.12:6379> set k3 3333
(error) READONLY You can't write against a read only slave.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值