Python远程操作Redis哨兵+集群(详细配置)

Python操作Redis哨兵+集群(基础配置)

仅供参考

一、配置信息

名称版本
虚拟机centos7
主机macos10.14.6
Redisredis-4.0.10
Python3.7.3

二、依赖包

名称网站
redis-pyhttps://pypi.org/project/redis/
redis-py-clusterhttps://pypi.org/project/redis-py-cluster/

1、Redis主从配置

主要为了实现数据备份读写分离

  1. 创建Redis配置文件夹`
mkdir /etc/init.d/redis
  1. 复制Redis模板配置文件(配置文件一般在/usr/local/redis/下)
cp /usr/local/redis-4.0.10/redis.conf /etc/init.d/redis
  1. 进入Redis文件夹配置
mv redis.conf master.conf

cp master.conf ./slave.conf
  1. 配置master.conf
    由于我们是由本机连接虚拟机
默认配置修改后配置
bind 127.0.0.1bind 0.0.0.0
  1. 配置slave.conf
默认配置修改后配置
bind 127.0.0.1bind 0.0.0.0
port 6379port 6378
#slaveofslaveof 6379
  1. 重启Redis
redis-server /etc/init.d/redis/master.conf
redis-server /etc/init.d/redis/slave.conf
  1. 查看进程
ps aux | grep redis

如果你看到三个进程,那么恭喜你

2、Redis哨兵配置

根据Redis要求,我们最少设置3个哨兵

  1. 复制哨兵配置文件到redis文件目录下
cp sentinel.conf /etc/init.d/redis/sentinel01.conf
  1. 修改sentinel01.conf配置文件
默认配置修改后配置
bind 127.0.0.1bind 0.0.0.0
port 26379port 26380
sentinel monitor mymaster 127.0.0.1 6379 2sentinel monitor mymaster 172.16.154.177 6379 2

172.16.154.177是我的IP,根据自己的IP配置

  1. 复制两份
cp sentinel01.conf ./sentinel02.conf
cp sentinel01.conf ./sentinel03.conf

修改sentinel 文件中的port即可(26380,26381,26382)

4.启动哨兵

redis-sentinel /etc/init.d/redis/sentinel01.conf
redis-sentinel /etc/init.d/redis/sentinel02.conf
redis-sentinel /etc/init.d/redis/sentinel03.conf
# 我把误删redis-sentinel就用了下面的方法来启动
redis-server  /etc/init.d/redis/sentinel01.conf --sentinel
redis-server  /etc/init.d/redis/sentinel02.conf --sentinel
redis-server  /etc/init.d/redis/sentinel03.conf --sentinel
  1. 查看进程
ps aux | grep sentinel

3、Python远程连接Redis哨兵

from redis.sentinel import Sentinel

#配置哨兵IP和端口号
sentinels = [
	('172.16.154.177', 26380),
	('172.16.154.177', 26381),
	('172.16.154.177', 26382),
]
# 创建哨兵对象
sentinel = Sentinel(sentinels=sentinels)

# 主数据库别名(根据自己爱好设置的,叫狗蛋也挺好)
service_name = 'mymaster'

# 通过哨兵获取redis主从
redis_master = sentinel.master_for(service_name=service_name)
redis_slave = sentinel.slave_for(service_name=service_name)

我们可以通过redis_master对象进行读写操作
我们可以通过redis_slave对象进行读操作

记得开启自己虚拟机防火墙

firewall-cmd --add-port=26380/tcp --permanent
firewall-cmd --add-port=26381/tcp --permanent
firewall-cmd --add-port=26382/tcp --permanent
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --add-port=6378/tcp --permanent
#最后执行重启
firewall-cmd --reload
#或者可以关闭防火墙
systemctl stop firewalld.service

4、Redis集群配置

Redis集群要求至少三主三从, 我没有那么多虚拟机。所以我在centos上创建了6个Redis实例

  1. 复制Redis配置文件到redis文件目录下
cp /usr/local/redis-4.0.10/redis.conf /etc/init.d/redis/7000.conf
  1. 修改7000.conf配置文件
默认配置修改后配置
bind 127.0.0.0bind 0.0.0.0
port6379port 7000
cluster-enable nocluster-enable yes
cluster-config-file nodes-6379.confcluster-config-file nodes-7000.conf
appendonly noappendonly yes

修改好配置文件后复制5份,分别命名为7001.conf,7002.conf,7003.conf,7004.conf,7005.conf
分别修改配置文件中的luster-config-file nodes-7000.conf字段为luster-config-file nodes-7001.conf,luster-config-file nodes-7002.conf,luster-config-file nodes-7003.conf,luster-config-file nodes-7004.conf,luster-config-file nodes-7005.conf

  1. 启动Redis实例
redis-server /etc/init.d/redis/7000.conf
redis-server /etc/init.d/redis/7001.conf
redis-server /etc/init.d/redis/7002.conf
redis-server /etc/init.d/redis/7003.conf
redis-server /etc/init.d/redis/7004.conf
redis-server /etc/init.d/redis/7005.conf

这是效果

  1. 创建集群
    我用的是Redis4.0,所以需要用到集群管理工具redis-trib.rb
    在这里插入图片描述
    把这个文件放入/usr/local/bin 目录下
cp redis-trib.rb /usr/local/bin/

安装ruby

https://blog.csdn.net/qq_26440803/article/details/82717244
可以参考这篇博客来进行安装

  1. 创建集群
redis-trib.rb create --replicas 1 0.0.0.0:7000 0.0.0.0:7001 0.0.0.0:7002 0.0.0.0:7003 0.0.0.0:7004 0.0.0.0:7005
  1. 访问集群
    到这里我们就可以访问集群了
redis-cli -p 7000 -c

在这里插入图片描述

5、Python操作Redis集群

from rediscluster import RedisCluster

nodes = [
    {'host': '172.16.154.177', 'port': 7000},
    {'host': '172.16.154.177', 'port': 7001},
    {'host': '172.16.154.177', 'port': 7002},

]

rc = RedisCluster(startup_nodes=nodes, decode_responses=True)
rc.set('name', 'zs')
print(rc.get('name'))

最简单的Redis集群配置就这样了,关于Redis详细配置信息请查阅官方文档

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值