redis批量删除指定pattern的keys
redis-cli keys "*" | xargs redis-cli del
# 如果redis-cli没有设置成系统变量,需要指定redis-cli的完整路径
# 如:/opt/redis/redis-cli keys "*" | xargs /opt/redis/redis-cli del
redis-cli -a password keys "*" | xargs redis-cli -a password del
# 下面的命令指定数据序号为0,即默认数据库
redis-cli -n 0 keys "*" | xargs redis-cli -n 0 del
# 删除当前数据库中的所有Key
flushdb
# 删除所有数据库中的key
flushall
python使用redis文档
http://redis-py.readthedocs.io/en/latest/
清除redis缓存数据sentinel
from redis import sentinel
cli = sentinel.Sentinel([["192.168.229.169", 29000], ["192.168.229.169", 29001], ["192.168.229.169", 29002],["192.168.229.169",29003]])
ms = cli.master_for("cluster_169-20000")
ms.delete("webcc:miccard_uids_live_key")
ms.delete("webcc:game_uids_live_key")
redis 同库迁移
# 命令说明
-h hostip,
-p 端口,
-a 密码,
-n db库,
--no-raw选项是要求命令的返回结果必须是原始的格式,
--raw恰恰相反,返回格式化后的结果,
-x 选项代表从标准输入(stdin)读取数据
perl -p(perl 命令可自行百度) 使 Perl 隐式地循环遍历指定的文件,同时打印所有的行。自动循环+自动输出,相当于 while(<>) { 脚本; print; }
# 复制全部一个库的key值
#!/bin/bash
src_ip=127.0.0.1
src_port=8090
src_db=14
src_pw='1234'
dest_ip=127.0.0.1
dest_port=8090
dest_db=15
desc_pw='1234'
redis-cli -h $src_ip -p $src_port -a $src_pw -n $src_db keys "*" | while read key
do
redis-cli -h $src_ip -p $src_port -a $src_pw -n $src_db --raw dump $key | perl -pe 'chomp if eof' | redis-cli -h $dest_ip -p $dest_port -a $desc_pw -n $dest_db -x restore $key 0
echo "migrate key $key"
done
#复制一个库的部分key
#!/bin/bash
src_ip=127.0.0.1
src_port=8090
src_db=14
src_pw='1234'
dest_ip=127.0.0.1
dest_port=6379
dest_db=2
desc_pw='1234'
#要遍历的key
k=(test ws we)
for loop in ${k[*]}
do
redis-cli -h $src_ip -p $src_port -a $src_pw -n $src_db --raw dump $loop | perl -pe 'chomp if eof' | redis-cli -h $dest_ip -p $dest_port -a $desc_pw -n $dest_db -x restore $loop 0
echo "The value is: $loop"
done
#只复制一个key
redis-cli -h 127.0.0.1 -p 8090 -a 1234 -n 14 --raw dump test | perl -pe 'chomp if eof' | redis-cli -h 127.0.0.1 -p 8090 -a 1234 -n 15 -x restore test 0
redis 迁移python实现
# coding=utf-8
import redis
redis_from = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
redis_to = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
if __name__ == '__main__':
cnt = 0
for k in redis_from.keys():
data_type = redis_from.type(k)
if data_type == 'string':
v = redis_from.get(k)
redis_to.set(k, v)
elif data_type == 'list':
values = redis_from.lrange(k, 0, -1)
redis_to.lpush(k, values)
elif data_type == 'set':
values = redis_from.smembers(k)
redis_to.sadd(k, values)
elif data_type == 'hash':
keys = redis_from.hkeys(k)
for key in keys:
value = redis_from.hget(k, key)
redis_to.hset(k, key, value)
else:
print 'not known type'
cnt = cnt + 1
print 'total', cnt