陈奇网络工作室

Redis数据库主从复制

系统运输

Redis数据库主从复制

一、主从复制概述;

二、同步详解;

三、案例:配置主从复制;

一.主从复制概述:

摘要:为了分担读写压力,Redis支持主从复制。 Redis的主从结构可采用一主主从或级联结构,根据Redis主从复制是否全量可分为全量同步和增量同步。 下图为级联结构。

二、同期详情:

同步类型:全量同步、增量同步

1 .总量同步:

Redis的全部复制通常发生在Slave的初始化阶段。 在这种情况下,Slave必须复制主服务器上的所有数据。 具体步骤如下。

1 )从服务器连接到主服务器,然后发送SYNC命令(在初始从服务器同步到主服务器时,主服务器不会接收到来自客户端的请求)。

2 )主服务器接收到SYNC命名后,启动BGSAVE命令的执行,生成RDB文件,使用缓冲区记录所有后续执行的写入命令;

3 )主服务器BGSAVE运行后,是否要将快照文件发送到所有辅助服务器并继续记录发送期间执行的写入命令?

4 )从服务器收到快照文件后,是否要销毁所有旧数据并加载收到的快照?

5 )主服务器快照发送完成后,开始向从服务器发送缓冲区中的写入命令?

6 )从服务器加载快照完成,开始接收命令请求,执行主服务器缓冲区的写命令?

完成上述步骤后,来自服务器数据的初始化的所有操作都将完成,并且可以从服务器接收来自用户的读取请求。

2 .增量同步:

Redis增量复制是指在Slave初始化后开始正常运行时,主服务器上的写入操作与从服务器同步的过程。 增量复制的过程主要是在主服务器每次执行写入命令时,向从服务器发送相同的写入命令,然后接收从服务器收到的写入命令。

同步策略:

主从刚接通时,进行全量同步;

完全同步完成后,进行增量同步。 当然,如果需要,slave可以随时开始全量同步。 无论如何,redis策略都是首先尝试差分同步,如果不成功,则要求从站进行全量同步。

注:

如果多个Slave断开且需要重新启动,则主io可能会急剧增加并停机,因为只要Slave启动,sync请求就会发送到主机并全部同步。

建议打开主持久化功能,以便在主服务器重新启动后无法恢复数据

三、案例:配置主从复制:

实验步骤:

? 安装并配置主角色redis服务;

? 安装并配置slave角色的redis服务(双实例);

? 用于设置主角色的文件;

? 构成slave角色的文件;

? 主从复制验证;

? 安装并配置主角色redis服务;

[ root @ master~] # wget http://download.redis.io/releases/redis-4.0.9.tar.gz

[ root @ master~] # tarzxvfredis-4.0.9.tar.gz

[root@master ~]# cd redis-4.0.9

[root@master redis-4.0.9]# make

[root@master redis-4.0.9]# echo $?

[root@master redis-4.0.9]# cd光盘

[ root @ master~] # mkdir-p/usr/local/redis

[ root @ master~] # CP/root/redis-4.0.9/src/redis-server/usr/local/redis/# #服务器端程序

[ root @ master~] # CP/root/redis-4.0.9/src/redis-CLI/usr/local/redis/# #客户端程序

[ root @ master~] # CP/root/redis-4.0.9/redis.conf/usr/local/redis/# #主配置文件

[ root @ master~] # ls/usr/local/redis /

redis-CLI redis.conf redis-server

[ root @ master~] # sed-I\/168.100.0.0 $/s/127.0.1/192.168.100.101/g \

[root@master~]#sed-I\\& quot;/protected-mode/s/yes/no/g\& amp; quot; /usr/local/redis/redis.conf

[root@master~]#sed-I\\& quot; /daemonize/s/no/yes/g\\& quot; 打开/usr/local/redis/redis.conf # # redis

[root@master~]#sed-I\\\& #039;/require pass/s/foobar ed/123123/g\\ & amp; #039; /usr/local/redis/redis.( ) )/usr/local/redis

[root@master~]#sed-I\\\& #039; /requirepass123123/s/^#/g\\& quot; /usr/local/redis/redis.conf#

