Centos7搭建redis一主多从启用哨兵

  • 准备工作

需要准备4台配置一样的Centos7,其中作为master的IP为192.168.1.154

作为slave的IP为:

192.168.1.151

192.168.1.152

192.168.1.153

将下载的redis-4.0.6.tar.gz文件移动到/usr/local/src/目录下

  • 编译安装redis

cd /usr/local/src

tar -zxvf redis-4.0.6.tar.gz

cd redis-4.0.6

make && make install PREFIX=/data/server/redis

cp *.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:

        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.1.154 6379

        appendfilename "appendonly_7001.aof"

        appendfsync everysec

哨兵:

master:

        vim sentinel.conf

        daemonize yes
               protected-mode no

        sentinel monitor mymaster 192.168.1.154 6379 4

slave:  vim sentinel.conf

        daemonize yes

protected-mode no

        sentinel monitor mymaster 192.168.1.154 6379 4

将服务启动:先启动master再启动slave

        ./redis-server redis.conf

        ./redis-sentinel sentinel.conf

  • 测试

查看主从信息

./redis-cli -h 192.168.1.154 -p 6379 info Replication

发送消息

./redis-cli -h 192.168.1.154 -p 6379

192.168.1.154:6379> set test hello

./redis-cli -h 192.168.1.152 -p 6379

192.168.1.152:6379> get test

测试当master服务器出问题时,slave是否变为master

将192.168.1.154机器服务停止:./redis-cli shutdown

在其他机器查看主从信息:./redis-cli -h 192.168.1.152 -p 6379 info Replication

你可能感兴趣的:(Linux服务,Centos7,redis,master,slave)