一、主从复制原理
彻底搞懂Redis主从复制原理及实战 - cooffeeli - 博客园
二、资源分配
系统版本 | IP地址 | 应用 | |
CentOS Linux release 7.9.2009 (Core) | 192.168.2.152 | redis 6.26 | master |
Anolis OS release 8.8 | 192.168.2.34 | redis 6.26 | slave |
三、master配置
# egrep -v "^#|^$|^ *#" redis.conf
bind 0.0.0.0 #可写多个地址bind 192.168.1.100 10.0.0.1
protected-mode no # 保护模式是否开启。开启需要配置bind地址,适用于生产
port 6379 # 端口
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes # 是否要用守护线程的方式启动
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "6379.log" #日志文件名
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass kXXXXXX223 #设置redis认证密码,也用于主从同步认证密码
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no #redis数据是否需要持久化,默认关闭
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
jemalloc-bg-thread yes
四、slave配置
# egrep -v "^$|^#|^ #" redis.conf
bind 0.0.0.0 ##
protected-mode no ##
port 6379 ##
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes ##
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "6379.log" ##
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replicaof 192.168.2.152 6379 #redis master的地址、端口
requirepass "kXXXXXX223" # 设置redis认证密码
masterauth "kXXXXXX223" #同步认证使用密码,和认证密码一致。也可以新建账户。
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
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 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
jemalloc-bg-thread yes
五、启动
# /usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
六、日志
master;
# cat 6379.log
481704:C 15 Jan 2025 10:16:53.197 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
481704:C 15 Jan 2025 10:16:53.197 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=481704, just started
481704:C 15 Jan 2025 10:16:53.197 # Configuration loaded
481704:M 15 Jan 2025 10:16:53.198 * monotonic clock: POSIX clock_gettime
481704:M 15 Jan 2025 10:16:53.199 * Running mode=standalone, port=6379.
481704:M 15 Jan 2025 10:16:53.199 # Server initialized
481704:M 15 Jan 2025 10:16:53.200 * Loading RDB produced by version 6.2.6
481704:M 15 Jan 2025 10:16:53.200 * RDB age 4 seconds
481704:M 15 Jan 2025 10:16:53.200 * RDB memory usage when created 2.77 Mb
481704:M 15 Jan 2025 10:16:53.200 # Done loading RDB, keys loaded: 1, keys expired: 0.
481704:M 15 Jan 2025 10:16:53.200 * DB loaded from disk: 0.000 seconds
481704:M 15 Jan 2025 10:16:53.200 * Ready to accept connections
481704:M 15 Jan 2025 10:18:24.947 * Replica 192.168.2.34:6379 asks for synchronization
481704:M 15 Jan 2025 10:18:24.947 * Full resync requested by replica 192.168.2.34:6379
481704:M 15 Jan 2025 10:18:24.948 * Replication backlog created, my new replication IDs are 'cffca5478188723e5d026b11868a3392aa29a886' and '0000000000000000000000000000000000000000'
481704:M 15 Jan 2025 10:18:24.948 * Starting BGSAVE for SYNC with target: disk
481704:M 15 Jan 2025 10:18:24.948 * Background saving started by pid 484151
484151:C 15 Jan 2025 10:18:24.984 * DB saved on disk
484151:C 15 Jan 2025 10:18:24.984 * RDB: 0 MB of memory used by copy-on-write
481704:M 15 Jan 2025 10:18:25.063 * Background saving terminated with success
481704:M 15 Jan 2025 10:18:25.063 * Synchronization with replica 192.168.2.34:6379 succeeded
481704:M 15 Jan 2025 10:34:22.972 # Connection with replica 192.168.2.34:6379 lost.
481704:M 15 Jan 2025 10:35:06.708 * Replica 192.168.2.34:6379 asks for synchronization
481704:M 15 Jan 2025 10:35:06.708 * Partial resynchronization request from 192.168.2.34:6379 accepted. Sending 0 bytes of backlog starting from offset 1331.
slave:
# cat 6379.log
3200098:C 15 Jan 2025 10:35:06.712 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3200098:C 15 Jan 2025 10:35:06.712 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=3200098, just started
3200098:C 15 Jan 2025 10:35:06.712 # Configuration loaded
3200098:S 15 Jan 2025 10:35:06.712 * monotonic clock: POSIX clock_gettime
3200098:S 15 Jan 2025 10:35:06.713 * Running mode=standalone, port=6379.
3200098:S 15 Jan 2025 10:35:06.713 # Server initialized
3200098:S 15 Jan 2025 10:35:06.714 * Loading RDB produced by version 6.2.6
3200098:S 15 Jan 2025 10:35:06.714 * RDB age 44 seconds
3200098:S 15 Jan 2025 10:35:06.714 * RDB memory usage when created 1.79 Mb
3200098:S 15 Jan 2025 10:35:06.714 # Done loading RDB, keys loaded: 1, keys expired: 0.
3200098:S 15 Jan 2025 10:35:06.714 * DB loaded from disk: 0.000 seconds
3200098:S 15 Jan 2025 10:35:06.714 * Before turning into a replica, using my own master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
3200098:S 15 Jan 2025 10:35:06.714 * Ready to accept connections
3200098:S 15 Jan 2025 10:35:06.714 * Connecting to MASTER 192.168.2.152:6379
3200098:S 15 Jan 2025 10:35:06.714 * MASTER <-> REPLICA sync started
3200098:S 15 Jan 2025 10:35:06.714 * Non blocking connect for SYNC fired the event.
3200098:S 15 Jan 2025 10:35:06.715 * Master replied to PING, replication can continue...
3200098:S 15 Jan 2025 10:35:06.715 * Trying a partial resynchronization (request cffca5478188723e5d026b11868a3392aa29a886:1331).
3200098:S 15 Jan 2025 10:35:06.715 * Successful partial resynchronization with master.
3200098:S 15 Jan 2025 10:35:06.716 * MASTER <-> REPLICA sync: Master accepted a Partial Resynchronization.
七、客户端工具查看
八、致谢
诚谢: 佛小富 中彻底搞懂Redis主从复制原理及实战中的主从复制原理部分阐述
如有侵权,请及时联系删除。