redis二进制部署 https://www.jianshu.com/p/f41862328ce4 redis安全认证 : https://www.jianshu.com/p/115beb211062 redis哨兵 https://www.jianshu.com/p/6133377b74ec https://www.jianshu.com/p/20dcf4534c12
在从库主机操作
db02:6379> SLAVEOF 172.16.210.53 6379 ##连接主库建立主从关系 OK db02:6379> keys * ##查看数据 -- --- 998) "k_283" 999) "k_247" 1000) "k_590" 1001) "k_903" 1002) "k_559" 1003) "k_711" ##数据成功同步到从库从库发起同步请求 主库收到请求后执行bgsave保存当前内存里的数据到磁盘 主库将持久化的数据发送给从库的数据目录 从库收到主库的持久化数据之后,先清空自己当前内存中的所有数据 从库将主库发送过来的持久化文件加到自己的内存里
1.执行主从复制之前,先将数据备份一份 2.建议将主从复制写入到配置文件中 3,在业务低峰期做主从复制. 3.拷贝数据时会占用宽带 4.不能自动完成主从切换,需要人工介入
1.1 Redis单机的问题
1.1.1 机器故障
在一台服务器上部署一个Redis节点,如果机器发生主板损坏,硬盘损坏等问题,不能在短时间修复完成,就不能处理Redis操作了,这就是单机可能存在的问题
同样的,服务器正常运行,但是Redis主进程发生宕机事件,此时只需要重启Redis就可以了。如果不考虑在Redis重启期间的性能损失,可以考虑Redis的单机部署
Redis单机部署出现故障时,把Redis迁移到另一台服务器上,此时需要把发生故障的Redis中的数据同步到新部署的Redis节点,这也需要很高的成本
1.1.2 容量瓶颈
一台服务器有16G内存,此时分配12G内存运行Redis
如果有新需求:Redis需要占用32G或者64G等更多的内存,此时这台服务器就不能满足需求了,此时可以考虑更换一台更大内存的服务器,也可以用多台服务器组成一个Redis集群来满足这个需求
1.1.3 QPS瓶颈
根据Redis官方的说法,单台Redis可以支持10万的QPS,如果现在的业务需要100万的QPS,此时可以考虑使用Redis分布式
2.1 一主一从模型
一个Redis节点为master节点(主节点),负责对外提供服务。
另一个节点为slave节点(从节点),负责同步主节点的数据,以达到备份的效果。当主节点发生宕机等故障时,从节点也可以对外提供服务
如下图所示 2.2 一主多从模型
一个Redis节点为master节点(主节点),负责对外提供服务。
多个节点为slave节点(从节点)。每个slave都会对主节点中的数据进行备份,以达到更加高可用的效果。这种情况下就算master和一个slave同时发生宕机故障,其余的slave仍然可以对外读提供服务,并保证数据不会丢失
当master有很多读写,达到Redis的极限阀值,可以使用多个slave节点对Redis的读操作进行分流,有效实现流量的分流和负载均衡,所以一主多从也可以做读写分离 2.3 读写分离模型 master节点负责写数据,同时客户端可以从slave节点读取数据
对数据提供了多个备份,这些备份数据可以大大提高Redis的读性能,是Redis高可用或者分布式的基础
4.1 slaveof命令 取消复制 4.2 配置文件配置 修改Redis配置文件/etc/redis.conf
slaveof <masterip> <masterport> # masterip为主节点IP地址,masterport为主节点端口 slave - read - only yes # 从节点只做读操作,不做写操作,保证主从设备数据相同4.3 两种主从配置方式比较
使用命令行配置无需重启 Redis ,可以实现统一配置 使用配置文件方式配置不变于管理,而且需要重启 Redis4.4 例子
有两台虚拟机,操作系统都是CentOS 7.5
一台虚拟机的IP地址为192.168.81.100,做master 一台虚拟机的IP地址为192.168.81.101,做slave第一步:在192.168.81.101虚拟机操作
[root@mysql ~]# vi /etc/redis.conf # 修改Redis配置文件 bind 0.0.0.0 # 可以从外部连接Redis服务端 slaveof 192.168.81.100 6379 # 设置master的IP地址和端口然后保存修改,启动Redis
[root@mysql ~]# systemctl stop firewalld # 关闭firewalld防火墙 [root@mysql ~]# systemctl start redis # 启动slave上的Redis服务端 [root@mysql ~]# ps aux | grep redis-server # 查看redis-server的进程 redis 2319 0.3 0.8 155204 18104 ? Ssl 09:55 0:00/usr/bin/redis-server 0.0.0.0:6379 root 2335 0.0 0.0112664 968 pts/ [root@mysql ~]# redis-cli # 启动Redis客户端 127.0.0.1:6379> info replication # 查看Redis的复制信息 查看192.168.81.101机器上的Redis的info # Replication role:slave # 角色为slave master_host:192.168.81.100 # 主节点IP为192.168.81.100 master_port:6379 # 主节点端口为6379 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_repl_offset:155 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0第二步:在192.168.81.100虚拟机上操作
[root@localhost ~]# systemctl stop firewalld # 关闭firewalld防火墙 [root@localhost ~]# vi /etc/redis.conf # 修改Redis配置文件 bind 0.0.0.0 然后保存修改,启动Redis [root@localhost ~]# systemctl start redis # 启动master上的Redis [root@localhost ~]# ps aux | grep redis-server # 查看redis-server进程 redis 2529 0.2 1.8 155192 18192 ? Ssl 17:55 0:00 /usr/bin/redis-server 0.0.0.0:6379 root 2536 0.0 0.0 112648 960 pts/2 R+ 17:56 0:00 grep --color=auto redis [root@localhost ~]# redis-cli # 启动master上的redis-cli客户端 127.0.0.1:6379> info replication # 查看192.168.81.100机器上Redis的信息 # Replication role:master # 角色为主节点 connected_slaves:1 # 连接一个从节点 slave0:ip=192.168.81.101,port=6379,state=online,offset=141,lag=2 # 从节点的信息 master_repl_offset:141 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:140 127.0.0.1:6379> set hello world # 向主节点写入数据 OK 127.0.0.1:6379> info server # Server redis_version:3.2.10 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:c8b45a0ec7dc67c6 redis_mode:standalone os:Linux 3.10.0-514.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll gcc_version:4.8.5 process_id:2529 run_id:7091f874c7c3eeadae873d3e6704e67637d8772b # 注意这个run_id tcp_port:6379 uptime_in_seconds:488 uptime_in_days:0 hz:10 lru_clock:12784741 executable:/usr/bin/redis-server config_file:/etc/redis.conf第三步:回到192.168.81.101这台从节点上操作
127.0.0.1:6379> get hello # 获取'hello'的值,可以获取到 "world" 127.0.0.1:6379> set a b # 向192.168.81.101从节点写入数据,失败 (error) READONLY You can't write against a read only slave. 127.0.0.1:6379> slaveof no one # 取消从节点设置 OK 127.0.0.1:6379> info replication # 查看192.168.81.101机器,已经不再是从节点,而变成主节点了 # Replication role:master # 变成主节点了 connected_slaves:0 master_repl_offset:787 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6379> dbsize # 查看192.168.81.101上Redis所有数据大小 (integer) 2第四步:回到192.168.81.100虚拟机
127.0.0.1:6379> mset a b c d e f # 向192.168.81.100上的Redis集合中写入数据 OK 127.0.0.1:6379> dbsize # Redis中数据大小为5 (integer) 5第五步:查看192.168.81.100虚拟机上Redis的日志
[root@localhost ~]# tail /var/log/redis/redis.log # 查看Redis最后10行日志 2529:M 14 Oct 17:55:09.448 * DB loaded from disk: 0.026 seconds 2529:M 14 Oct 17:55:09.448 * The server is now ready to accept connections on port 6379 2529:M 14 Oct 17:55:10.118 * Slave 192.168.81.101:6379 asks for synchronization 2529:M 14 Oct 17:55:10.118 * Partial resynchronization not accepted: Runid mismatch (Client asked for runid '9f93f85bce758b9c48e72d96a182a2966940cf52', my runid is '7091f874c7c3eeadae873d3e6704e67637d8772b') # 与192.168.81.100设备上通过info命令查看到的run_id相同 2529:M 14 Oct 17:55:10.118 * Starting BGSAVE for SYNC with target: disk # 执行BGSAVE命令成功 2529:M 14 Oct 17:55:10.119 * Background saving started by pid 2532 2532:C 14 Oct 17:55:10.158 * DB saved on disk 2532:C 14 Oct 17:55:10.159 * RDB: 12 MB of memory used by copy-on-write 2529:M 14 Oct 17:55:10.254 * Background saving terminated with success 2529:M 14 Oct 17:55:10.256 * Synchronization with slave 192.168.81.101:6379 succeeded # 向192.168.81.101同步数据成功第六步:回到192.168.81.101虚拟机
127.0.0.1:6379> slaveof 192.168.81.100 6379 # 把192.168.81.101重新设置为192.168.81.100的从节点 OK 127.0.0.1:6379> dbsize (integer) 5 127.0.0.1:6379> mget a 1) "b"第七步:查看192.168.81.101虚拟机上Redis的日志
[root@mysql ~]# tail /var/log/redis/redis.log # 查看Redis最后10行日志 2319:S 14 Oct 09:55:17.625 * MASTER <-> SLAVE sync started 2319:S 14 Oct 09:55:17.625 * Non blocking connect for SYNC fired the event. 2319:S 14 Oct 09:55:17.626 * Master replied to PING, replication can continue... 2319:S 14 Oct 09:55:17.626 * Trying a partial resynchronization (request 9f93f85bce758b9c48e72d96a182a2966940cf52:16). 2319:S 14 Oct 09:55:17.628 * Full resync from master: 7091f874c7c3eeadae873d3e6704e67637d8772b:1 # 从master节点全量复制数据 2319:S 14 Oct 09:55:17.629 * Discarding previously cached master state. 2319:S 14 Oct 09:55:17.763 * MASTER <-> SLAVE sync: receiving 366035 bytes from master # 显示从master同步的数据大小 2319:S 14 Oct 09:55:17.765 * MASTER <-> SLAVE sync: Flushing old data # slave清空原来的数据 2319:S 14 Oct 09:55:17.779 * MASTER <-> SLAVE sync: Loading DB in memory # 加载同步过来的RDB文件 2319:S 14 Oct 09:55:17.804 * MASTER <-> SLAVE sync: Finished with success参考链接 :
Redis高可用之主从复制 :https://mp.weixin.qq.com/s/9WN3AT_4luRCr503u7U6gw