Linux下python3的安装以及redis的使用
python3的安装
上传Python-3.5.2.tar.xz软件到 /server/tools 中
解压 :
tar xf Python-3.5.2.tar.xz
编译安装
cd Python-3.5.2
./configure
make
make install
--------------------------
安装redis for python驱动
cd /server/tools
unzip redis-py-master.zip
cd redis-py-master
python3 setup.py install
安装集群驱动
cd /server/tools
unzip redis-py-cluster-unstable.zip
cd redis-py-cluster-unstable
python3 setup.py install
------------------------------------------
1、python3 连接单机redis:
import redis
r = redis.StrictRedis(host='127.0.0.1', port=6379,password='123')
r.get("name")
r.set("name","123")
------------------------------------------------------------
----
sentinel模式:
>>> from redis.sentinel import Sentinel
>>> sentinel = Sentinel([('127.0.0.1', 26380)], socket_timeout=0.1)
>>> sentinel.discover_master('mymaster')
>>> sentinel.discover_slaves('mymaster')
配置读写分离:
>>> master = sentinel.master_for('mymaster', socket_timeout=0.1)
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)
>>> master.set('foo', 'bar')
>>> slave.get('foo')
------------------------------------------------------------------
cluster模式
------
(1) redis-py并没有提供redis-cluster的支持,去github找了一下,有个叫redis-py-cluster的源码,
但是和redis-py不是一个作者,地址为:https://github.com/Grokzen/redis-py-cluster
watch,star,fork还算可以。
----
(2) 安装
Latest stable release from pypi
$ pip install redis-py-cluster
or from source
$ python setup.py install
---
(3) 使用
>>> 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'
=-------------------------------------------
安装redis cluster
EPEL源安装ruby支持
yum install ruby rubygems -y
使用国内源
gem sources -a http://mirrors.aliyun.com/rubygems/
gem sources --remove http://rubygems.org/
gem install redis -v 3.3.3
-------------------
准备集群环境:
2、集群节点准备
mkdir /nosql/700{0..5}
vim /nosql/7000/redis.conf
port 7000
daemonize yes
pidfile /nosql/7000/redis.pid
loglevel notice
logfile "/nosql/7000/redis.log"
dbfilename dump.rdb
dir /nosql/7000
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /nosql/7001/redis.conf
port 7001
daemonize yes
pidfile /nosql/7001/redis.pid
loglevel notice
logfile "/nosql/7001/redis.log"
dbfilename dump.rdb
dir /nosql/7001
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /nosql/7002/redis.conf
port 7002
daemonize yes
pidfile /nosql/7002/redis.pid
loglevel notice
logfile "/nosql/7002/redis.log"
dbfilename dump.rdb
dir /nosql/7002
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /nosql/7003/redis.conf
port 7003
daemonize yes
pidfile /nosql/7003/redis.pid
loglevel notice
logfile "/nosql/7003/redis.log"
dbfilename dump.rdb
dir /nosql/7003
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /nosql/7004/redis.conf
port 7004
daemonize yes
pidfile /nosql/7004/redis.pid
loglevel notice
logfile "/nosql/7004/redis.log"
dbfilename dump.rdb
dir /nosql/7004
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
vim /nosql/7005/redis.conf
port 7005
daemonize yes
pidfile /nosql/7005/redis.pid
loglevel notice
logfile "/nosql/7005/redis.log"
dbfilename dump.rdb
dir /nosql/7005
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
启动节点:
redis-server /nosql/7000/redis.conf
redis-server /nosql/7001/redis.conf
redis-server /nosql/7002/redis.conf
redis-server /nosql/7003/redis.conf
redis-server /nosql/7004/redis.conf
redis-server /nosql/7005/redis.conf
[root@db01 ~]# ps -ef |grep redis
root 8854 1 0 03:56 ? 00:00:00 redis-server *:7000 [cluster]
root 8858 1 0 03:56 ? 00:00:00 redis-server *:7001 [cluster]
root 8860 1 0 03:56 ? 00:00:00 redis-server *:7002 [cluster]
root 8864 1 0 03:56 ? 00:00:00 redis-server *:7003 [cluster]
root 8866 1 0 03:56 ? 00:00:00 redis-server *:7004 [cluster]
root 8874 1 0 03:56 ? 00:00:00 redis-server *:7005 [cluster]
3、将节点加入集群管理
redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \
127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
4、集群状态查看
集群主节点状态
redis-cli -p 7000 cluster nodes | grep master
集群从节点状态
redis-cli -p 7000 cluster nodes | grep slave
作者:赵盼盼
出处:https://www.cnblogs.com/zhaopanpan/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
⇩ 关注或点个喜欢就行 ^_^
关注我