tar xf Python-3.5.2.tar.xz
cd Python-3.5.2
./configure
make && make install
https://redis.io/clients
下載redis-py-master.zip
安裝驅(qū)動:
unzip redis-py-master.zip
cd redis-py-master
python3 setup.py install
安裝redis-cluser的客戶端程序
cd redis-py-cluster-unstable
python3 setup.py install
1. 對redis的單實例進行連接操作
python3
>>>import redis
>>>r = redis.StrictRedis(host='10.0.0.200', port=6379, db=0,password='123')
>>>r.set('foo', 'bar')
True
>>>r.get('foo')
'bar'
2. sentinel集群連接并操作
redis-sentinel集群: http://www.reibang.com/p/a6540539eaa8
[root@db01 ~]# redis-server /data/6380/redis.conf
[root@db01 ~]# redis-server /data/6381/redis.conf
[root@db01 ~]# redis-server /data/6382/redis.conf
[root@db01 ~]# redis-sentinel /data/26380/sentinel.conf &
導(dǎo)入redis sentinel包
>>>from redis.sentinel import Sentinel
sentinel的地址和端口號
>>> sentinel = Sentinel([('localhost', 26380)], socket_timeout=0.1)
測試,獲取以下主庫和從庫的信息
>>> sentinel.discover_master('mymaster')
>>> sentinel.discover_slaves('mymaster')
配置讀寫分離
寫節(jié)點
>>> master = sentinel.master_for('mymaster', socket_timeout=0.1,password="123")
讀節(jié)點
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1,password="123")
讀寫分離測試
>>> master.set('oldboy', '123')
>>> slave.get('oldboy')
'123'
----------------------
redis cluster的連接并操作(python2.7.2以上版本才支持redis cluster闰渔,我們選擇的是3.5)
https://github.com/Grokzen/redis-py-cluster
3. python連接redis cluster集群測試
redis-cluster集群: http://www.reibang.com/p/e3e5fba594ed
python3
>>> from rediscluster import StrictRedisCluster
>>> startup_nodes = [{"host": "127.0.0.1", "port": "7000"},{"host": "127.0.0.1", "port": "7001"},{"host": "127.0.0.1", "port": "7002"}]
### Note: decode_responses must be set to True when used with python3
>>> rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
>>> rc.set("foo0000", "bar0000")
True
>>> print(rc.get("foo0000"))
'bar'