Redis的主從複製通常用來在線備份和讀寫分離,提升服務器的負載能力。主數據庫主要進行寫操做,而從數據庫負責讀操做。node
主從的配置: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 bash
[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
啓動主,從節點數據就開始同步了服務器
=============================================配置密碼======================================app
主節點;less
requirepass pass1234 # 注意,這個是鏈接master節點,同步數據用的密碼. slave節點須要設置這兩個密碼
從節點:tcp
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
參考:ui