1、至少两台服务器分别部署好单机的redis
192.168.174.5 (主master)主
192.168.174.2 (salve)从
2、修改redis配置文件(标记的地方都是需要取配置的其余可以默认)
192.168.174.5(主)修改redsi.conf配置文件
####### NETWORK ####### bind 0.0.0.0 protected-mode no port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 ####### GENERAL ####### daemonize yes supervised no pidfile "/var/run/redis_6379.pid" loglevel notice databases 16 always-show-logo yes ####### SNAPSHOTTING ####### #save 900 1 #save 300 10 #save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/usr/local/redis-5.0.5/data" ####### REPLICATION ####### masterauth "root123" slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 ####### SECURITY ####### requirepass "root123" ####### LAZY FREEING ####### lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no slave-lazy-flush no ####### APPEND ONLY MODE ####### appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble no ####### LUA SCRIPTING ####### lua-time-limit 5000 ####### SLOW LOG ####### slowlog-log-slower-than 10000 slowlog-max-len 128 ####### LATENCY MONITOR ####### latency-monitor-threshold 0 ####### EVENT NOTIFICATION ####### notify-keyspace-events "" ####### ADVANCED CONFIG ####### hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes
192.168.174.2(从)修改redis.conf配置文件
#配置除了与主一样外,还需要添加下面的行slaveof <masterip> <masterport> slaveof 192.168.174.5 6379
3、启动redis服务
主从启动方法一样,先起主
[root@t-enter redis]# redis-server redis.conf 10906:C 24 Sep 2020 09:14:38.554 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 10906:C 24 Sep 2020 09:14:38.554 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=10906, just started 10906:C 24 Sep 2020 09:14:38.554 # Configuration loaded
4、修改Sentinel配置文件(标记的地方都是需要去配置的)
bind 0.0.0.0 protected-mode no port 26379 dir "/tmp" #logfile "/home/autouser/redis/redis-4.0.10/log/redis-sentinel.log" #sentinel monitor 主库名 主库IP 端口 从库的个数
sentinel monitor mymaster 10.5.5.194 6379 2 #yes 表示后台启动
daemonize yes
#配置密码 sentinel auth-pass mymaster root123 sentinel config-epoch mymaster 1 sentinel leader-epoch mymaster 1 sentinel current-epoch 1
5、启动Sentinel服务
[root@t-enter redis]# redis-sentinel sentinel.conf 10986:X 24 Sep 2020 09:48:44.924 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 10986:X 24 Sep 2020 09:48:44.924 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=10986, just started 10986:X 24 Sep 2020 09:48:44.924 # Configuration loaded
6、停止redid和sentinel方法
[root@t-enter redis]# cd src/
#停止redis [root@t-enter src]# ./redis-cli -h 192.168.174.5 -p 6379 -a root123 shutdown Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
#停止sentinel
[root@t-enter src]# ./redis-cli -h 192.168.174.5 -p 26379 -a root123 shutdown
7、验证
停止redis服务
[root@t-enter src]# ./redis-cli -h 192.168.174.5 -p 6379 -a root123 shutdown
检查redis进程
[root@t-enter src]# ps -ef|grep redis root 10987 1 1 09:48 ? 00:00:11 redis-sentinel 0.0.0.0:26379 [sentinel] root 11056 10596 0 10:06 pts/1 00:00:00 grep redis
登录从库192.168.174.2,info replication检查redis状态,角色由slave改为master,slave连接数为0。
127.0.0.1:6379> info replication # Replication role:master connected_slaves:0 master_replid:3aa04d4aa6c029d125dce2410b580a1afc8a3914 master_replid2:b795b92ad217acc709045524b0e783fe4aa6d513 master_repl_offset:148517 second_repl_offset:144547 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:148517
启动主库192.168.174.5
[root@t-enter redis]# redis-server 6379.conf 11059:C 24 Sep 2020 10:07:45.089 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 11059:C 24 Sep 2020 10:07:45.089 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=11059, just started 11059:C 24 Sep 2020 10:07:45.089 # Configuration loaded
检查主从redis状态
192.168.174.5主库重启后状态为slave,主库IP为192.168.174.2,从库状态为master
# 192.168.174.5
127.0.0.1:6379> info replication # Replication role:slave master_host:192.168.174.2 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:3843707 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:3aa04d4aa6c029d125dce2410b580a1afc8a3914 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:3843707 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2795132 repl_backlog_histlen:1048576 127.0.0.1:6379>
192.168.174.5主库重启后,192.168.174.2状态为master,slave链接数为1,在192.168.174.5重启前连接数为0。
127.0.0.1:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=192.168.174.5,port=6379,state=online,offset=3839576,lag=0 master_replid:3aa04d4aa6c029d125dce2410b580a1afc8a3914 master_replid2:b795b92ad217acc709045524b0e783fe4aa6d513 master_repl_offset:3839576 second_repl_offset:144547 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2791001 repl_backlog_histlen:1048576
验证成功。
grep -Ev ^# /usr/local/redis/etc/sentinel.bak | grep -v ^$ > sentinel.conf //将注释和空白行删除
原文:https://www.cnblogs.com/quanyao/p/13721658.html