当前位置:网站首页>Redis master-slave replication
Redis master-slave replication
2022-07-04 18:03:00 【Reef of】
List of articles
One 、Redis Master slave replication overview
(1) Master slave replication overview
- In order to share the Redis The pressure of reading and writing ,Redis Support master-slave replication ,Redis The master-slave structure of can adopt one master-slave or cascade structure
- Redis The synchronization types of master-slave replication are divided into full synchronization and incremental synchronization
Full amount of synchronization
Redis The full synchronization of generally occurs in Slave Server initialization phase , That is, just joined the cluster or Slave After downtime and restart , This is the time Slave Need to put Master Synchronize all data on , The following is the process of synchronization :( The command of synchronization is write command )
- **Slave Server connection Master The server , send out SYNC( Sync ) command ,Slave The first time the server sends Master When the server synchronizes , It won't affect Master The server receives the request from the client **
- Master The server receives Slave Server's SYNC After the command , Start execution bgsave Command to take a database snapshot to generate RDB File and use a buffer to record all commands executed since , in other words Master Will take a snapshot of the current database , Then it will put all the subsequent commands into its own cache
- Master Server bgsave After command execution , To all Slave Server send RDB file , And continue to record the executed commands to its own cache during sending
- Slave The server is receiving Master Server's RDB After the document , You will lose all your data , Then load RDB file , In fact, at this time Slave The full backup has been completed , But because RDB There are also executed commands after the file , So we need to Master Commands executed after server synchronization
- Master Server to the Slave The server sends the executed command in the cache
- Slave The server has finished checking RDB After loading the file , Will continue to receive Master The command of the cache sent by the server is synchronized
Once the above operations are completed Slave Initialization from the server , At this time, the server can receive the read request from the user , Then synchronize Master The data of the primary server is incremental synchronization
The incremental synchronization
Redis Incremental synchronization of occurs in Slave When the server starts to work normally after the initialization phase is completed ,Master The write operation of the primary server will be synchronized to Slave Process from server
Master Every time the master server receives a write operation , Will give you Slave Send the same write operation from the server , then Slave Synchronize from the server
(2)Redis Master slave synchronization strategy
- Redis The master connected from the beginning ,Slave The slave server will perform full synchronization
- After full synchronization ,Slave The synchronization after the server is incremental
- If necessary ,Slave The server can be fully synchronized at any time
- Redis Our synchronization strategy is : in any case ,Slave The server will first try to perform incremental synchronization , If synchronization fails , be Slave Full synchronization from the server
- Recommended Opening Master Persistence function of the master server , Because in many stations Slave When the server goes down and needs to be restarted , After restart, each Slave From the server to Master The master server sends SYNC( Sync ) Request , and Master The server performs full synchronization , And that leads to Master Of the main server IO Rapid increase leads to Master Main server down
Two 、 To configure Redis Master-slave replication of
(1) Experimental environment
System | ip | Host name | redis edition | Play a role |
---|---|---|---|---|
Centos7.4 | 192.168.100.202 | master | redis-5.0.4 | master master server |
Centos7.4 | 192.168.100.203 | slave | redis-5.0.4 | slave From the server |
(2) The experiment purpose
Realization Redis Master slave copy
(3) The experimental steps
Configure two servers Redis, install Redis The two servers are the same , Different host names 、ip Different , It's just master Configuration of
[[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 Write protect , Will mount... As read-only
mount: /dev/sr0 Already mounted or /mnt busy
/dev/sr0 It has been attached to /mnt On
[[email protected] ~]# ll
Total usage 1928
-rw-------. 1 root root 1264 1 month 12 18:27 anaconda-ks.cfg
-rw-r--r-- 1 root root 1966337 6 month 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 # modify
......
68 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69 bind 192.168.100.202 # Change to local address , If 127.0.0.1 You can only access it locally
70
......
87 # are explicitly listed using the "bind" directive.
88 protected-mode no # close redis Protection mode of , If yes Other clients cannot connect to this server
89
......
135 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
136 daemonize yes # Turn on redis Background daemon for , That is to say redis After it is turned on, it runs in the background
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 # Remove annotations , modify redis The password for 123123
508
# Save and exit
[[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
To configure master Server master profile
[[email protected] redis-5.0.4]# vim /usr/local/redis/redis.conf # modify
......
456 #
457 min-replicas-to-write 1 # Set up slave Number of servers , When slave When the number of servers is less than this ,Master The master server will stop receiving all write requests from the client
458 min-replicas-max-lag 10 # Set the timeout for synchronizing data between the master server and the slave server , When this time is exceeded ,master The master server will stop all write operations on the client , The unit is in seconds
459 #
......
[[email protected] redis-5.0.4]# /etc/init.d/redis restart # 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...
To configure slave Server master profile
[[email protected] redis-5.0.4]# vim /usr/local/redis/redis.conf
......
285 #
286 replicaof 192.168.100.202 6379 # Specify the master server on the slave server ip And port
287
......
292 #
293 masterauth 123123 # Specify the primary server redis Password
294
......
# Save and exit
[[email protected] redis-5.0.4]# /etc/init.d/redis restart # Restart the service
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...
Verify master-slave replication
******(1) Log in to the main server redis, establish 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) Switch to... From the server reids, See if it's syncing , And whether it can be written
[[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. # Found unable to write data
————————————————————————————————————————————————————
Use info Command to view the replication information from the master server
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 Information about
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
边栏推荐
- Self reflection of a small VC after two years of entrepreneurship
- 中断的顶半部和底半部介绍以及实现方式(tasklet 和 工作队列)
- S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
- 项目通用环境使用说明
- How to test MDM products
- Detectron2 installation method
- Pytorch深度学习之环境搭建
- 整理混乱的头文件,我用include what you use
- 比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
- Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
猜你喜欢
MVC mode and three-tier architecture
斑马识别成狗,AI犯错的原因被斯坦福找到了丨开源
Ks007 realizes personal blog system based on JSP
【HCIA持续更新】WLAN工作流程概述
如何进行MDM的产品测试
解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
Superscalar processor design yaoyongbin Chapter 6 instruction decoding excerpt
表情包坑惨职场人
为啥有些线上演唱会总是怪怪的?
CocosCreator事件派發使用
随机推荐
Is it science or metaphysics to rename a listed company?
通过事件绑定实现动画效果
Offline and open source version of notation -- comprehensive evaluation of note taking software anytype
Image retrieval
ISO27001认证办理流程及2022年补贴政策汇总
What is low code development?
Dynamic programming stock problem comparison
CocosCreator事件派发使用
Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection
【HCIA持续更新】WLAN概述与基本概念
To sort out messy header files, I use include what you use
Implementation of super large-scale warehouse clusters in large commercial banks
Flask lightweight web framework
Clever use of curl command
【每日一题】871. 最低加油次数
五千字讲清楚团队自组织建设 | Liga 妙谈
表情包坑惨职场人
【HCIA持续更新】WLAN工作流程概述
内核中时间相关的知识介绍
Recast of recastnavigation