[ root @ master~] # ln-s/usr/local/redis/redis-CLI/usr/local/bin/redis

[ root @ master~] # cat end/etc/init.d/redis

#! /bin/sh

# chkconfig: 2345 80 90

# description :开始和停止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=\\& quot; ( ( netstat-utpln|grep redis-server|awk\& amp; quot; {print\\& quot; ( ( $4) ) ) (|awk ( ( ) ) ) ) ( print ( ) ) )

case \\\\$1 in

start )

if [ -f \\\\$PIDFILE ]

then

echo \\\\$PIDFILE exists,processisalreadyrunningorcrashed

else

echo Starting Redis server .

\\\\& quot; exec\\& quot; conf

fi

if [ \\\\$& quot;=0 ]

then

echo Redis is running .

fi

stop )

if [! -f \\\\$PIDFILE ]

then

echo \\\\$PIDFILE does not exist,process is not running

else

PID=\\\& quot; $(cat\\& quot; $pidfile )

echo Stopping .

\\$ redis _ CLI-h\\ $ listen _ IP-p\\ $ re disport-a\\ $ auths hutdown

while [ -x \\\\${PIDFILE} ]

do

echo Waiting for Redis to shutdown .

第一阶段

echo Redis stopped

fi

重新开始)

\\\\${0} stop

\\\\${0} start

* )

echo usage:/etc/init.d/redis { start|stop } 2

exit 1

欧洲航空

结束

[ root @ master~] # chmod 755/etc/init.d/redis

[ root @ master~] # chkconfig-- add redis

[ root @ master~~ ] #/etc/init.d/redis start

开始就绪服务器.

4390:c 04 may 02:16:45.232 # oo0ooo0ooo0ooredisisstartingoo0ooo0ooo0ooo 0

4390:c 04 may 02:16:45.232 # redis version=4.0. 9,bits=64,commit=00000000,modified=0,pid=4390,just stastared

4390:c 04 may 02:16:45.232 #配置加载

Redis is running .

[ root @ master~] # netstat-utpln|grep redis

TCP 0192.168.100.101:63790.0.0.0:* listen 4204/redis-server *

[ root @ master~] # redis-h 192.168.100.101-a 123123-p 6379

192.168.100.101:6379退出

? 安装并配置slave角色的redis服务(两个实例);

[ root @ slave~] # wget http://download.redis.io/releases/redis-4.0.9.tar.gz

[ root @ slave~] # tarzxvfredis-4.0.9.tar.gz

[root@slave ~]# cd redis-4.0.9

[root@slave redis-4.0.9]# make

[root@slave redis-4.0.9]# echo $?

[root@slave redis-4.0.9]# cd光盘

[ root @ slave~] # mkdir-p/usr/local/redis

[ root @ slave~] # CP/root/redis-4.0.9/src/redis-server/usr/local/redis/# #服务器端程序

[ root @ slave~] # CP/root/redis-4.0.9/src/redis-CLI/usr/local/redis/# #客户端程序

[ root @ slave~] # CP/root/redis-4.0.9/redis.conf/usr/local/redis/# #主配置文件

[ root @ slave~] # ls/usr/local/redis /

redis-CLI redis.conf redis-server

[root@slave~]#sed-I\\& quot;/^ bind 127.0.0 $/s/127.0.0.1/192.168.100.102/g\& amp; quot;

[root@slave~]#sed-I\\& quot;/protected-mode/s/yes/no/g\& amp; quot; /usr/local/redis/redis.conf

[root@slave~]#sed-I\\& quot; /daemonize/s/no/yes/g\\& quot; 选中/usr/local/redis/redis.conf##

[root@slave~]#sed-I\\& quot;/require pass/s/foobar ed/123123/g\\ & amp; quot; /usr/local/redis/redis.ccc

[root@slave~]#sed-I\\& quot; /requirepass123123/s/^#/g\\& quot; /usr/local/redis/redis.conf#

[ root @ slave~] # CP/usr/local/redis/redis.conf/usr/local/redis/redis 01.conf

[root@slave~]#sed-I\\& quot; 92s/6379/6380/g\\& quot; 更改/usr/local/redis/redis 01.conf # #监听端口

