当前位置:网站首页>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
边栏推荐
- 【HCIA持续更新】WLAN概述与基本概念
- 斑马识别成狗,AI犯错的原因被斯坦福找到了丨开源
- CocosCreator事件派發使用
- Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
- 【HCIA持续更新】广域网技术
- 大规模服务异常日志检索
- Analysis of I2C adapter driver of s5pv210 chip (i2c-s3c2410. C)
- With an annual income of more than 8 million, he has five full-time jobs. He still has time to play games
- Image retrieval
- 居家打工年入800多万,一共五份全职工作,他还有时间打游戏
猜你喜欢
Recast of recastnavigation
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
我写了一份初学者的学习实践教程!
celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products
上网成瘾改变大脑结构:语言功能受影响,让人话都说不利索
"In Vietnam, money is like lying on the street"
估值900亿,超级芯片IPO来了
【HCIA持续更新】广域网技术
Five thousand words to clarify team self-organization construction | Liga wonderful talk
“在越南,钱就像躺在街上”
随机推荐
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
图像检索(image retrieval)
R语言plotly可视化:plotly可视化多分类变量小提琴图(multiple variable violin plot in R with plotly)
KS007基于JSP实现人个人博客系统
What are cache penetration, cache breakdown, and cache avalanche
【Hot100】31. 下一个排列
“在越南,钱就像躺在街上”
MVC mode and three-tier architecture
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
为啥有些线上演唱会总是怪怪的?
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"
shell脚本的替换功能实现
What if Kaili can't input Chinese???
[test development] software testing - Basics
Is BigDecimal safe to calculate the amount? Look at these five pits~~
Wuzhicms code audit
[daily question] 556 Next bigger element III
简单易用的地图可视化
To sort out messy header files, I use include what you use
Performance test of Gatling