单个连接(以连接池连接)
- 需要的基础库:
import redis
pool=redis.ConnectionPool(host=ip,port=port,db=db)
cli=redis.StrictRedis(connection_pool=pool)
或
cli = redis.Redis(host='ip', port=port, db=0)
以哨兵形式连接:
- 需要的基础库:
from redis.sentinel import Sentinel
sentinel = Sentinel([(ip[0],port[0]),(ip[1],port[1]),(ip[2],port[2])])
cli= sentinel.master_for(service_name=service_name,db=db)
以集群形式连接
安装基础库: pip install redis-py-cluster
- 需要的基础库:
from rediscluster import RedisCluster
node = [
'host1:port1',
'host1:port1',
'host1:port1',
'host1:port1',
'host1:port1',
'host1:port1']
# print(node)
startup_nodes=[{'host':node_info.split(':')[0],'port':node_info.split(':')[1]} for node_info in node]
cli = RedisCluster(startup_nodes=startup_nodes,password='password',decode_responses=True)
官网地址: https://pypi.org/project/redis-py-cluster/
验证连接成功
print cli.ping()
关于断开连接
python在连接时,会有一定的时限。故不需要进行断开连接的操作。
查看redis连接情况
使用命令: CLIENT LIST
Redis连接方式详解
2978

被折叠的 条评论
为什么被折叠?



