概述
安装
先把安装包放到/opt目录下
[root@localhost opt]# tar zxvf redis-5.0.7.tar.gz
[root@localhost opt]# cd redis-5.0.7/
[root@localhost redis-5.0.7]# make
[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis install
[root@localhost redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@localhost redis-5.0.7]# cd utils/
[root@localhost utils]# ./install_server.sh ##一路回车就行
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
Please select the redis port for this instance: [6379] ##默认端口
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] ##配置文件
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] ##日志文件
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] ##数据存放目录
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] ##执行文件路径
命令
[root@localhost utils]# /etc/init.d/redis_6379 status ##查看状态
Redis is running (45987)
[root@localhost utils]# /etc/init.d/redis_6379 stop ##停止
Stopping ...
Redis stopped
[root@localhost utils]# /etc/init.d/redis_6379 start ##开启
Starting Redis server...
[root@localhost utils]# /etc/init.d/redis_6379 restart ##重启
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost utils]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> help set
SET key value [expiration EX seconds|PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
group: string
127.0.0.1:6379> help @list
BLPOP key [key ...] timeout
summary: Remove and get the first element in a list, or block until one is available
since: 2.0.0
.....//省略
string(字符串类型)
- set 新增
- get 查询
127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> get name
"zhangsan"
127.0.0.1:6379> type name
string
hash(哈希类型)
- hmset 新增
- hget 查询
127.0.0.1:6379> hmset offo name hr age 34 sex 男
OK
127.0.0.1:6379> hget offo name
"hr"
127.0.0.1:6379> hget offo age
"34"
127.0.0.1:6379> hget offo sex
"\xe7\x94\xb7"
这边我们看到中文字符显示的格式不正确
所以我们要重新进入redis添加 --raw 让他识别
[root@localhost utils]# redis-cli -h 127.0.0.1 -p 6379 --raw
127.0.0.1:6379> hget offo sex
男
127.0.0.1:6379> type offo
hash
list(列表类型)
- lpush 添加
- lrange 查看
127.0.0.1:6379> lpush hobby play
1
127.0.0.1:6379> lpush hobby read
2
127.0.0.1:6379> lpush hobby sport
3
127.0.0.1:6379> lrange hobby 0 0
sport
127.0.0.1:6379> lrange hobby 1 1
read
127.0.0.1:6379> lrange hobby 2 2
play
127.0.0.1:6379> type hobby
list
set(集合类型)
- sadd 新增
- smembers 查询
127.0.0.1:6379> sadd colour red green yellow black
4
127.0.0.1:6379> smembers colour
black
yellow
green
red
127.0.0.1:6379> type colour
set
zset(有序集合类型)
- zadd 新增
- zrangebyscore 查询
127.0.0.1:6379> zadd number 1 12
1
127.0.0.1:6379> zadd number 1 22
1
127.0.0.1:6379> zadd number 2 10
1
127.0.0.1:6379> zadd number 2 5
1
127.0.0.1:6379> zrangebyscore number 0 10
12
22
10
5
127.0.0.1:6379> type number
zset
注意:
Redis不支持自定义数据库的名字,每个数据库都以编号命名,开发者必须自己记录哪些数据库存储了哪些数据。
另外Redis也不支持为每个数据库设置不同的访问密码,所以一个客户端要么可以访问全部数据库,要么连一个数据库也没有权限访问。
最重要的一点是多个数据库之间并不是完全隔离的
比如FLUSHALL命令可以清空一个Redis实例中所有数据库中的数据。
综上所述,这些数据库更像是一种命名空间,而不适宜存储不同应用程序的数据。
作用场景:
可以使用0号数据库存储某个应用生产环境中的数据,使用1号数据库存储测试环境中的数据,
但不适宜使用0号数据库存储A应用的数据而使用1号数据库B应用的数据,不同的应用应该使用不同的Redis实例存储数据。由于Redis非常轻量级,一个空Redis实例占用的内存只有1M左右,所以不用担心多个Redis实例会额外占用很多内存。
key常规管理
验证key是否存在 exists
#1为存在,0为不存在
127.0.0.1:6379> exists number
1
127.0.0.1:6379> exists numbers
0
重命名 rename
127.0.0.1:6379> rename number numbers
OK
设置超时(保存)时间 pexpire 单位:毫秒
127.0.0.1:6379> pexpire numbers 3000
1
127.0.0.1:6379> get numbers
##数据没了
查看剩余时间 PTTL(单位:毫秒)
127.0.0.1:6379> pttl name
(integer) -1 ###负一代表永久
127.0.0.1:6379> pexpire name 50000
(integer) 1 ###设置保存时间为50秒
127.0.0.1:6379> pttl name
(integer) 44206 ###剩余的时间
127.0.0.1:6379> pttl name
(integer) -2 ###负二表示不存在的值
127.0.0.1:6379> get name
(nil) ###nil:空,无
随机返回key randomkey
127.0.0.1:6379> flushall ##清空所有,生产环境不要用!!!!
OK
127.0.0.1:6379> mset name zhangsan age 44 sex man
OK
127.0.0.1:6379> randomkey
"name"
127.0.0.1:6379> randomkey
"sex"
127.0.0.1:6379> randomkey
"sex"
127.0.0.1:6379> randomkey
"age"
查看所有key
127.0.0.1:6379> keys *
1) "age"
2) "name"
3) "sex"
数据转移和切库
- redis默认支持从0开始到15的16个库,不过可以通过配置文件支持更多,无上限
- 每个库的数据时隔离的不共享,并且基于单机才有,如果时集群的话就没有数据库的概念。
- 客户端与redis建立连接后会自动选择0号数据库
切换数据库命令 select
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> select 15
OK
127.0.0.1:6379[15]> select 16
(error) ERR DB index is out of range
数据转移命令 move
127.0.0.1:6379> keys *
1) "age"
2) "name"
3) "sex"
127.0.0.1:6379> move sex 1
(integer) 1
127.0.0.1:6379> move name 2
(integer) 1
127.0.0.1:6379> keys *
1) "age"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
1) "sex"
127.0.0.1:6379[1]> select 2
OK
127.0.0.1:6379[2]> keys *
1) "name"