Redis主从配置及主从切换
Posted minseo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis主从配置及主从切换相关的知识,希望对你有一定的参考价值。
环境及部署图
本次配置在同一台主机配置文件及端口号不一样
6379配置文件/etc/redis/6379.conf
bind 0.0.0.0 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "/var/log/redis.log" databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ 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 requirepass 123456 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no slave-lazy-flush no 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-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" 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
6380配置文件/etc/redis/6380.conf
bind 0.0.0.0 protected-mode yes port 6380 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "/var/log/redis.log" databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ slaveof 10.1.43.212 6379 masterauth 123456 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 requirepass 123456 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no slave-lazy-flush no 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-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" 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
说明:配置文件大部分相同不同的是端口号以及备配置文件增加以下两行
slaveof 10.1.43.212 6379 masterauth 123456
PS:如果主redis没有配置密码则不需要配置masterauth
启动两个redis测试同步
redis-server /etc/redis/6379.conf redis-server /etc/redis/6380.conf
连接6379插入数据
redis-cli -p 6379 -a 123456
127.0.0.1:6379> set name minseo OK 127.0.0.1:6379> get name "minseo"
连接6380查看同步的数据
redis-cli -p 6380 -a 123456
127.0.0.1:6380> get name "minseo"
默认是读写分离的主库可写从库只能读取
127.0.0.1:6380> set name 123 (error) READONLY You can\'t write against a read only slave.
主从切换
停止主库
src/redis-cli -n 6379 shutdown
将从库设置成主库
redis-cli -p 6380 -a123456 slaveof NO ONE
主库恢复正常了
从库保存数据
127.0.0.1:6380> save OK
把根目录下面的数据文件dump.rdb拷贝至主库对应的目录下面启动原来的主库
从库恢复成从库
redis-cli -p 6380 slaveof 10.1.43.212 6379
以上是关于Redis主从配置及主从切换的主要内容,如果未能解决你的问题,请参考以下文章