python操作redis集群_python 连接管理作redis集群
python的redis库是不支持集群操作的,推荐库:redis-py-cluster。
安装
pip3 install redis-py-cluster
连接redis集群
#!/usr/bin/env python
# coding: utf-8
from rediscluster import StrictRedisCluster
class RedisCluster(object): # 连接redis集群
def __init__(self,conn_list):
self.conn_list = conn_list # 连接列表
def connect(self):
"""
连接redis集群
:return: object
"""
try:
# 非密码连接redis集群
# redisconn = StrictRedisCluster(startup_nodes=self.conn_list)
# 使用密码连接redis集群
redisconn = StrictRedisCluster(startup_nodes=self.conn_list, password='123456')
return redisconn
except Exception as e:
print(e)
print("错误,连接redis 集群失败")
return False
redis_basis_conn = [{'host': '192.168.10.168', 'port': 7201}, {'host': '192.168.10.169', 'port': 7201}, {'host': '192.168.10.170', 'port': 7201}, {'host': '192.168.10.171', 'port': 7201}, {'host': '192.168.10.142', 'port': 7201}, {'host': '192.168.10.143', 'port': 7201}]
res = RedisCluster(redis_basis_conn).connect()
if not res:
print("连接redis集群失败")
else:
print("连接redis集群成功")
执行输出:
连接redis集群成功
二、操作redis集群
查看节点状态
#!/usr/bin/env python
# coding: utf-8
from rediscluster import StrictRedisCluster
class RedisCluster(object): # 连接redis集群
def __init__(self,conn_list):
self.conn_list = conn_list # 连接列表
def connect(self):
"""
连接redis集群
:return: object
"""
try:
# 非密码连接redis集群
# redisconn = StrictRedisCluster(startup_nodes=self.conn_list)
# 使用密码连接redis集群
redisconn = StrictRedisCluster(startup_nodes=self.conn_list, password='123456')
return redisconn
except Exception as e:
print(e)
print("错误,连接redis 集群失败")
return False
def get_state(self):
"""
获取状态
:return:
"""
res = RedisCluster(self.conn_list).connect()
# print("连接集群对象",res,type(res),res.__dict__)
if not res:
return False
dic = res.cluster_info() # 查看info信息, 返回dict
for i in dic: # 遍历dict
ip = i.split(":")[0]
if dic[i].get('cluster_state'): # 获取状态
print("节点状态, ip: ", ip, "value: ", dic[i].get('cluster_state'))
redis_basis_conn = [{'host': '192.168.10.168', 'port': 7201}, {'host': '192.168.10.169', 'port': 7201}, {'host': '192.168.10.170', 'port': 7201}, {'host': '192.168.10.171', 'port': 7201}, {'host': '192.168.10.142', 'port': 7201}, {'host': '192.168.10.143', 'port': 7201}]
RedisCluster(redis_basis_conn).get_state()
执行输出:
节点状态, ip: 192.168.10.171 value: ok
节点状态, ip: 192.168.10.169 value: ok
节点状态, ip: 192.168.10.143 value: ok
节点状态, ip: 192.168.10.142 value: ok
节点状态, ip: 192.168.10.170 value: ok
节点状态, ip: 192.168.10.168 value: ok
查看aof是否开启
#!/usr/bin/env python
# coding: utf-8
from rediscluster import StrictRedisCluster
class RedisCluster(object): # 连接redis集群
def __init__(self,conn_list):
self.conn_list = conn_list # 连接列表
def connect(self):
"""
连接redis集群
:return: object
"""
try:
# 非密码连接redis集群
# redisconn = StrictRedisCluster(startup_nodes=self.conn_list)
# 使用密码连接redis集群
redisconn = StrictRedisCluster(startup_nodes=self.conn_list, password='123456')
return redisconn
except Exception as e:
print(e)
print("错误,连接redis 集群失败")
return False
def get_info(self):
"""
获取redis集群info信息
:return: dict
"""
res = RedisCluster(self.conn_list).connect()
# print("连接集群对象",res,type(res),res.__dict__)
if not res:
return False
dic = res.cluster_info() # 查看info信息, 返回dict
if not dic:
return False
return dic
def get_state(self):
"""
获取状态
:return:
"""
dic = self.get_info() # type:dict
if not dic:
return dic
for i in dic: # 遍历dict
ip = i.split(":")[0]
if dic[i].get('cluster_state'): # 获取状态
print("节点状态, ip: ", ip, "value: ", dic[i].get('cluster_state'))
def get_has_aof(self):
"""
查看aof是否打开
:return:
"""
res = RedisCluster(self.conn_list).connect()
# print("连接集群对象",res,type(res),res.__dict__)
if not res:
return False
dic = res.config_get('appendonly') # 从config配置项中查询appendonly
for i in dic:
ip = i.split(":")[0]
# print(dic[i])
if dic[i].get('appendonly'):
print("aof开关, ip: ", ip,"value: ",dic[i].get('appendonly'))
redis_basis_conn = [{'host': '192.168.10.168', 'port': 7201}, {'host': '192.168.10.169', 'port': 7201}, {'host': '192.168.10.170', 'port': 7201}, {'host': '192.168.10.171', 'port': 7201}, {'host': '192.168.10.142', 'port': 7201}, {'host': '192.168.10.143', 'port': 7201}]
RedisCluster(redis_basis_conn).get_has_aof()
执行输出:
aof开关, ip: 192.168.10.170 value: no
aof开关, ip: 192.168.10.168 value: no
aof开关, ip: 192.168.10.142 value: no
aof开关, ip: 192.168.10.171 value: no
aof开关, ip: 192.168.10.169 value: no
aof开关, ip: 192.168.10.143 value: no
set和get
#!/usr/bin/env python
# coding: utf-8
from rediscluster import StrictRedisCluster
class RedisCluster(object): # 连接redis集群
def __init__(self,conn_list):
self.conn_list = conn_list # 连接列表
def connect(self):
"""
连接redis集群
:return: object
"""
try:
# 非密码连接redis集群
# redisconn = StrictRedisCluster(startup_nodes=self.conn_list)
# 使用密码连接redis集群
redisconn = StrictRedisCluster(startup_nodes=self.conn_list, password='123456')
return redisconn
except Exception as e:
print(e)
print("错误,连接redis 集群失败")
return False
# 连接列表,注意:必须严格按照此格式来!
redis_basis_conn = [{'host': '192.168.10.168', 'port': 7201}, {'host': '192.168.10.169', 'port': 7201}, {'host': '192.168.10.170', 'port': 7201}, {'host': '192.168.10.171', 'port': 7201}, {'host': '192.168.10.142', 'port': 7201}, {'host': '192.168.10.143', 'port': 7201}]
redis_conn = RedisCluster(redis_basis_conn).connect() # redis连接对象
redis_conn.set('name','weijing') # 插入一个值
print("name is: ", redis_conn.get('name')) # 查询值
执行输出:
name is: b'weijing'
python操作redis集群_python 连接管理作redis集群相关推荐
- python操作hive数据库代码_Python连接Hive操作数据库
前言 客户端连接Hive需要使用HiveServer2.HiveServer2是HiveServer的重写版本,HiveServer不支持多个客户端的并发请求.当前HiveServer2是基于Thri ...
- python操作mysql事务提交_python连接mysql并提交mysql事务示例
代码如下: # -*- coding: utf-8 -*- import sys import MySQLdb reload(sys) sys.setdefaultencoding('utf-8') ...
- python redis 集群_python 连接redis集群 ,常见报错解决。
背景:工作需要,处理的数据需要通过redis进行缓存处理,之后方便统计分析. 目标:python连接redis进行读取&写入. 连接 redis 与 redis集群 是不同的 !!! 一.连接 ...
- python连接redis有中文_Python连接Redis并操作
首先开启redis的外连 sch01ar@ubuntu:~$ sudo vim /etc/redis/redis.conf 把bind 127.0.0.1这行注释掉 然后重启redis sudo /e ...
- python连数据库课程设计_python 连接操作 各类数据库
摘要: 用Python写脚本也有一段时间了,经常操作数据库(MySQL),现在就整理下对各类数据库的操作,如后面有新的参数会补进来,慢慢完善. 一,python 操作 MySQL:详情见:这里 mac ...
- 故障转移集群无法连接到节点_Redis集群以及自动故障转移测试
在Redis中,与Sentinel(哨兵)实现的高可用相比,集群(cluster)更多的是强调数据的分片或者是节点的伸缩性,如果在集群的主节点上加入对应的从节点,集群还可以自动故障转移,因此相比Sen ...
- python操作mysql事务提交_python关于Mysql操作
一.安装mysql windows下,直接下载mysql安装文件,双击安装文件下一步进行操作即可, Linux下的安装也很简单,除了下载安装包进行安装外,一般的linux仓库中都会有mysql ,我们 ...
- python操作文件和目录_Python操作文件和目录
Python操作文件和目录 读写文件比较简单,有一点特别注意就好了 windows下Python默认打开的文件以gbk解码,而一般我们的文件是utf-8编码的,所以如果文本含有中文,就会出现异常或者乱 ...
- python抓取qq群消息_Python获取统计自己的qq群成员信息的方法
首先说明一下需要使用的工具以及技术:python3 + selenium selenium安装方法:pip install selenium 前提:获取自己的qq群成员信息,自己必须是群主或者管理员, ...
最新文章
- C 判断输入的字符是什么
- python之functools partial
- gcc动态链接库基本知识
- GraphPad Prism:如何在轴上放置一个或多个缺口?
- 8086汇编-实验4-[bx]和loop的使用
- 为什么别人有微粒贷,而你没有?
- matlab 刻度间隔,matlab – 地图的主要和次要刻度?
- apktool反编译生成java_apktool反编译工具下载|apktool反编译工具 v3.0.1 最新版-520下载站...
- Ubuntu使用记录:安装deb软件方法以及apt、apt-get和dpkg的区别
- BP神经网络与RBF神经网络matlab代码实现
- 阿里巴巴CRM库问题
- linux系统时间与网络时间不同步
- mysql服务什么意思_mysql数据库服务是什么意思
- R语言编写用户自定义函数:R语言编写用户自定义函数计算变异系数(coefficient of variation)、输入为向量
- smb1文件共享不安全不能连接文件共享
- 谷歌表格图表 横坐标 滚动_如何使用Google表格的“探索”功能构建即时图表
- 女人拉屎故事_一个敏锐的女性下午的故事
- opencv 稀疏光流 稠密光流
- 【Python】np.nonzero()函数
- fedora dnf 指定安装目录
热门文章
- C++查看各种数据类型所占字节和最大最小值(数据范围)
- Opencv中Mat的data数据只定义为uchar*类型,
- 《 FRIDA系列文章 》
- springcloud 创建子父项目_idea搭建springCloud----搭建父子项目(二)
- Modbus协议栈应用实例之三:Modbus TCP客户端应用
- 矩阵 II : 线性组的线性相关性
- 现代软件工程 10 绩效管理
- 二十年后我发明了保姆机器人作文_机器人保姆我的发明作文450字
- python猜数字游戏续_python3实现猜数字游戏
- amd核芯显卡控制面板自定义分辨率_AMD翻盘,NVIDIA翻车,你的下一张光追显卡选哪个...