代码改变世界

redis 基础

2019-06-10 17:40  brookin  阅读(247)  评论(0编辑  收藏  举报

源码编译安装

1. 下载,解压,进入源码目录,指定安装目录,编译安装

tar xzf redis-5.0.5.tar.gz
cd redis-5.0.5
make PREFIX=/home/user_00/services/redis-5.0.5 install

2. 配置文件复制

mkdir /home/user_00/services/redis-5.0.5/conf
cp redis.conf /home/user_00/services/redis-5.0.5/conf

3. 修改配置

daemonize yes 		# 设为守护进程
requirepass foobared      # 设置密码
bind 127.0.0.1			# 修改为内网 ip
port 6379				# 修改端口
dir ./data 				# 指定 db 文件的路径
loglevel verbose             # 设置日志级别,默认 notice
logfile stdout                   # 设置日志文件的输出方式,如果以守护进程的方式运行redis 默认:"" , 并且日志输出设置为stdout,那么日志信息就输出到/dev/null里面去了 

启动,关闭

./bin/redis-server redis.conf
./bin/redis-cli shutdown

客户端连接

./redis-cli -h 10.33.90.2 -p 6379
10.33.90.2:6379> auth mypassword     // 密码替换 mypassword

./redis-cli -h 10.33.90.2 -p 6379 -a mypassword

批量插入

shell 操作

You could use redis-cli in pipe mode, first you prepare a file like (note that the lines should be terminated by cr/lf or set by -d option):

SET Key0 Value0
SET Key1 Value1
...
SET KeyN ValueN

then pipe it to redis-cli: https://redis.io/topics/mass-insert

cat data.txt | redis-cli --pipe

python 操作

rs = redis.StrictRedis(host=my_host, port=my_port, db=0, password=my_password)
pipe = rs.pipeline()
with open(filename) as fp:
	for row in fp:
		imei_md5, bid_middle, bid_average = row.strip().split(",")
		key = prefix + imei_md5
		val = {"middle": bid_middle, "average": bid_average}
		value = json.dumps(val)
		pipe.set(key, value)
pipe.execute()