[root@slave~]#sed-I\\& quot; 158s/6379/6380/g\\& quot; 修改/usr/local/redis/redis 01.conf # # PID文件

[root@slave~]#sed-I\\\& quot; 171s/^\\& quot; (() ) ) ) ) ( (\\ ) ) ) ) ) (()/logfile\( ) ) ) ) ) 652

[ root @ slave~] # ln-s/usr/local/redis/redis-CLI/usr/local/bin/redis

[ root @ slave~] # cat end/etc/init.d/redis

#! /bin/sh

# chkconfig: 2345 80 90

# description :开始和停止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=\\& quot; ( ( netstat-utpln|grep redis-server|awk\& amp; quot; {print\\& quot; ( ( $4) ) ) )|awk ( ( ( ( ) ) ) ( print ) ) )

case \\\\$1 in

start )

if [ -f \\\\$PIDFILE ]

then

echo \\\\$PIDFILE exists,processisalreadyrunningorcrashed

else

echo Starting Redis server .

\\\\& quot; exec\\& quot; conf

fi

if [ \\\\$& quot;=0 ]

then

echo Redis is running .

fi

stop )

if [! -f \\\\$PIDFILE ]

then

echo \\\\$PIDFILE does not exist,process is not running

else

PID=\\\& quot; $(cat\\& quot; $pidfile )

echo Stopping .

\\$ redis _ CLI-h\\ $ listen _ IP-p\\ $ re disport-a\\ $ auths hutdown

while [ -x \\\\${PIDFILE} ]

do

echo Waiting for Redis to shutdown .

第一阶段

echo Redis stopped

fi

重新开始)

\\\\${0} stop

\\\\${0} start

* )

echo usage:/etc/init.d/redis { start|stop } 2

exit 1

欧洲航空

结束

[ root @ slave~] # chmod 755/etc/init.d/redis

[ root @ slave~] # chkconfig-- add redis

[ root @ slave~] # CP/etc/init.d/redis/etc/init.d/redis 01

[root@slave~]#sed-I\\& quot; s/6379/6380/g\\& quot; /etc/init.d/redis01

[root@slave~]#sed-I\\& quot;/conf=/s/redis.conf/redis 01.conf/g\\ & amp; quot; /etc/init.d/redis01

[ root @ slave~] #/etc/init.d/redis start

开始就绪服务器.

4390:c 04 may 02:16:45.232 # oo0ooo0ooo0ooredisisstartingoo0ooo0ooo0ooo 0

4390:c 04 may 02:16:45.232 # redis version=4.0. 9,bits=64,commit=00000000,modified=0,pid=4390,just stastared

4390:c 04 may 02:16:45.232 #配置加载

Redis is running .

[ root @ slave~] #/etc/init.d/redis 01 start

开始就绪服务器.

4390:c 04 may 02:16:45.232 # oo0ooo0ooo0ooredisisstartingoo0ooo0ooo0ooo 0

4390:c 04 may 02:16:45.232 # redis version=4.0. 9,bits=64,commit=00000000,modified=0,pid=4390,just stastared

4390:c 04 may 02:16:45.232 #配置加载

Redis is running .

[ root @ slave~] # netstat-utpln|grep redis

TCP 0192.168.100.102:63790.0.0.0:* listen 11864/redis-server

TCP 0192.168.100.102:6380.0.0.0:* listen 11877/redis-server

[ root @ slave~] # redis-h 192.168.100.102-a 123123-p 6379

192.168.100.102:6379退出

[ root @ slave~] # redis-h 192.168.100.102-a 123123-p 6380

192.168.100.102:6380退出

? 用于设置主角色的文件;

[root@master~]#sed-I\\\& quot; 450s/^\\& quot; (() ) ) ) ) ( (\) ) ) ) /最小间隔( ) ) ) ) 65

设置#slave节点的数量。 如果slave节点的数量小于此值,则主节点将停止来自客户端的所有写入请求

[root@master~]#sed-n\\\& quot; 451s/^\\& quot; (() ) ) ) ) ( (\) ) ) ) /最小间隔( ) ) ) ) ) 65

#master和slave之间的数据同步超时时间。 如果超过此时间,主节点将停止所有客户端写入操作

