【Redis】—04、Redis的主从复制

2022-12-14  本文已影响0人  云之图

1、生产场景:生产环境为什么要做主从同步?

2、Redis主从复制架构简图

image.png

3、主从同步原理

1、slave服务器连接到master服务器,便开始进行数据同步,发送psync命令。

2、master服务器收到psync命令之后,开始执行bgsave命令生成RDB快照文件并使用缓存区记录此后执行的所有写命令。

3、master服务器bgsave执行完之后,就会向所有Slava服务器发送快照文件,并在发送期间继续在缓冲区内记录被执行的写命令。

4、slave服务器收到RDB快照文件后,会将接收到的数据写入磁盘,然后清空所有旧数据,在从本地磁盘载入收到的快照到内存中,同时基于旧的数据版本对外提供服务。

5、master服务器发送完RDB快照文件之后,便开始向slave服务器发送缓冲区中的写命令。

6、slave服务器完成对快照的载入,开始接受命令请求,并执行来自主服务器缓冲区的写命令;

7、如果slave node开启了AOF,那么会立即执行BGREWRITEAOF,重写AOF。

4、主从同步是不会

4.1、redis实例部署

redis的部署详见【Redis】—0.1、Ubuntu20.04源码编译部署redis6.2.7

4.2、节点环境

序号 hostname IP地址
1 db01 172.21.209.40
2 db02 172.21.209.41
3 db03 172.21.209.42

4.3、创建目录并准备配置文件

1、创建目录
 mkdir -p /data/db/redis/6379/
2、配置文件
#三个节点配置一样
cat >> /data/db/redis/6379/redis.conf <<EOF
port 6379
daemonize yes
pidfile /data/db/redis/6379/redis.pid
loglevel notice
logfile "/data/db/redis/6379/redis.log"
dbfilename dump.rdb
dir /data/db/redis/6379/
requirepass 123
masterauth 123
EOF

4.4、启动服务

#通过配置文件启动服务
redis-server  /data/db/redis/6379/redis.conf

4.5、查看端口和进程


root@db01:/data/db/redis/6379# ps -ef|grep redis 
root        7479       1  0 21:04 ?        00:00:00 redis-server 172.21.209.40:6379
root        7489    7440  0 21:05 pts/0    00:00:00 grep --color=auto redis
root@db01:/data/db/redis/6379# 
root@db01:/data/db/redis/6379# netstat  -lntp|grep redis
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      7479/redis-server 1 
tcp        0      0 172.21.209.40:6379      0.0.0.0:*               LISTEN      7479/redis-server 1 
root@db01:/data/db/redis/6379# 

4.6、主从复制环境部署

说明:主节点:db01   从节点db02和db03

db02执行:完成主从部署
root@db02:~# redis-cli  -a 123 SLAVEOF 172.21.209.40 6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
root@db02:~# 

root@db03:~#  redis-cli  -a 123 SLAVEOF 172.21.209.40 6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
root@db03:~# 


4.7、查看主从状态

主节点执行:

root@db01:~# redis-cli -p 6379 -a 123 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=172.21.209.42,port=6379,state=online,offset=0,lag=1
slave1:ip=172.21.209.41,port=6379,state=online,offset=0,lag=1
master_failover_state:no-failover
master_replid:c4652513a72b7721082b613b86eeec34061fe66b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
root@db01:~# 


从节点db02:
root@db02:~# redis-cli -p 6379 -a 123 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:172.21.209.40
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_read_repl_offset:42
slave_repl_offset:42
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c4652513a72b7721082b613b86eeec34061fe66b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:42
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:42
root@db02:~# 



从节点db03:
root@db03:~# redis-cli -p 6379 -a 123 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:172.21.209.40
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:84
slave_repl_offset:84
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c4652513a72b7721082b613b86eeec34061fe66b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:84
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:84
root@db03:~# 


4.8、查看数据同步

主库设置:
root@db01:~# redis-cli  -a 123 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> 
127.0.0.1:6379> SET name zhangsan
OK
127.0.0.1:6379> hmset stu  id 101 name zhangsan age 20 gender m
OK
127.0.0.1:6379> 

从库查看数据:
root@db02:~# redis-cli  -a 123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> 
127.0.0.1:6379> KEYS *
 1) "stu1"
 2) "wo"
 3) "woman"
 4) "1"
 5) "topn"
 6) "key1"
 7) "age"
 8) "key2"
 9) "a"
10) "gender"
11) "b"
12) "stu2"
13) "name"
14) "stu"
127.0.0.1:6379> KEYS name 
1) "name"

至此,redis的主从复制部署完成。

上一篇下一篇

猜你喜欢

热点阅读