Redis主从复制

Redis主从复制介绍

Redis多实例主从复制实例

角色 主机 IP 端口
主库(master) db01 10.0.0.51 6379
从库(slave01) db01 10.0.0.51 6380
从库(slave02) db02 10.0.0.52 6379
从库(slave03) db02 10.0.0.52 6380
从库(slave04) db03 10.0.0.53 6379
从库(slave05) db03 10.0.0.53 6380

redis多实例配置

6379端口安装可查看TP

# 创建多实例目录(db01-db03)
[root@db01 ~]# mkdir -p /etc/redis/6380
[root@db02 ~]# mkdir -p /etc/redis/6380
[root@db03 ~]# mkdir -p /etc/redis/6380

# 编写redis 6380 配置文件
##db01
[root@db01 ~]# vim /etc/redis/6380/redis.conf
port 6380
daemonize yes
pidfile /etc/redis/6380/redis.pid
loglevel notice
logfile /etc/redis/6380/redis.log
dbfilename dump.rdb
dir /etc/redis/6380
bind 127.0.0.1 172.16.1.51
protected-mode no

##db02
[root@db02 ~]# vim /etc/redis/6380/redis.conf
# 监听端口
port 6380
# 是否以后台进程运行
daemonize yes
# pid文件位置
pidfile /etc/redis/6380/redis.pid
# 日志级别,分别有:
# debug :适用于开发和测试
# verbose :更详细信息
# notice :适用于生产环境
loglevel notice
# 日志文件位置
logfile /etc/redis/6380/redis.log
# rdb持久化数据文件名
dbfilename dump.rdb
# 数据库(dump.rdb)文件存放目录
dir /etc/redis/6380
# 监听在指定的IP地址
bind 127.0.0.1 172.16.1.52
# 关闭保护模式(不关的情况,非本机连接后,操作不了)
protected-mode no

##db03
[root@db03 ~]# vim /etc/redis/6380/redis.conf
port 6380
daemonize yes
pidfile /etc/redis/6380/redis.pid
loglevel notice
logfile /etc/redis/6380/redis.log
dbfilename dump.rdb
dir /etc/redis/6380
bind 127.0.0.1 172.16.1.53
protected-mode no

# 使用system管理redis
##db01
[root@db01 ~]# cp /usr/lib/systemd/system/{redis,redis6380}.service
[root@db01 ~]# vim /usr/lib/systemd/system/redis6380.service 
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/app/redis/src/redis-server /etc/redis/6380/redis.conf

[Install]
WantedBy=multi-user.target

##db02
[root@db02 ~]# cp /usr/lib/systemd/system/{redis,redis6380}.service
[root@db02 ~]# vim /usr/lib/systemd/system/redis6380.service 
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/app/redis/src/redis-server /etc/redis/6380/redis.conf

[Install]
WantedBy=multi-user.target

##db03
[root@db03 ~]# cp /usr/lib/systemd/system/{redis,redis6380}.service
[root@db03 ~]# vim /usr/lib/systemd/system/redis6380.service 
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/app/redis/src/redis-server /etc/redis/6380/redis.conf

[Install]
WantedBy=multi-user.target

# 启动redis
##db01
[root@db01 ~]# systemctl start redis
[root@db01 ~]# systemctl start redis6380

##db02
[root@db02 ~]# systemctl start redis
[root@db02 ~]# systemctl start redis6380

##db03
[root@db03 ~]# systemctl start redis
[root@db03 ~]# systemctl start redis6380

开启主从复制

# 修改主库配置文件
[root@db01 ~]# vim /app/redis/redis.conf 
requirepass 123

# 修改从库配置文件
[root@db01 ~]# vim /etc/redis/6380/redis.conf
requirepass 123
masterauth 123

[root@db02 ~]# vim /app/redis/redis.conf
requirepass 123
masterauth 123
[root@db02 ~]# vim /etc/redis/6380/redis.conf
requirepass 123
masterauth 123

[root@db03 ~]# vim /app/redis/redis.conf
requirepass 123
masterauth 123
[root@db03 ~]# vim /etc/redis/6380/redis.conf
requirepass 123
masterauth 123

# 重启redis
[root@db01 ~]# systemctl restart redis redis6380
[root@db02 ~]# systemctl restart redis redis6380
[root@db03 ~]# systemctl restart redis redis6380

# 连接从库
[root@db01 ~]# redis-cli -a 123 -p 6380 
[root@db02 ~]# redis-cli -a 123 -p 6379
[root@db02 ~]# redis-cli -a 123 -p 6380
[root@db03 ~]# redis-cli -a 123 -p 6379
[root@db03 ~]# redis-cli -a 123 -p 6380
# 开启主从(指定主库)
127.0.0.1:6380> SLAVEOF 172.16.1.51 6379

# 连接master(6379)
[root@db01 ~]# redis-cli -a 123 -p 6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

# 查看主从复制详情
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:5
slave0:ip=172.16.1.51,port=6380,state=online,offset=350,lag=0
slave1:ip=172.16.1.52,port=6379,state=online,offset=350,lag=1
slave2:ip=172.16.1.52,port=6380,state=online,offset=350,lag=1
slave3:ip=172.16.1.53,port=6379,state=online,offset=350,lag=0
slave4:ip=172.16.1.53,port=6380,state=online,offset=350,lag=0
master_failover_state:no-failover
master_replid:75e2468491ef8092ffd85374314f5197f41815d6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:350
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:350

主从切换(手动)

# 取消从库身份

127.0.0.1:6380> slaveof no one

当主库宕机了,主从是如何切换的?

[root@db01 6380]# systemctl stop redis
# 取消从库身份
127.0.0.1:6380> slaveof no one
OK
127.0.0.1:6380> slaveof 172.16.1.51 6380
OK
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:172.16.1.51
master_port:6380
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_read_repl_offset:7462
slave_repl_offset:7462
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:95c2a415d0200ced2e2029a7afe37adad7676f75
master_replid2:b452962e762b8c18ec4b893feee86434eba2b12a
master_repl_offset:7462
second_repl_offset:7463
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2969
repl_backlog_histlen:4494
posted @ 2023-10-09 08:40  普里莫  阅读(3)  评论(0编辑  收藏  举报