Redist的主从配置
Redis的主从复制一般用来在线备份和读写分离,提高服务器的负载能力。主数据库主要进行写操作,而从数据库负责读操作。
主从的配置:
主节点:
[root@master ~]# grep -v -E "^#|^$" /etc/redis/6379.conf bind 0.0.0.0 //绑定的主机地址。说明只能通过这个ip地址连接本机的redis。最好绑定0.0.0.0 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes //这个修改为yes,守护进程 supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile /var/log/redis_6379.log databases 16 always-show-logo yes save 900 1 #启用RDB快照功能,默认就是启用的 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /var/lib/redis/6379 #redis数据目录 replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly yes #启用AOF持久化方式 appendfilename "appendonly.aof" #AOF文件的名称,默认为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 yes 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 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
从节点的配置,只需加一行replicaof 192.168.0.10 6379
[root@node2 redis]# grep -v -E "^#|^$" /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_6379.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 /var/lib/redis/6379 replicaof 192.168.0.10 6379 #主节点redis的IP和端口 replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly yes 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 yes 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 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
启动主,从节点数据就开始同步了
=============================================配置密码======================================
主节点;
requirepass pass1234 # 注意,这个是连接master节点,同步数据用的密码. slave节点需要设置这两个密码
从节点:
requirepass pass1234 这个是slave节点上登录自己的redis用的密码 masterauth pass1234 # 注意,这个是连接master节点,同步数据用的密码. slave节点需要设置这两个密码
使用role和info 查看主从信息
127.0.0.1:6379> role 1) "master" #主节点 2) (integer) 4740 #偏移量 3) 1) 1) "192.168.0.12" #从节点 2) "6379" 3) "4740" 127.0.0.1:6379> INFO replication # Replication role:master connected_slaves:1 slave0:ip=192.168.0.12,port=6379,state=online,offset=5300,lag=1 master_replid:3f8d3c3ab80297ef3916615f8c3b2c22733814cf master_replid2:0000000000000000000000000000000000000000 master_repl_offset:5300 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:530
参考: