学无先后,达者为师

网站首页 编程语言 正文

Redis主从复制关系实现(Linux系统)

作者:小黑孩666 更新时间: 2022-07-22 编程语言

一、假设有三台redis机器,端口别为6380,6382和6384,其中6380端口为主机,其余两台为从机

进入到redis的安装目录,从原有的 redis.conf 拷贝三份,分别命名为 redis6380.conf, redis6382.conf , redis6384.conf

二、把这三个文件的内容清空

上图我是先清空redis6380.conf文件,然后以该文件又拷贝的redis6382.conf和redis6384.conf文件

三、在redis6380.conf主从机中配置以下内容

include /usr/local/redis-4.0.13/redis.conf
daemonize yes
port 6380
pidfile /var/run/redis_6380.pid
logfile 6380.log
dbfilename dump6380.rdb

******配置项说明******

include :包含原来的配置文件内容。/usr/local/redis-4.0.13/redis.conf 按照自己的目录设置。 daemonize:yes 后台启动应用,相当于 ./redis-server & 的作用。

port : 自定义的端口号

pidfile : 自定义的文件,表示当前程序的 pid ,进程 id。

logfile:日志文件名

dbfilename:持久化的 rdb 文件名

四、在redis6382.conf文件中配置以下内容

include /usr/local/redis-4.0.13/redis.conf
daemonize yes
port 6382
pidfile /var/run/redis_6382.pid
logfile 6382.log
dbfilename dump6382.rdb
slaveof 127.0.0.1 6380

******配置项说明******

slaveof :表示当前 Redis 是谁的从。当前是 127.0.0.0 端口 6380 这个 机器 的从。

五、在redis6384.conf文件中配置以下内容

include /usr/local/ redis-4.0.13/redis.conf
daemonize yes
port 6384
pidfile /var/run/redis_6384.pid
logfile 6384.log
dbfilename dump6384.rdb
slaveof 127.0.0.1 6380

六、命令:

①: Redis 客户端使用指定端口连接 Redis 服务器 ./redis-cli -p 端口

②:查看服务器信息 info replication

七、在新窗口分别登录到 6382 ,6384 查看信息

 6384 也登录内容同 6382.

八、在 6380 执行 flushall 清除数据,避免干扰的测试数据。生产环境避免使用

 九、6382,6384 都可以读主 Master 的数据,不能写

至此redis主从复制关系已实现,接下来来模拟容灾处理,即当主redis挂掉后,如何将其中的一台从redis提升为主redis,并且把剩下的从机挂载的主redis

原文链接:https://blog.csdn.net/xiaoheihai666/article/details/125919354

栏目分类
最近更新