Redis 主从复制

本文详细介绍了如何配置Redis的主从复制,包括在配置文件中设置、命令行启动时指定以及使用slaveof命令。此外,还讲解了在同一台机器上启动多个Redis server的方法,如修改端口号、pidfile、logfile和rdb文件名。最后,通过info replication命令检查复制状态,并展示了如何断开复制,将从库转为主库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置复制

配置复制的方式有以下三种:

1)在配置文件中加入 slaveof {masterHost} {masterPort}随redis启动生效

2)在redis-server启动命令后加入 –slaveof {masterHost} {masterPort}生效

3)直接使用命令 :slaveof {masterHost} {masterPort}生效

主从节点复制成功建立之后,可以使用info replication 命令查看复制相关状态。

在一台机器上启动多个redis server

配置过程:

1、拷贝多个redis.confp配置文件

这里写图片描述

2、开启daemonize yes

redis-6379.conf

     daemonize yes

redis-6380.conf

     daemonize yes

redis-6381.conf

     daemonize yes

3、pidfile文件名字

redis-6379.conf

     pidfile /var/run/redis_6379.pid

redis-6380.conf

     pidfile /var/run/redis_6380.pid

redis-6381.conf

     pidfile /var/run/redis_6381.pid

4、端口号

redis-6379.conf

     port 6379

redis-6380.conf

     port 6380

redis-6381.conf

     port 6381

5、logfile文件名字

redis-6379.conf

     logfile “6379.log”

redis-6380.conf

     logfile “6380.log”

redis-6381.conf

     logfile “6381.log”

6、rdb文件名 dbfilename

redis-6379.conf

     dbfilename dump_6379.rdb

redis-6380.conf

     dbfilename dump_6380.rdb

redis-6381.conf

     dbfilename dump_6381.rdb

一主两从

分别启动6379、6380、6381

例如:

[root@redis bin]# ./redis-server /usr/local/redis/etc/redis-6381.conf

info replication查询当前角色

这里写图片描述

slaveof

这里写图片描述

Master info replication

这里写图片描述

Slave info replication

这里写图片描述

slave of no one 断开复制

使当前数据库停止与其他数据库的同步,转成主数据库

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值