当前位置:网站首页>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
边栏推荐
猜你喜欢

就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
![[HCIA continuous update] overview of WLAN workflow](/img/0a/b3986307589a9f7379fe1dd707b9f8.png)
[HCIA continuous update] overview of WLAN workflow

电子宠物小狗-内部结构是什么?

DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply

【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子

上市公司改名,科学还是玄学?

Firewall basic transparent mode deployment and dual machine hot standby

Offline and open source version of notation -- comprehensive evaluation of note taking software anytype

创业两年,一家小VC的自我反思
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
随机推荐
Heartless sword Chinese translation of Elizabeth Bishop's a skill
简单易用的地图可视化
Ks007 realizes personal blog system based on JSP
图像检索(image retrieval)
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
What is low code development?
Face_ Attendance statistics of recognition face recognition
Is it safe for Great Wall Securities to open an account? How to open a securities account
设置窗体透明 隐藏任务栏 与全屏显示
Pytoch deep learning environment construction
RecastNavigation 之 Recast
KS007基于JSP实现人个人博客系统
Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
Win32 API access route encrypted web pages
To sort out messy header files, I use include what you use
What are cache penetration, cache breakdown, and cache avalanche
[Huawei HCIA continuous update] SDN and FVC
正则表达式
Rainfall warning broadcast automatic data platform bwii broadcast warning monitor
Solve the El input input box For number number input problem, this method can also be used to replace the problem of removing the arrow after type= "number"