redis部署及使用

一、redis安装
1、redis下载地址

wget http://download.redis.io/releases/redis-3.2.6.tar.gz
tar xf redis-3.2.6.tar.gz
cd redis-3.2.6
make
cd /home/tools
mkdir -p redis6380                                                       #创建redis的工作目录
cp /home/tools/redis-3.2.6/src/redis-server /home/tools/redis6380/       #复制启动文件到redis6380目录
cp /home/tools/redis-3.2.6/redis.conf /home/tools/redis6380/             #复制配置文件到redis6380目录

2、修改配置文件

egrep -v "^$|^#" redis.conf.bak >>redis.conf     #将配置文件的空行和以#号开头的行去掉
cat /home/tools/redis6380/redis.conf
bind 127.0.0.1
protected-mode yes
#requirepass root                      #用户redis管理的认证用户
port 6380                              #修改redis服务的端口
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_6380.pid"      #修改pid文件名
loglevel notice 
logfile "redis6380.log"                #修改日志文件名
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/home/tools/redis6380"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
#masterauth root                 #主从复制用于认证的用户,也就是redis管理认证的用户
slaveof 127.0.0.1 6382           #设置从库

3、启动redis服务

[root@redis redis6380]# pwd
/home/tools/redis6380
[root@redis redis6380]# ./redis-server ./redis.conf

4、检查6380端口是否存在

[root@redis redis6380]# ss -tlunp|grep 6380
tcp    LISTEN     0      128            127.0.0.1:6380                  *:*      users:(("redis-server",5382,4))

5、连接redis

[root@redis tools]# /home/tools/redis-3.2.6/src/redis-cli -p 6380
127.0.0.1:6380>

6、添加软连接,方便管理

ln -s /home/tools/redis-3.2.6/src/redis-cli /usr/bin/redis-cli
[root@redis tools]# redis-cli -p 6380
127.0.0.1:6380> auth root      #认证,root为配置文件中设置的密码
OK

7、redis简单使用
7.1、redis事务

127.0.0.1:6380> zadd salary 200 ljx
(integer) 1
127.0.0.1:6380> zadd salary 3000 oldboy
(integer) 1
127.0.0.1:6380> multi
OK
127.0.0.1:6380> zincrby salary 1000 ljx
QUEUED
127.0.0.1:6380> zincrby salary -1000 oldboy
QUEUED
127.0.0.1:6380> exec
1) "1200"
2) "2000"

7.2、慢日志查询

127.0.0.1:6380> config set slowlog-log-slower-than 1000
OK
127.0.0.1:6380> config set slowlog-max-len 1000
OK
127.0.0.1:6380> config get slow*
1) "slowlog-log-slower-than"
2) "1000"
3) "slowlog-max-len"
4) "1000"

7.3、备份数据

[root@redis redis6380]# mv dump.rdb /root/
[root@redis redis6380]# ./redis-server ./redis.conf
[root@redis redis6380]# redis-cli -p 6380
127.0.0.1:6380> auth root
OK
127.0.0.1:6380> keys *
(empty list or set)
127.0.0.1:6380> shutdown
not connected> exit
[root@redis redis6380]# mv /root/dump.rdb .
mv: overwrite `./dump.rdb'? y
[root@redis redis6380]# ./redis-server ./redis.conf
[root@redis redis6380]# redis-cli -p 6380
127.0.0.1:6380> auth root
OK
127.0.0.1:6380> keys *
1) "salary"
2) "set2"
3) "name"
4) "user1"
5) "java"
6) "set1"
7) "zhang3"
127.0.0.1:6380> 

7.4、订阅
7.4.1、客户端操作

127.0.0.1:6380> subscribe publish1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "publish1"
3) (integer) 1
1) "message"
2) "publish1"
3) "this is publish1 channel"
1) "message"
2) "publish1"
3) "this is publish2 channel"

7.4.2、服务端操作

127.0.0.1:6380> publish publish1 "this is publish1 channel"
(integer) 2
127.0.0.1:6380> publish publish1 "this is publish2 channel"
(integer) 2

7.5、将从库变为主库

slaveof no one

7.6、字符串键值

127.0.0.1:6380> set name 12345   设定一个值
OK
127.0.0.1:6380> get name         查看
"12345"
127.0.0.1:6380> type name        查看类型
string
127.0.0.1:6380> expire name 30   设置生存周期时间为30秒(时间可以任意设置)
(integer) 1
127.0.0.1:6380> ttl name         查看生存周期剩余的时间,当生存周期剩余的时间为负数的时候,值就会消失
(integer) 16

127.0.0.1:6380> set name ljx     设定一个值
OK
127.0.0.1:6380> get name         查看设定的值
"ljx"
127.0.0.1:6380> append name -beijing  给原先的值追加内容
(integer) 11
127.0.0.1:6380> get name          查看
"ljx-beijing"

127.0.0.1:6380> del name key     删除值
(integer) 2
127.0.0.1:6380> keys *           查看所有的值
(empty list or set)

7.7、hash键值

127.0.0.1:6380> hset user1 name ljx     #设置hash键值
(integer) 1
127.0.0.1:6380> type user1              #查看类型
hash
127.0.0.1:6380> hget user1 name         #查看值
"ljx"

127.0.0.1:6380> hset user1 age 24       #添加一个值
(integer) 1
127.0.0.1:6380> hgetall user1           #查看所有的hash值
1) "name"
2) "ljx"
3) "age"
4) "24"
127.0.0.1:6380> hget user1 age          #查看age对应的值
"24"

7.8、创建集合类型的值

127.0.0.1:6380> sadd set1 "liujx" 123 "zhan3" "li4"
(integer) 4
127.0.0.1:6380> type set1
set

127.0.0.1:6380> smembers set1           #查看集合的值
1) "123"
2) "liujx"
3) "li4"
4) "zhan3"

127.0.0.1:6380> sadd set2 "liujx" 456 "wang5"     #第二个集合
(integer) 3
127.0.0.1:6380> smembers set2                     #查看集合
1) "wang5"
2) "liujx"
3) "456"
127.0.0.1:6380> sdiff set1 set2                   #显示集合1和集合2的差集
1) "123"
2) "zhan3"
3) "li4"

127.0.0.1:6380> sunion set1 set2                  #取两个集合的并集
1) "zhan3"
2) "li4"
3) "123"
4) "wang5"
5) "liujx"
6) "456"

二、python连接redis
1、安装依赖包

yum -y install python-pip  (需要epel源)
pip install redis
python  (登录python)
>>> import redis
>>> r = redis.StrictRedis(host='127.0.0.1',port=6382,db=0)   (连接redis)
>>> r.get('name')    #查看redis中的数据

2、设置一个键值

>>> r.set("python_test","python redis test case by ljx")
True
>>> r.get ("python_test")          #查看
'python redis test case by ljx'

3、redis服务端查看

127.0.0.1:6382> get python_test
"python redis test case by ljx"

你可能感兴趣的:(部署)