Python操作Redis
Python2.7.9+或Python3.4+版本,内置了pip
安装pip
# unzip setuptools-38.5.1.zip
# cd setuptools-38.5.1
# python setup.py install
# tar -zxvf pip-9.0.1.tar.gz
# cd pip-9.0.1
# python setup.py install
# unzip setuptools-38.5.1.zip
# cd setuptools-38.5.1
# python setup.py install
# tar -zxvf pip-9.0.1.tar.gz
# cd pip-9.0.1
# python setup.py install
pyenv的安装
# mkdir .pyenv
# git clone https://github.com/yyuu/pyenv.git ~/.pyenv
# echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
# echo 'export PATH="$PYENV_ROOT/bin/:$PATH"' >> ~/.bash_profile
初始化
# echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
# source .bash_profile
使用pyenv安装不同的Python版本
# pyenv install --list
# pyenv install -v 2.7.13
# pyenv install -v 3.6.8
# pyenv versions
# pyenv global 2.7.13
# pyenv global 3.6.8
# mkdir .pyenv
# git clone https://github.com/yyuu/pyenv.git ~/.pyenv
# echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
# echo 'export PATH="$PYENV_ROOT/bin/:$PATH"' >> ~/.bash_profile
初始化
# echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
# source .bash_profile
使用pyenv安装不同的Python版本
# pyenv install --list
# pyenv install -v 2.7.13
# pyenv install -v 3.6.8
# pyenv versions
# pyenv global 2.7.13
# pyenv global 3.6.8
# pip install redis
第一种方式
修改redis配置文件,找到
#requirepass foobared
去掉注释,并修改为所需要的密码
重启redis服务
第二种方式
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
127.0.0.1:6379> config set requirepass "abc123"
OK
redis-cli -h 127.0.0.1 -p 6379 -a "abc123"
普通连接
import redis
import random
import string
client = redis.StrictRedis(host='192.168.1.101',port=6379,db=0,password='mypass')
key = "hello"
setResult = client.set(key,"python-redis")
print(setResult)
value = client.get(key)
print("key:",key)
print("value:",value)
i = 0
import redis
import random
import string
client = redis.StrictRedis(host='192.168.1.101',port=6379,db=0,password='mypass')
key = "hello"
setResult = client.set(key,"python-redis")
print(setResult)
value = client.get(key)
print("key:",key)
print("value:",value)
i = 0
while True:
#1.string
r_name = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(random.randint(10,30)))
client.set("name_%d" %i,r_name)
print(client.get("name_%d" %i))
print(client.incr("counter"))
#1.string
r_name = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(random.randint(10,30)))
client.set("name_%d" %i,r_name)
print(client.get("name_%d" %i))
print(client.incr("counter"))
#2.hash
client.hset("myhash","f1","v1")
client.hset("myhash","f2","v2")
print(client.hgetall("myhash"))
#3.list
client.rpush("mylist","1")
client.rpush("mylist","2")
client.rpush("mylist","3")
print(client.lrange("mylist",0,-1))
#4.set
client.sadd("myset","a")
client.sadd("myset","b")
print(client.smembers("myset"))
#5.zset
mapping = {
"tom":99,
"peter":33,
"allen":66,
}
client.zadd("myzset",mapping)
print(client.zrange("myzset",0,-1,withscores=True))
i = i + 1
print("i = ",i)
client.hset("myhash","f1","v1")
client.hset("myhash","f2","v2")
print(client.hgetall("myhash"))
#3.list
client.rpush("mylist","1")
client.rpush("mylist","2")
client.rpush("mylist","3")
print(client.lrange("mylist",0,-1))
#4.set
client.sadd("myset","a")
client.sadd("myset","b")
print(client.smembers("myset"))
#5.zset
mapping = {
"tom":99,
"peter":33,
"allen":66,
}
client.zadd("myzset",mapping)
print(client.zrange("myzset",0,-1,withscores=True))
i = i + 1
print("i = ",i)
sentinel方式
import redis
from redis.sentinel import Sentinel
import redis
from redis.sentinel import Sentinel
# 连接哨兵服务器(主机名也可以用域名)
sentinel = Sentinel([('192.168.1.101', 26379),('192.168.1.102', 26379),('192.168.1.103', 26379)],socket_timeout=0.5)
sentinel = Sentinel([('192.168.1.101', 26379),('192.168.1.102', 26379),('192.168.1.103', 26379)],socket_timeout=0.5)
# 获取主服务器地址
master = sentinel.discover_master('mymaster')
print(master)
master = sentinel.discover_master('mymaster')
print(master)
# 获取从服务器地址
slave = sentinel.discover_slaves('mymaster')
print(slave)
slave = sentinel.discover_slaves('mymaster')
print(slave)
# 获取主服务器进行写入
master = sentinel.master_for('mymaster', socket_timeout=0.5, password='mypass', db=0)
w_ret = master.set('foo', 'bar')
master = sentinel.master_for('mymaster', socket_timeout=0.5, password='mypass', db=0)
w_ret = master.set('foo', 'bar')
# # 获取从服务器进行读取(默认是round-roubin)
slave = sentinel.slave_for('mymaster', socket_timeout=0.5, password='mypass', db=0)
r_ret = slave.get('foo')
print(r_ret)
slave = sentinel.slave_for('mymaster', socket_timeout=0.5, password='mypass', db=0)
r_ret = slave.get('foo')
print(r_ret)
redis cluster方式
# pip install redis-py-cluster
from rediscluster import StrictRedisCluster
import datetime
import sys
def redis_cluster():
redis_nodes = [{'host':'192.168.1.101','port':6379},
{'host':'192.168.1.102','port':6379},
{'host':'192.168.1.103','port':6379},
{'host':'192.168.1.101','port':6380},
{'host':'192.168.1.102','port':6380},
{'host':'192.168.1.103','port':6380}
]
try:
redisconn = StrictRedisCluster(startup_nodes=redis_nodes)
return redisconn
except Exception:
print('Connect Error!')
sys.exit(1)
if __name__ == '__main__':
redisconn = redis_cluster()
redisconn.set('name','admin')
redisconn.set('age',18)
print("name is:",redisconn.get('name'))
print(redisconn.get('name'),redisconn.get('age'))
redisconn = redis_cluster()
redisconn.set('name','admin')
redisconn.set('age',18)
print("name is:",redisconn.get('name'))
print(redisconn.get('name'),redisconn.get('age'))