二主从复制redis(一主二从)安装部署
1.配置三台redis并启动
#创建实例的配置文件所在目录 [root@mysql8-0-30 ~]# mkdir /rediscluster #复制配置文件 [root@mysql8-0-30 ~]# cp /etc/redis/redis.conf /rediscluster/redis.conf [root@mysql8-0-30 ~]# cd /rediscluster/ [root@mysql8-0-30 rediscluster]# vim redis.conf appendonly yes改为appendonly no daemonize no改为daemonize yes #创建三个redis的配置文件redis-6379.conf、redis-6380.conf 和 redis-6381.conf [root@mysql8-0-30 rediscluster]# vim redis-6379.conf include /rediscluster/redis.conf #修改端口号 port 6379 #修改pid文件名 pidfile /var/run/redis/redis_6379.pid #修改持久化文件名 dbfilename dump_6379.rdb dir "/rediscluster" [root@mysql8-0-30 rediscluster]# vim redis-6380.conf include /rediscluster/redis.conf port 6380 pidfile /var/run/redis/redis_6380.pid dbfilename dump_6380.rdb dir "/rediscluster" #按照要求向redis-6381.conf添加内容 [root@mysql8-0-30 rediscluster]# vim redis-6381.conf #启动服务 #先确认当前没有进程占用6379端口 [root@mysql8-0-30 rediscluster]# ss -lntup | grep 6379 [root@mysql8-0-30 rediscluster]# redis-server redis-6379.conf [root@mysql8-0-30 rediscluster]# redis-server redis-6380.conf [root@mysql8-0-30 rediscluster]# redis-server redis-6381.conf [root@mysql8-0-30 rediscluster]# ps -ef | grep redis #连接服务 [root@mysql8-0-30 rediscluster]# redis-cli -p 6379 [root@mysql8-0-30 rediscluster]# redis-cli -p 6380 [root@mysql8-0-30 rediscluster]# redis-cli -p 6381
2.查看信息
#6379显示信息 127.0.0.1:6379> info replication # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:cfb3d5f5e9f9a3b7402459073933af9a00d90260 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 #6380显示信息 127.0.0.1:6380> info replication # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:7fd915077bea2d5407859fb69a316a5ee9ee9210 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 #6381显示信息 127.0.0.1:6381> INFO replication # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:aa892355806d09e7bad26c7ae5c2796f94e8ea30 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 #从输出信息可以发现,这三台都是 master,也就是都是主服务器
3.主从配置
假设我们希望 6379 是主服务器,而 6380 和 6381 是从服务器,则需要在从主机上做如下配置。
命令格式:slaveof <ip> <port>
我们分别在 6380 和 6381 客户端中执行如下命令:
127.0.0.1:6380> slaveof 127.0.0.1 6379 OK 127.0.0.1:6381> slaveof 127.0.0.1 6379 OK
注意:
-
在 redis-cli 客户端中执行 slaveof 命令只会对当前环境生效,重启后失效。要想永久生效,需要在 redis.conf 配置文件中添加
slaveof <masterip> <masterport>
配置。再次执行
info replication
命令查看,可以发现已经变为了从机了。#6380信息 127.0.0.1:6380> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_read_repl_offset:126 slave_repl_offset:126 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:49386e3f793d39dd91ecc65bedf637e5e8863a93 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:126 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:127 repl_backlog_histlen:0 127.0.0.1:6380> exit #6381信息 127.0.0.1:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_read_repl_offset:14 slave_repl_offset:14 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:49386e3f793d39dd91ecc65bedf637e5e8863a93 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 127.0.0.1:6381> exit #6379信息 127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6381,state=online,offset=168,lag=0 slave1:ip=127.0.0.1,port=6380,state=online,offset=168,lag=1 master_failover_state:no-failover master_replid:49386e3f793d39dd91ecc65bedf637e5e8863a93 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:168 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:10 48576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:168
4.主从切换
redis-cli -p 6380 127.0.0.1:6380> slaveof no one