【十七】redis之主从复制概述及部署
概述
主从复制主要解决单机故障的问题。
master同步自己数据到slave上。
数据流向是单向的,master到slave。
可以一个master有多个slave。
一个slave只能有一个master。
可以读写分离,master写,而读可以分流到各个slave上。
不会故障自动转移。
主要作用:
1.为数据提供多个副本
2.扩展了redis读的性能,可以做读写分离。
实现主从复制的两种方式
1.slave of 命令 无需重启
如果希望B成为A的从节点,在B机器上执行slaveof a的ip a的port
如果从节点不想再做从节点,则在从节点上执行 slaveof no one 这样并不会清楚从节点已经同步的数据
2.修改配置文件 需要重启
这里两台机器node1是master,node2是slave。两台机器都要安装redis
先根据单机介绍安装好node1,然后拷贝到node2上
cd /app
scp -r redis aaa@qq.com:/app
【一】linux安装redis(单机版)、3种启动方式、及配置文件介绍。
修改node2的配置文件
cd /app/redis/config/
vi redis-6379.conf
protected-mode no
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
logfile "6379.log"
dir /app/redis/data
slave-read-only yes
slaveof node1 6379
slaveof node1 6379 配置它是谁的从节点
slave-read-only yes 从节点只做读的操作
启动master
启动slave
cd /app/redis/src
./redis-server /app/redis/config/redis-6379.conf
分别在node1和node2的命令行输入info replication
测试数据是否同步到从节点
在node1的redis中 set test master
在node2的redis中 get test
runid
redis每次启动都有一个runid。
slave知道master的runid,如果slave察觉到master的runid发生了变化,slave会从master中全量同步数据,因为master可能重启可能做了很大的变化。
输入info可以查看runid
偏移量
全量复制
psync runid offset 第一个参数是runid,第二个参数是偏移量。
第一次复制不知到runid和偏移量用 psync ? -1
部分复制
slave的偏移量在master的repl_back_buffer内,就部分复制
报错
使用主从复制的时候有一个slave一直在同步master的时候连接超时,master上info查不到这个slave。下面的slave的日志
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
5517:S 07 Aug 18:41:23.427 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
5517:S 07 Aug 18:41:23.428 * DB loaded from disk: 0.000 seconds
5517:S 07 Aug 18:41:23.428 * The server is now ready to accept connections on port 6379
5517:S 07 Aug 18:41:23.428 * Connecting to MASTER node1:6379
5517:S 07 Aug 18:41:23.429 * MASTER <-> SLAVE sync started
5517:S 07 Aug 18:42:24.263 # Timeout connecting to the MASTER...
5517:S 07 Aug 18:42:24.264 * Connecting to MASTER node1:6379
5517:S 07 Aug 18:42:24.265 * MASTER <-> SLAVE sync started
5517:S 07 Aug 18:43:25.249 # Timeout connecting to the MASTER...
5517:S 07 Aug 18:43:25.250 * Connecting to MASTER node1:6379
5517:S 07 Aug 18:43:25.251 * MASTER <-> SLAVE sync started
5517:S 07 Aug 18:44:26.082 # Timeout connecting to the MASTER...
5517:S 07 Aug 18:44:26.083 * Connecting to MASTER node1:6379
5517:S 07 Aug 18:44:26.084 * MASTER <-> SLAVE sync started
5517:S 07 Aug 18:45:27.933 # Timeout connecting to the MASTER...
5517:S 07 Aug 18:45:27.933 * Connecting to MASTER node1:6379
5517:S 07 Aug 18:45:27.934 * MASTER <-> SLAVE sync started
5517:S 07 Aug 18:46:28.771 # Timeout connecting to the MASTER...
5517:S 07 Aug 18:46:28.771 * Connecting to MASTER node1:6379
5517:S 07 Aug 18:46:28.772 * MASTER <-> SLAVE sync started
5517:S 07 Aug 18:47:29.616 # Timeout connecting to the MASTER...
5517:S 07 Aug 18:47:29.617 * Connecting to MASTER node1:6379
5517:S 07 Aug 18:47:29.618 * MASTER <-> SLAVE sync started
推荐阅读