当前位置:网站首页>Redis主从复制
Redis主从复制
2022-07-04 16:06:00 【礁之】
一、Redis主从复制概述
(1)主从复制概述
- 为了分担Redis的读写压力,Redis支持主从复制,Redis的主从结构可以采用一主多从或者级联结构
- Redis主从复制的同步类型分为全量同步和增量同步
全量同步
Redis的全量同步一般发生在Slave服务器初始化阶段,也就是刚加入集群或者是Slave宕机重启后,这个时候Slave需要将Master上所有的数据进行同步,下面是同步的过程:(同步的命令为写命令)
- **Slave服务器连接Master服务器,发送SYNC(同步)命令,Slave服务器初次向Master服务器进行同步时,是不会影响Master服务器接收客户端的请求的 **
- Master服务器接收到Slave服务器的SYNC命令后,开始执行bgsave命令拍摄数据库快照生成RDB文件并且使用缓冲区记录此后执行的所有命令,也就是说Master会做一个当前数据库的快照,然后会把之后执行的所有命令放到自己的缓存中
- Master服务器上bgsave命令执行完成后,会向所有的Slave服务器发送RDB文件,并且在发送期间继续记录被执行的命令至自己的缓存中
- Slave服务器在接收到Master服务器的RDB文件后,会丢掉自己的所有数据,然后载入RDB文件,其实这个时候的Slave已经是完成全量备份了,但是因为RDB文件之后还有被执行的命令,所以还需向Master服务器同步之后执行的命令
- Master服务器向Slave服务器发送在缓存中的被执行的命令
- Slave服务器完成对RDB文件的载入后,会继续接收Master服务器发送过来的缓存区的命令进行同步
完成了上面的操作就完成了Slave从服务器的初始化操作,从服务器此时就可以接收来自用户的读请求了,之后进行同步Master主服务器的数据都是增量同步
增量同步
Redis的增量同步是发生在Slave从服务器初始化阶段完成之后开始正常工作时,Master主服务器发生的写操作会同步到Slave从服务器的过程
Master主服务器每接收到一个写操作,就会给Slave从服务器发送相同的写操作,然后Slave从服务器进行同步
(2)Redis主从的同步策略
- Redis主从最开始连接的时候,Slave从服务器会进行全量同步
- 在全量同步结束之后,Slave从服务器之后的同步都是增量同步
- 如果有需要,Slave服务器在任何时候都是可以进行全量同步的
- Redis的同步策略是:无论如何,Slave服务器首先会先尝试进行增量同步,如果同步失败,则Slave从服务器进行全量同步
- 建议开启Master主服务器的持久化功能,因为在多台Slave服务器宕机需要重启的情况下,在重启后每台Slave从服务器都会向Master主服务器发送SYNC(同步)的请求,和Master服务器进行全量同步,这样会导致Master主服务器的IO快速增加而导致Master主服务器宕机
二、配置Redis的主从复制
(1)实验环境
系统 | ip | 主机名 | redis版本 | 扮演角色 |
---|---|---|---|---|
Centos7.4 | 192.168.100.202 | master | redis-5.0.4 | master主服务器 |
Centos7.4 | 192.168.100.203 | slave | redis-5.0.4 | slave从服务器 |
(2)实验目的
实现Redis主从复制
(3)实验步骤
配置两台服务器的Redis,安装Redis两台服务器相同,主机名不同、ip不同,下面只写master的配置
[[email protected] ~]# hostnamectl set-hostname master
[[email protected] ~]# su
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# setenforce 0
setenforce: SELinux is disabled
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
mount: /dev/sr0 已经挂载或 /mnt 忙
/dev/sr0 已经挂载到 /mnt 上
[[email protected] ~]# ll
总用量 1928
-rw-------. 1 root root 1264 1月 12 18:27 anaconda-ks.cfg
-rw-r--r-- 1 root root 1966337 6月 9 01:16 redis-5.0.4.tar.gz
[[email protected] ~]# tar xf redis-5.0.4.tar.gz
[[email protected] ~]# cd redis-5.0.4
[[email protected] redis-5.0.4]# make
[[email protected] redis-5.0.4]# mkdir -p /usr/local/redis
[[email protected] redis-5.0.4]# cp /root/redis-5.0.4/src/redis-server /usr/local/redis/
[[email protected] redis-5.0.4]# cp /root/redis-5.0.4/src/redis-cli /usr/local/redis/
[[email protected] redis-5.0.4]# cp /root/redis-5.0.4/redis.conf /usr/local/redis/
[[email protected] redis-5.0.4]# vim /usr/local/redis/redis.conf #修改
。。。。。。
68 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69 bind 192.168.100.202 #修改为本机地址,如果为127.0.0.1就只能本机访问
70
。。。。。。
87 # are explicitly listed using the "bind" directive.
88 protected-mode no #关闭redis的保护模式,如果为yes的话其他客户端就无法连接到此服务器
89
。。。。。。
135 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
136 daemonize yes #开启redis的后台守护程序,即在redis开启之后是放在后台运行的
137
。。。。。。
262 # Note that you must specify a directory here, not a file name.
263 dir /usr/local/redis/rdb
264
。。。。。。
506 #
507 requirepass 123123 #去掉注释,修改redis的密码为123123
508
#保存退出
[[email protected] redis-5.0.4]# mkdir /usr/local/redis/rdb
[[email protected] redis-5.0.4]# vim /etc/init.d/redis
#!/bin/sh
# chkconfig: 2345 80 90
# description: Start and Stop redis
#PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/redis-server
REDIS_CLI=/usr/local/redis/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/usr/local/redis/redis.conf"
AUTH="123123"
LISTEN_IP=$(netstat -utpln |grep redis-server |awk '{print $4}'|awk -F':' '{print $1}')
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -h $LISTEN_IP -p $REDISPORT -a $AUTH SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
[[email protected] redis-5.0.4]# chkconfig --add redis
[[email protected] redis-5.0.4]# chmod 755 /etc/init.d/redis
[[email protected] redis-5.0.4]# ln -s /usr/local/redis/* /usr/local/bin/
[[email protected] redis-5.0.4]# /etc/init.d/redis start
Starting Redis server...
5233:C 09 Jun 2021 01:25:53.069 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5233:C 09 Jun 2021 01:25:53.069 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5233, just started
5233:C 09 Jun 2021 01:25:53.069 # Configuration loaded
Redis is running...
[[email protected] redis-5.0.4]# netstat -anpt | grep 6379
tcp 0 0 192.168.100.202:6379 0.0.0.0:* LISTEN 5234/redis-server 1
配置master服务器主配置文件
[[email protected] redis-5.0.4]# vim /usr/local/redis/redis.conf #修改
。。。。。。
456 #
457 min-replicas-to-write 1 #设置slave服务器的数量,当slave服务器少于这个数量时,Master主服务器会停止接收客户端的一切写请求
458 min-replicas-max-lag 10 #设置主服务器和从服务器之间同步数据的超时时间,当超过此时间时,master主服务器会停止客户端的一切写操作,单位为秒
459 #
。。。。。。
[[email protected] redis-5.0.4]# /etc/init.d/redis restart #重启redis
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped
Starting Redis server...
5291:C 09 Jun 2021 02:04:39.132 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5291:C 09 Jun 2021 02:04:39.132 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5291, just started
5291:C 09 Jun 2021 02:04:39.132 # Configuration loaded
Redis is running...
配置slave服务器主配置文件
[[email protected] redis-5.0.4]# vim /usr/local/redis/redis.conf
。。。。。。
285 #
286 replicaof 192.168.100.202 6379 #在从服务器上指定主服务器的ip和端口
287
。。。。。。
292 #
293 masterauth 123123 #指定主服务器上redis的密码
294
。。。。。。
#保存退出
[[email protected] redis-5.0.4]# /etc/init.d/redis restart #重启服务
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped
Starting Redis server...
5304:C 09 Jun 2021 02:11:32.241 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5304:C 09 Jun 2021 02:11:32.241 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=5304, just started
5304:C 09 Jun 2021 02:11:32.241 # Configuration loaded
Redis is running...
验证主从复制
******(1)登录主服务器的redis,创建key
[[email protected] ~]# redis-cli -h 192.168.100.202 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.202:6379> keys *
(empty list or set)
192.168.100.202:6379> set aaa bbb
OK
192.168.100.202:6379> keys *
1) "aaa"
192.168.100.202:6379>
******(2)切换到从服务器的reids,查看是否同步,并且是否可以写入
[[email protected] redis-5.0.4]# redis-cli -h 192.168.100.203 -a 123123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.203:6379> keys *
1) "aaa"
192.168.100.203:6379> set bbb ccc
(error) READONLY You can't write against a read only replica. #发现无法写入数据
————————————————————————————————————————————————————
使用info命令在主服务器上查看从服务器复制信息
192.168.100.202:6379> info replication
# Replication
role:master
connected_slaves:1
min_slaves_good_slaves:1
slave0:ip=192.168.100.203,port=6379,state=online,offset=712,lag=0 #slave的信息
master_replid:2550f57c98e18e6cb41d2c9172cc5ee5d2bbebfd
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:712
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:712
边栏推荐
- Analysis of I2C adapter driver of s5pv210 chip (i2c-s3c2410. C)
- 【HCIA持续更新】广域网技术
- Implementation of super large-scale warehouse clusters in large commercial banks
- 【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子
- Win32 API 访问路由的加密网页
- VB cannot access database stocks
- 正则表达式
- 动态规划股票问题对比
- R language plot visualization: plot visualizes overlapping histograms and uses geom at the top edge of the histogram_ The rug function adds marginal rug plots
- Perfectly integrated into win11 style, Microsoft's new onedrive client is the first to see
猜你喜欢
比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
Recast of recastnavigation
五千字讲清楚团队自组织建设 | Liga 妙谈
超大规模数仓集群在大型商业银行的落地实践
Zhijieyun - meta universe comprehensive solution service provider
Talk about seven ways to realize asynchronous programming
Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
MVC mode and three-tier architecture
一文掌握数仓中auto analyze的使用
The Block:USDD增长势头强劲
随机推荐
ARTS_ twenty million two hundred and twenty thousand six hundred and twenty-eight
【Hot100】32. Longest valid bracket
超标量处理器设计 姚永斌 第7章 寄存器重命名 摘录
Display opencv drawn pictures on MFC picture control control
Clever use of curl command
90后开始攒钱植发,又一个IPO来了
Internet addiction changes brain structure: language function is affected, making people unable to speak neatly
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
Heartless sword Chinese translation of Elizabeth Bishop's a skill
就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
To sort out messy header files, I use include what you use
整理混乱的头文件,我用include what you use
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
中断的顶半部和底半部介绍以及实现方式(tasklet 和 工作队列)
Flask 轻量web框架
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
离线、开源版的 Notion—— 笔记软件Anytype 综合评测
开发者,MySQL专栏完更,助你轻松从安装到入门进阶
Is it safe for Great Wall Securities to open an account? How to open a securities account
【Proteus仿真】基于VSM 串口printf调试输出示例