当前位置:网站首页>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
边栏推荐
- MVC mode and three-tier architecture
- The Block:USDD增长势头强劲
- 比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
- 【HCIA持续更新】广域网技术
- 超大规模数仓集群在大型商业银行的落地实践
- 华为云ModelArts的使用教程(附详细图解)
- The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
- curl 命令妙用
- What if Kaili can't input Chinese???
- 【Proteus仿真】基于VSM 串口printf调试输出示例
猜你喜欢
Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
Self reflection of a small VC after two years of entrepreneurship
估值900亿,超级芯片IPO来了
Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
离线、开源版的 Notion—— 笔记软件Anytype 综合评测
就在今天丨汇丰4位专家齐聚,共讨银行核心系统改造、迁移、重构难题
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Master the use of auto analyze in data warehouse
为啥有些线上演唱会总是怪怪的?
Easy to use map visualization
随机推荐
[daily question] 556 Next bigger element III
DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
缓存穿透、缓存击穿、缓存雪崩分别是什么
What is low code development?
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
[HCIA continuous update] WLAN overview and basic concepts
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
Achieve animation effect through event binding
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
Dynamic programming stock problem comparison
雨量预警广播自动化数据平台BWII 型广播预警监测仪
智捷云——元宇宙综合解决方案服务商
Rainfall warning broadcast automatic data platform bwii broadcast warning monitor
shell脚本的替换功能实现
Flask lightweight web framework
使用3DMAX制作一枚手雷
无心剑中译伊丽莎白·毕肖普《一门技艺》
CocosCreator事件派发使用
什么是低代码开发?
Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation