Redis的官方文档介绍了多种安装方式(包括Linux、Windows、MacOs平台上的安装和从源码包安装):Redis安装。这里只介绍源码安装方式。
下载源码包
$ wget https://download.redis.io/redis-stable.tar.gz
编译Redis
$ tar -xzvf redis-stable.tar.gz
$ cd redis-stable
$ ./configure prefix=/usr/local/bin/redis # 指定安装目录为/usr/local/bin/redis
$ make distclean && make
$ make test
如果编译成功,将会在src目录下生成两个二进制文件
安装Redis到**/usr/local/bin/redis**(默认安装到/usr/local)
$ make install
安装成功后将会在**/usr/local/bin/redis**下有一个bin文件夹:
修改Redis启动方式为后台启动
将源码包中的redis.conf复制到安装路径/usr/local/bin/redis/
下
$ cp /usr/local/src/redis-stable/redis.conf /usr/local/bin/redis/
修改daemonize
为yes
启动redis
$ ./bin/redis-server ./redis.conf
配置Redis其他机器可以连接本机Redis服务
$ vim ./redis.conf
protected-mode no
bind 0.0.0.0
停止Redis服务
$ redis-cli shutdown
默认设置下String的值最大不能超过512M。
基础使用
> set bike:1 Deimos
> get bike:1
同时设置和获取多个值
> mset bike:1 "Deimos" bike:2 "Ares" bike:3 "Vanth"
> mget bike:1 bike:2 bike:3
字符串计数
> set total_crashes 0
> incr total_crashes
(integer) 1
> incrby total_crashes 10
(integer) 11
> hset bike:1 model Deimos brand Ergonom type 'Enduro bikes' price 4972
(integer) 4
> hget bike:1 model
"Deimos"
> hget bike:1 price
"4972"
> hgetall bike:1
1) "model"
2) "Deimos"
3) "brand"
4) "Ergonom"
5) "type"
6) "Enduro bikes"
7) "price"
8) "4972"
hmget
> hmget bike:1 model price no-such-field
1) "Deimos"
2) "4972"
3) (nil)
hincryby
> hincrby bike:1 price 100
(integer) 5072
> hincrby bike:1 price -100
(integer) 4972
Redis list是一个链表结构
LPUSH从链表头部添加元素,RPUSH从链表尾部添加元素
LPOP从链表头部弹出元素,RPOP从链表尾部弹出元素
先进先出
> LPUSH work:queue:ids 101
(integer) 1
> LPUSH work:queue:ids 237
(integer) 2
> RPOP work:queue:ids
"101"
> RPOP work:queue:ids
"237"
后进先出
> LPUSH work:queue:ids 101
(integer) 1
> LPUSH work:queue:ids 237
(integer) 2
> LPOP work:queue:ids
"237"
> LPOP work:queue:ids
"101"
LLEN检查链表长度
> LLEN work:queue:ids
(integer) 0
LTRIM只保留指定区间的元素,其他的元素将会被删除
> LPUSH notifications:user:1 "You've got mail!"
(integer) 1
> LTRIM notifications:user:1 0 99
OK
> LPUSH notifications:user:1 "Your package will be delivered at 12:01 today."
(integer) 2
> LTRIM notifications:user:1 0 99
OK
LMOVE将一个链表的头部(尾部)元素删除并添加到另外一个链表的头部(或尾部)
LMOVE source destination LEFT|RIGHT LEFT|RIGHT
中第一个LEFT|RIGHT
指定从头部还是尾部删除,第二个LEFT|RIGHT
指定从尾部还是头部添加。
> LPUSH board:todo:ids 101
(integer) 1
> LPUSH board:todo:ids 273
(integer) 2
> LMOVE board:todo:ids board:in-progress:ids LEFT LEFT
"273"
> LRANGE board:todo:ids 0 -1
1) "101"
> LRANGE board:in-progress:ids 0 -1
1) "273"
Set中不允许出现重复的元素
SADD添加元素
> SADD user:123:favorites 347
(integer) 1
> SADD user:123:favorites 561
(integer) 1
> SADD user:123:favorites 742
(integer) 1
> SADD user:456:favorites 561
(integer) 1
SISMEMBER判断Set是否有指定元素
> SISMEMBER user:123:favorites 742
(integer) 1
> SISMEMBER user:123:favorites 299
(integer) 0
SINTER返回两个或多个Set中共同存在的元素(交集)
> SINTER user:123:favorites user:456:favorites
1) "561"
SCARD返回Set的元素个数
> SCARD user:123:favorites
(integer) 3
SMEMBERS查看Set的成员列表
> sadd myset 1 2 3
(integer) 3
> smembers myset
1. 3
2. 1
3. 2
SREM从Set中删除指定元素
> srem myset 1
SUNIONSTORE计算两个集合的并集并存储到另外一个集合中,如果另外一个集合存在则覆盖
> sunionstore game:1:deck deck
ZADD添加元素
> zadd mysort 70 a 80 b 90 c
(integer) 3
ZSCORE获取指定成员的分数
> zscore mysort a
"70"
ZCARD成员列表个数
> zcard mysort
(integer) 3
ZRANGE指定范围的成员列表(按分数从小到大排序)
> zrange mysort 0 -1
1) "a"
2) "b"
3) "c"
ZREVRANGE指定范围的成员列表(按分数从大到小排序)
> zrevrange mysort 0 -1
1) "c"
2) "b"
3) "a"
ZINCRBY增加指定成员的分数
> zincrby mysort 10 10
"80"
查找所有符合给定模式 pattern的 key,若 key 存在返回 1 ,否则返回 0 。
> keys *
检查给定 key 是否存在
> exists mykey
(integer) 0
获取指定 key 的值。如果 key 不存在,返回 nil 。如果key 储存的值不是字符串类型,返回一个错误。
> get mykey
修改 key 的名字为 newkey
。若key 不存在返回错误。
> RENAME mykey 1
"OK"
设置 key 的过期时间,key 过期后将不再可用。单位以秒计。
> expire mykey 60
删除已存在的键。不存在的 key 会被忽略。
> DEL mykey
Redis事务中的所有命令将会串行化执行,并且执行事务中的命令是Redis不会再执行其他的任何命令。
multi开启事务,multi
后面执行的命令将会在一个事务中执行,直到执行exec
或者discard
(exec
提交事务,discard
回滚事务)。
> multi
> ... # 执行多个命令
> exec
默认支持的持久化方式,是把当前内存中的数据集快照写入磁盘,也就是 Snapshot 快照(数据库中所有键值对数据)。恢复时是将快照文件直接读到内存里。
备份时间配置
# Save the DB to disk.
#
# save [ ...]
# Snapshotting can be completely disabled with a single empty string argument
# as in following example:
# 禁用rdb持久化
# save ""
#
#
# Unless specified otherwise, by default Redis will save the DB:
# * After 3600 seconds (an hour) if at least 1 change was performed
# * After 300 seconds (5 minutes) if at least 100 changes were performed
# * After 60 seconds if at least 10000 changes were performed
# 启用rdb持久化 3600秒后,如果至少有一个命令执行,则备份;300秒后,如果至少有100个命令执行则备份;60秒后,如果至少有10000个命令执行则备份
save 3600 1 300 100 60 10000
备份文件配置
# The filename where to dump the DB 备份文件名
dbfilename dump.rdb
# Remove RDB files used by replication in instances without persistence
# enabled. By default this option is disabled, however there are environments
# where for regulations or other security concerns, RDB files persisted on
# disk by masters in order to feed replicas, or stored on disk by replicas
# in order to load them for the initial synchronization, should be deleted
# ASAP. Note that this option ONLY WORKS in instances that have both AOF
# and RDB persistence disabled, otherwise is completely ignored.
#
# An alternative (and sometimes better) way to obtain the same effect is
# to use diskless replication on both master and replicas instances. However
# in the case of replicas, diskless is not always an option.
rdb-del-sync-files no
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name. 备份文件dump.rdb存储位置
dir ./
默认不启用,需要手动开启,通过保存Redis服务器所执行的写命令来记录数据库状态。
开启aof
持久化
appendonly yes
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
# appendfsync always 每次修改都记录
# 每秒记录
appendfsync everysec
# appendfsync no 不记录