Redis主从同步、哨兵模式、集群模式配置

摘要:
2、 Redis主从同步2.1。配置信息Redis主从同步重要参数(Redis.conf)#slavof&lt。
Redis主从同步、哨兵模式、集群模式配置

目录

一、概述

redis主从:是备份关系, 操作主库,数据也会同步到从库。 如果主库机器坏了,从库可以上。
redis哨兵:哨兵保证的是HA,保证特殊情况故障自动切换,哨兵监控“redis主从集群”,如果主库down,会自动找新的master。实际使用sentinel哨兵模式时,可结合Keepalived使用,保证down的redis可以自动重启,提高高可用性。
redis集群:集群保证的是高并发,同时集群会导致数据的分散,整个redis集群会分成一堆数据槽,即不同的key会放到不不同的槽中。

主从保证了数据备份,哨兵保证了HA 即故障时切换,集群保证了高并发性。

二、Redis主从同步

2.1、配置信息

redis主从同步重要参数(redis.conf)

# slaveof <masterip> <masterport>
# masterauth <master-password>
# requirepass foobared

Master关键配置信息redis.conf

masterauth 123456
requirepass 123456

Slave关键配置信息(redis.conf)

slaveof 192.168.1.100 6379
masterauth 123456
requirepass 123456

redis.conf其他常用配置

bind 0.0.0.0
protected-mode no #关闭保护,允许非本地连接
port 6379 #端口号
daemonize yes #后台运行
pidfile /var/run/redis_6379.pid #进程守护文件
logfile /var/log/redis_6379.log #日志文件
dir /var/lib/redis/6379 #db等相关目录位置
appendonly no #AOF模式持久化,默认未打开

2.2、检查状态

[root@localhost redis]# /usr/local/bin/redis-cli -h 192.168.1.100 -p 6379
192.168.1.100:6379> auth 123456
OK
192.168.1.100:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.101,port=6379,state=online,offset=560,lag=0
slave1:ip=192.168.1.102,port=6379,state=online,offset=560,lag=0
master_replid:658f07a291f7e58100e1650bd920f8bcef3e3083
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:560
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:560

2.3、验证主从

#验证Master 写和读
[root@localhost redis]# /usr/local/bin/redis-cli -h 192.168.1.100 -p 6379
192.168.1.100:6379> auth 123456
192.168.1.100:6379> set projectCode 1001
OK
192.168.1.100:6379> get projectCode
"1001"

#验证Slave 写和读
[root@localhost redis]# /usr/local/bin/redis-cli -h 192.168.1.101 -p 6379
192.168.1.101:6379> auth 123456
OK
192.168.1.101:6379> get projectCode
"1001"
192.168.1.101:6379> set projectCode 1002
(error) READONLY You can't write against a read only slave.

三、哨兵模式

3.1、配置信息

redis哨兵模式重要参数(sentinel.conf)

# sentinel monitor <master-name> <ip> <redis-port> <quorum>
# sentinel auth-pass <master-name> <password>
# sentinel down-after-milliseconds <master-name> <milliseconds>

关键配置信息sentinel.conf

sentinel monitor mymaster 192.168.1.100 6379 2
sentinel auth-pass mymaster 123456

sentinel.conf其他配置

bind 0.0.0.0
protected-mode no
port 26379
daemonize yes
# sentinel down-after-milliseconds mymaster 30000

启动sentinel

/usr/local/bin/redis-sentinel ./sentinel.conf
或者
/usr/local/bin/redis-server ./sentinel.conf --sentinel

3.2、检查状态

[root@localhost redis]# /usr/local/bin/redis-cli -h 192.168.1.100 -p 26379
192.168.1.100:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.100:6379,slaves=2,sentinels=3

3.3、验证故障转移

#停止master实例
[root@localhost redis]# /usr/local/bin/redis-cli -h 192.168.1.100 -p 6379 -a 123456 shutdown

#再次检查sentinel状态
[root@localhost redis]# /usr/local/bin/redis-cli -h 192.168.1.100 -p 26379
192.168.1.100:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.102:6379,slaves=2,sentinels=3
192.168.1.100:26379> 11886:X 19 Apr 18:00:21.912 # +sdown slave 192.168.1.100:6379 192.168.1.100 6379 @ mymaster 192.168.1.102 6379



# 其他检查命令
192.168.1.100:26379> sentinel get-master-addr-by-name mymaster
1) "192.168.1.102"
2) "6379"

192.168.1.100:26379> sentinel slaves mymaster
192.168.1.100:26379> sentinel masters

四、集群模式

梳理Redis集群模式时,发现2018年初工作上有整理过,躺在草稿箱了,干脆直接发布,这里就不在整理,详见:

Redis维护笔记 https://www.cnblogs.com/huligong1234/p/8457102.html

五、相关参考

免责声明:文章转载自《Redis主从同步、哨兵模式、集群模式配置》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SQL SERVER 关于多层嵌套的方法10种顶级javascript框架比较The Top 10 Javascript MVC Frameworks下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WebView2简单试用(四)—— 使用固定版本的Edge Runtime

Edge Runtime支持两种模式:长绿(All Green)和固定版本(Fixed Version)这两种方式和vc runtime安装比较类似。 长绿版本可以作为独立的安装包安装,所有程序共享。固定版本则也可以直接在程序中包含。 长绿版本的优势: 可以自动更新 支持在线和离线安装 所有WebView2程序共享运行时,节约磁盘空间     固定版本...

Git分支管理

Git默认分支是在master 假如需要开发新的功能又不影响主干master则需要开启新的分支 创建新分支 git branch about 切换到about分支 git checkout about 查看 开发一个about模块 vim about.html git add about.html git commit -m "add a...

Spring整合Redis

Oracle:  存储数据,   使用SQL操作数据库   Java 程序作为客户端, 发送sql, 操作数据库:  jdbc Redis: key/value 内存式数据库,  存储数据,   redis: 存储热点数据   Java程序作为客户端,  jedis 使用Spring-data-redis+jedis操作redis   与Spring整合,...

MySQL主从复制什么原因会造成不一致,如何预防及解决?

一、导致主从不一致的原因主要有: 人为原因导致从库与主库数据不一致(从库写入) 主从复制过程中,主库异常宕机 设置了ignore/do/rewrite等replication等规则 binlog非row格式 异步复制本身不保证,半同步存在提交读的问题,增强半同步起来比较完美。 但对于异常重启(Replication Crash Safe),从库写数据(G...

基于Springboot集成security、oauth2实现认证鉴权、资源管理

   1、Oauth2简介   OAuth(开放授权)是一个开放标准,允许用户授权第三方移动应用访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方移动应用或分享他们数据的所有内容,OAuth2.0是OAuth协议的延续版本,但不向后兼容OAuth 1.0即完全废止了OAuth1.0。 2、Oauth2服务器 授权服务器 Autho...

Redis打造URL缩短服务

用Redis打造URL缩短服务 此文章的英文版本已首发于 CodeProject :Building a simple URL shorten service with Redis 阅读文章之前,我建议你先下载源码,一边看文章,一边看代码。 代码在这里下载:http://www.codeproject.com/KB/NoSQL/819235/MicroU...