redis笔记之多API支持

通过python连接
环境:
[root@db01 ~]# cat /etc/centos-release
CentOS Linux release 7.5.1804 (Core) 
[root@db01 ~]# python3 -V
Python 3.6.1

自带python版本为2.7,有点低,需要先升级成3
我这选的是3.6.1

redis环境:
[root@db01 tools]# ps -ef|grep redis
root      11193      1  0 12:10 ?        00:00:39 redis-server *:6381
root      11197      1  0 12:10 ?        00:00:37 redis-server *:6382
root      11287      1  0 12:59 ?        00:01:02 redis-sentinel *:26380 [sentinel]
root      11303      1  0 13:09 ?        00:00:32 redis-server *:6380
root      11325      1  0 13:21 ?        00:00:58 redis-sentinel *:26381 [sentinel]
root      11418      1  0 13:58 ?        00:00:29 redis-server *:7000 [cluster]
root      11420      1  0 13:58 ?        00:00:30 redis-server *:7001 [cluster]
root      11422      1  0 13:58 ?        00:00:30 redis-server *:7002 [cluster]
root      11424      1  0 13:58 ?        00:00:23 redis-server *:7003 [cluster]
root      11430      1  0 13:58 ?        00:00:23 redis-server *:7005 [cluster]
root      11434      1  0 13:58 ?        00:00:33 redis-server *:7006 [cluster]
root      11441      1  0 13:58 ?        00:00:22 redis-server *:7007 [cluster]
root      11660      1  0 15:17 ?        00:00:18 redis-server *:7004 [cluster]
root      25871      1  0 17:22 ?        00:00:00 redis-server 10.0.0.51:6379
升级python2到3
[root@db01 tools]# tar xf Python-3.6.1.tar.xz 
[root@db01 tools]# cd Python-3.6.1/
[root@db01 Python-3.6.1]# ./configure
[root@db01 Python-3.6.1]# make && make install
[root@db01 Python-3.6.1]# python3 -V
Python 3.6.1

PS:使用3的时候直接python3就好,最好不要改动其他

安装redis驱动:
unzip redis-py-master.zip
cd redis-py-master
python3 setup.py install
安装redis-cluser的客户端程序
unzip redis-py-cluster-unstable.zip
cd redis-py-cluster-unstable
python3 setup.py install

对单实例测试

[root@db01 tools]# python3
Python 3.6.1 (default, Nov 30 2018, 17:08:38) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> r = redis.StrictRedis(host='localhost', port=6379, db=0,password='123')
>>> r.set('foo', 'bar')
True
>>> r.get('foo')
b'bar'

对sentinel集群连接测试

## 导入redis sentinel包
>>>from redis.sentinel import Sentinel  
##指定sentinel的地址和端口号
>>> sentinel = Sentinel([('localhost', 26380)], socket_timeout=0.1)  
##测试,获取以下主库和从库的信息
>>> sentinel.discover_master('mymaster')  
>>> sentinel.discover_slaves('mymaster')  

##配置读写分离
#写节点
>>> master = sentinel.master_for('mymaster', socket_timeout=0.1,password="123")  
#读节点
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1,password="123")  
###读写分离测试   key     
>>> master.set('hi', '123')  
>>> slave.get('hi')  
'123'

对rediscluster集群测试

[root@db01 tools]# python3
Python 3.6.1 (default, Nov 30 2018, 17:08:38) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from rediscluster import StrictRedisCluster
>>> startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]
>>> rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
>>> rc.set("foo", "bar")
True
>>> print(rc.get("foo"))
bar
>>> quit()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值