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

  

参考:

https://redis.io/topics/replication

最新文章

  1. C#设计模式之建造者模式
  2. CheckStyle, 强制你遵循编码规范
  3. f2fs解析(八)node 管理器中的node_info
  4. phpcms 网站迁移服务器
  5. fastjson格式化bean的简易属性过滤器
  6. HDU 1087 Super Jumping! Jumping! Jumping! (DP)
  7. Android 使用弹出对话框,报Unable to add window错误
  8. React java.lang.UnsatisfiedLinkError: dlopen failed: "/data/data/com.edaixi.activity/lib-main/libgnustl_shared.so" is 32-bit instead of 64-bit
  9. Xcode之Alcatraz
  10. python自动化测试应用-番外篇--接口测试1
  11. C语言颜色转换宏
  12. cdh启动datanode报错
  13. Mvc校验用户没有登录就跳转的实现
  14. windows Docker Desktop 搭建mysql,mssql和redis服务
  15. Ubuntu18.04和OpenWrt 18.06.0 下使用aria2和BaiduExport处理百度盘下载
  16. java多线程----Semaphore信号量
  17. P3456 [POI2007]GRZ-Ridges and Valleys(bfs)
  18. String类的substring方法
  19. R小问题
  20. js基本知识2

热门文章

  1. 逆向破解之160个CrackMe —— 019
  2. driver.implicitly_wait()与time.sleep()的区别
  3. Dart编译技术与平台
  4. linux学习10 Linux目录结构和根文件系统全面讲解
  5. blockstack与Ethereum
  6. cf1175 D\E
  7. CTSC 2017 游戏[概率dp 线段树]
  8. luogu P2353 背单词
  9. JavaScript map reduce
  10. 关于wineQQ8.9.19983deepin23版本提升不能在使用请升级,Linux偷懒升级方法