当前位置:网站首页>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
边栏推荐
- Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
- With an annual income of more than 8 million, he has five full-time jobs. He still has time to play games
- 就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
- 缓存穿透、缓存击穿、缓存雪崩分别是什么
- 解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
- Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
- 【Proteus仿真】基于VSM 串口printf调试输出示例
- CocosCreator事件派发使用
- Heartless sword Chinese translation of Elizabeth Bishop's a skill
- [daily question] 871 Minimum refueling times
猜你喜欢

【HCIA持续更新】WLAN概述与基本概念

To sort out messy header files, I use include what you use

Vb无法访问数据库stocks

VSCode修改缩进不成功,一保存就缩进四个空格

Rainfall warning broadcast automatic data platform bwii broadcast warning monitor

数学分析_笔记_第7章:多元函数的微分学

Vscode modification indentation failed, indent four spaces as soon as it is saved

居家打工年入800多万,一共五份全职工作,他还有时间打游戏

celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products

【HCIA持续更新】网络管理与运维
随机推荐
Easy to use map visualization
使用3DMAX制作一枚手雷
About the pit of firewall opening 8848 when Nacos is started
比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
上市公司改名,科学还是玄学?
Cocoscreator event dispatch use
curl 命令妙用
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
中断的顶半部和底半部介绍以及实现方式(tasklet 和 工作队列)
[HCIA continuous update] overview of WLAN workflow
[Huawei HCIA continuous update] SDN and FVC
Ble HCI flow control mechanism
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
超标量处理器设计 姚永斌 第6章 指令解码 摘录
CocosCreator事件派发使用
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
【HCIA持续更新】网络管理与运维
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
创业两年,一家小VC的自我反思
防火墙基础透明模式部署和双机热备