[ root @ master~~ ] #/etc/init.d/redis restart

停止.

Redis stopped

开始就绪服务器.

1638:c15 may 16:32:08.301 # oo0ooo0ooo0ooredisisstartingoo0ooo0ooo 0

1638:c15 may 16:32:08.301 # redis version=4.0. 9,bits=64,commit=00000000,modified=0,pid=1638,juststastatatation

1638:c15 may 16:32:08.301 #配置加载

Redis is running .

? 构成slave角色的文件;

[root@slave~]#sed-I\\\& quot; 281s/^\\& quot; (() ) ) ) ) (\) ) (() ) ) (() )/slaveof192.) ) ) )

指定主ip地址和端口

[root@slave~]#sed-I\\\& quot; 288s/^\\& quot; (() ) ) ) ) ) ) ) ) ) ) ) ) )/masterauth1298;

#指定主机的连接密码

[ root @ slave~] #/etc/init.d/redis restart

/var/run/redis _ 6379.piddoesnotexist,process is not running

开始就绪服务器.

4387:c 18 may 03:24:00.027 # oo0ooo0ooo0ooredisisstartingoo0ooo0ooo0ooo 0

487:c 18 may 03:24:00.027 # redis version=4.0. 9,bits=64,commit=00000000,modified=0,pid=4387,juststata

4387:c 18 may 03:24:00.027 #配置加载

Redis is running .

[root@slave~]#sed-I\\\& quot; 281s/^\\& quot; (() ) ) ) ) (\) ) (() ) ) (() )/slaveof192.) ) ) )

[root@slave~]#sed-I\\\& quot; 288s/^\\& quot; (() ) ) ) ) ( (\) ) ) ) )/masterauth1298;

[ root @ slave~] #/etc/init.d/redis 01 restart

停止.

Redis stopped

开始就绪服务器.

Redis is running .

? 主从复制验证;

[ root @ master~] # redis-h 192.168.100.101-a 123123-p 6379 # #在主节点上创建键值对

192.168.100.101:6379设置名称小命令

好的

192.168.100.101:6379 keys *

1 ) name

192.168.100.101:6379 get name

小明

192.168.100.101:6379信息复制# #查看复制信息

#复制

角色:主

connected_slaves:2

min_slaves_good_slaves:2

slave0:ip=192.168.100.102、port=6380、state=online、offset=522、lag=1

slave1:ip=192.168.100.102,port=6379,state=online,offset=522,lag=1

master _ replid:46e C2C 7e 971 d 337 d 060 d 183 d 3a0c 1313 C5 DD 1683

master _ repli D2:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

master_repl_offset:522

second_repl_offset:-1

repl_backlog_active:1

repl_backlog_size:1048576

repl _ backlog _ first _ byte _ offset:1

repl_backlog_histlen:522

192.168.100.101:6379退出

[ root @ slave~] # redis-h 192.168.100.102-p 6379-a 123123 # #登录并在slave节点上验证密钥同步并测试无法写入

192.168.100.102:6379 keys *

1 ) name

192.168.100.102:6379 get name

小明

192.168.100.102:6380 set name 1小手机

( error ) readonly you can ( ( twriteagainstareadonlyslave。

192.168.100.102:6379退出

[ root @ slave~] # redis-h 192.168.100.102-p 6380-a 123123

192.168.100.102:6380 keys *

1 ) name

192.168.100.102:6380 get name

小明

192.168.100.102:6380退出

[ root @ master~] #重新启动reboot # # master节点,验证redis的默认RDB持久性

[ root @ master~~ ] #/etc/init.d/redis restart

[ root @ master~] # redis-h 192.168.100.101-a 123123-p 6379

192.168.100.101:6379 keys *

1 ) name

192.168.100.101:6379退出

[ root @ slave~] # redis-h 192.168.100.102-p 6380-a 123123

192.168.100.102:6380 keys *

1 ) name

192.168.100.102:6380 get name

小明

192.168.100.102:6380退出

详情请访问云服务器、域名注册、虚拟主机的问题,请访问西部数码代理商官方网站: www.chenqinet.cn

相关推荐

后台-系统设置-扩展变量-手机广告位-内容页底部广告位3