文档课题:redis主从复制集群环境搭建.
操作系统:rhel 7.9 64位
数据库:redis 6.2.6
主机名:主库leo-redis626-a,从库1 leo-redis626-b,从库2 leo-redis626-c
IP信息:主库192.168.133.100,从库1 192.168.133.101,从库2 192.168.133.102
端口号:主库 6379,从库1 6380,从库2 6381
1、拓扑结构
2、准备工作
--准备三台Linux服务器,且安装好redis 6.2.6环境.
[root@leo-redis626-a redis-6.2.6]# ps -ef|grep redis
avahi 870 1 0 20:18 ? 00:00:00 avahi-daemon: running [leo-redis626-a.local]
root 57363 57302 0 23:39 pts/0 00:00:00 redis-server *:6379
root 57396 2484 0 23:40 pts/1 00:00:00 grep --color=auto redis
[root@leo-redis626-b redis-6.2.6]# ps -ef|grep redis
avahi 854 1 0 20:31 ? 00:00:00 avahi-daemon: running [leo-redis626-b.local]
root 57079 57011 0 23:39 pts/0 00:00:00 redis-server *:6379
root 57093 2303 0 23:39 pts/1 00:00:00 grep --color=auto redis
[root@leo-redis626-c redis-6.2.6]# ps -ef|grep redis
avahi 871 1 0 20:29 ? 00:00:00 avahi-daemon: running [leo-redis626-c.local]
root 57044 56993 0 23:38 pts/0 00:00:00 redis-server *:6379
root 57054 2334 0 23:38 pts/1 00:00:00 grep --color=auto redis
--关闭三台服务器redis进程,开始相关配置.
[root@leo-redis626-a redis-6.2.6]# kill -9 57363
[root@leo-redis626-b redis-6.2.6]# kill -9 57079
[root@leo-redis626-c redis-6.2.6]# kill -9 57044
3、配置主从
3.1、编写配置文件
--配置第一台服务器,新建redis6379.conf文件.
[root@leo-redis626-a redis-6.2.6]# pwd
/usr/local/src/redis-6.2.6
[root@leo-redis626-a redis-6.2.6]# vi redis6379.conf
添加如下内容:
include /usr/local/src/redis-6.2.6/redis.conf
pidfile /var/run/redis_6379.pid
port 6379
dbfilename dump6379.rdb
daemonize yes
bind 0.0.0.0
logfile "/usr/local/src/redis-6.2.6/redis.log"
说明:
A、参数说明
第一行:包含此前安装redis目录的配置.
第四行:数据持久化的rdb文件.
第五行:“daemonize yes”表示以后台运行redis进程.
第六行:“bind 0.0.0.0”防止从库执行slaveof时报错“Error condition on socket for SYNC: No route to host”,该参数仅在主库配置.
第七行:设置logfile目录文件.
B、后续以该配置文件启动redis服务.
--从库1新建redis6380.conf文件.
[root@leo-redis626-b redis-6.2.6]# vi redis6380.conf
添加如下内容:
include /usr/local/src/redis-6.2.6/redis.conf
pidfile /var/run/redis_6380.pid
port 6380
dbfilename dump6380.rdb
daemonize yes
logfile "/usr/local/src/redis-6.2.6/redis.log"
--从库2新建redis6381.conf文件.
[root@leo-redis626-c redis-6.2.6]# vi redis6381.conf
添加如下内容:
include /usr/local/src/redis-6.2.6/redis.conf
pidfile /var/run/redis_6381.pid
port 6381
dbfilename dump6381.rdb
daemonize yes
logfile "/usr/local/src/redis-6.2.6/redis.log"
3.2、关闭防火墙
--主库
[root@leo-redis626-a redis-6.2.6]# systemctl stop firewalld
[root@leo-redis626-a redis-6.2.6]# systemctl disable firewalld
--从库1
[root@leo-redis626-b redis-6.2.6]# systemctl stop firewalld
[root@leo-redis626-b redis-6.2.6]# systemctl disable firewalld
--从库2
[root@leo-redis626-c redis-6.2.6]# systemctl stop firewalld
[root@leo-redis626-c redis-6.2.6]# systemctl disable firewalld
3.3、开启redis进程
--启动三台服务器的redis进程.
--主库
[root@leo-redis626-a redis-6.2.6]# redis-server redis6379.conf
[root@leo-redis626-a ~]# ps -ef|grep redis
avahi 870 1 0 Apr02 ? 00:00:00 avahi-daemon: running [leo-redis626-a.local]
root 57625 2484 0 Apr02 pts/1 00:00:00 redis-server 127.0.0.1:6379
root 57679 57639 0 00:00 pts/2 00:00:00 grep --color=auto redis
[root@leo-redis626-a ~]# redis-cli -p 6379
127.0.0.1:6379> ping
PONG
--从库1
[root@leo-redis626-b redis-6.2.6]# redis-server redis6380.conf
[root@leo-redis626-b ~]# ps -ef|grep redis
avahi 854 1 0 Apr02 ? 00:00:00 avahi-daemon: running [leo-redis626-b.local]
root 57384 2303 0 00:00 pts/1 00:00:00 redis-server 127.0.0.1:6380
root 57459 57389 0 00:01 pts/2 00:00:00 grep --color=auto redis
[root@leo-redis626-b ~]# redis-cli -p 6380
127.0.0.1:6380> ping
PONG
--从库2
[root@leo-redis626-c redis-6.2.6]# redis-server redis6381.conf
[root@leo-redis626-c ~]# ps -ef|grep redis
avahi 871 1 0 Apr02 ? 00:00:00 avahi-daemon: running [leo-redis626-c.local]
root 57434 2334 0 00:01 pts/1 00:00:00 redis-server 127.0.0.1:6381
root 57441 57390 0 00:02 pts/2 00:00:00 grep --color=auto redis
[root@leo-redis626-c ~]# redis-cli -p 6381
127.0.0.1:6381> ping
PONG
4、配置主从
目标:配置leo-redis626-a作为主节点,leo-redis626-b、leo-redis626-c作为从节点.
--从库1
127.0.0.1:6380> slaveof 192.168.133.100 6379
OK
--从库2
127.0.0.1:6381> slaveof 192.168.133.100 6379
OK
说明:slaveof仅在从库执行.
5、结果验证
5.1、节点信息
--主节点查看.
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.133.101,port=6380,state=online,offset=280,lag=1
slave1:ip=192.168.133.102,port=6381,state=online,offset=280,lag=1
master_failover_state:no-failover
master_replid:42036cfd230f20054ec04042c0f001c3b083c3ce
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:280
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:280
--从库1查看.
[root@leo-redis626-b redis-6.2.6]# redis-cli -p 6380
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:192.168.133.100
master_port:6379
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_read_repl_offset:3556
slave_repl_offset:3556
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:42036cfd230f20054ec04042c0f001c3b083c3ce
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3556
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3556
--从库2查看.
127.0.0.1:6381> info replication
# Replication
role:slave
master_host:192.168.133.100
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:3612
slave_repl_offset:3612
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:42036cfd230f20054ec04042c0f001c3b083c3ce
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3612
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:267
repl_backlog_histlen:3346
5.2、添加键值测试
--主库添加键值.
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> set key1 val1
OK
--从库1查询
127.0.0.1:6380> keys *
1) "key1"
127.0.0.1:6380> get key1
"val1"
--从库2查询
127.0.0.1:6381> keys *
1) "key1"
127.0.0.1:6381> get key1
"val1"
说明:如上所示,1主2从环境搭建完成.
6、架构缺点
redis主从复制存在主机Master宕机后,写服务无法使用,此时需要手动切换主从关系.
这样会造成一段时间内服务不可用.更多时候应考虑哨兵模式.
标签:redis626,主从复制,redis,00,leo,集群,6.2,root
From: https://blog.51cto.com/u_12991611/6167493