一、下載redis3.2.3redis
wget http://download.redis.io/releases/redis-3.2.3.tar.gz tar -zxvf redis-3.2.3.tar.gz cd redis-3.2.3 make make test //檢查一下 看有沒有問題 make install
安裝完以後,會在src目錄下生成幾個可執行文件,分別是mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server。其中redis-server是啓動Redis服務的,redis-cli是進入Redis客戶端的。工具
mkdir etc mkdir bin mv redis.conf etc/ mv sentinel.conf etc/ mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-sentinel redis-server redis-trib.rb redis-cli ../bin cp -R /usr/local/jenkins/redis/redis-3.2.3 /usr/local/jenkins/redis/redis-master-3.2.3 cp -R /usr/local/jenkins/redis/redis-3.2.3 /usr/local/jenkins/redis/redis-slave-3.2.3
二、配置主從redis測試
cd /usr/local/jenkins/redis/redis-master-3.2.3/etc/ cp redis.conf master.conf cd /usr/local/jenkins/redis/redis-slave-3.2.3/etc/ cp redis.conf slave_6380.conf cp redis.conf slave_6381.conf mkdir /usr/local/jenkins/redis/redis-master-3.2.3/logs mkdir /usr/local/jenkins/redis/redis-master-3.2.3/run mkdir /usr/local/jenkins/redis/redis-slave-3.2.3/logs mkdir /usr/local/jenkins/redis/redis-slave-3.2.3/run
修改主配置文件master.confui
port 6379 pidfile /usr/local/jenkins/redis/redis-master-3.2.3/run/redis_6379.pid # slaveof <masterip> <masterport> logfile "/usr/local/jenkins/redis/redis-master-3.2.3/logs/redis.master.log" requirepass 123456 daemonize yes bind 0.0.0.0 masterauth 123456
修改從配置文件slave_6380.conf(slave_6381.conf)spa
port 6380 pidfile /usr/local/jenkins/redis/redis-slave-3.2.3/run/redis_6380.pid slaveof 127.0.0.1 6379 logfile "/usr/local/jenkins/redis/redis-slave-3.2.3/logs/redis.slave6380.log" requirepass 123456 daemonize yes bind 0.0.0.0 masterauth 123456
三、啓動redis日誌
./redis-server /usr/local/jenkins/redis/redis-master-3.2.3/etc/master.conf
./redis-server /usr/local/jenkins/redis/redis-slave-3.2.3/etc/slave_6380.conf ./redis-server /usr/local/jenkins/redis/redis-slave-3.2.3/etc/slave_6381.conf
主redis打印的日誌code
啓動哨兵server
./redis-sentinel /usr/local/jenkins/redis/redis-slave-3.2.3/etc/sentinel_26379.conf ./redis-sentinel /usr/local/jenkins/redis/redis-slave-3.2.3/etc/sentinel_26380.conf ./redis-sentinel /usr/local/jenkins/redis/redis-slave-3.2.3/etc/sentinel_26381.conf
sentinel_26379.conf配置文件blog
port 26379 dir /usr/local/jenkins/redis/redis-sentinel-3.2.3/tmp26379 daemonize yes logfile "/usr/local/jenkins/redis/redis-sentinel-3.2.3/logs/redis.sentinel26379.log" sentinel monitor mymaster 192.168.1.135 6379 1 sentinel auth-pass mymaster 123456 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000
查看信息:圖片
redis-cli -h 127.0.0.1 -p 6379 -a 123456 info replication redis-cli -h 127.0.0.1 -p 6380 -a 123456 info replication
用redis-cli工具登陸其中一個哨兵
./redis-cli -p 26379
鏈接成功後運行以下命令
sentinel master mymaster
四、測試
登陸主redis
./redis-cli -h 127.0.0.1 -p 6379 -a 123456 set name zhangsan set addr shanghai set curTime 2016-07-11
登陸從reids
./redis-cli -h 127.0.0.1 -p 6380 get name get addr get curTime
五、測試主redis宕機,切換從reids爲主
關閉主redis
./redis-cli -h 127.0.0.1 -p 6379 -a 123456 shutdown
切換從reids爲主reids
./redis-cli -p 6380 -a 123456 slaveof NO ONE
測試從redis是否切換從主redis是否成功
./redis-cli -h 127.0.0.1 -p 6380 -a 123456 set name zhangsan
更多技術文章: