Redis 提供了几个面向 Redis 数据库的操作,例如之前已经介绍过或者使用过的
、
- DBSIZE
、
- SELECT
本节将通过具体的使用场景介绍这些命令。
- FLUSHDB/FLUSHALL
自1.0.0可用。
**时间复杂度:**O(1)。
切换到指定的数据库,数据库索引号
用数字值指定,以
- index
作为起始索引值。
- 0
默认使用
号数据库。
- 0
OK
- coderknock> SET db_number 0 # 默认使用 0 号数据库
- OK
- coderknock> SELECT 1 # 使用 1 号数据库
- OK
- coderknock[1]> GET db_number # 已经切换到 1 号数据库,注意 Redis 现在的命令提示符多了个 [1]
- (nil)
- coderknock[1]> SET db_number 1
- OK
- coderknock[1]> GET db_number
- "1"
- coderknock[1]> SELECT 3 # 再切换到 3 号数据库
- OK
- coderknock[3]> # 提示符从 [1] 改变成了 [3]
许多关系型数据库,例如 MySQL 支持在一个实例下有多个数据库存在的,但是与关系型数据库用字符来区分不同数据库名不同,Redis 只是用数字作为多个数据库的区分。Redis 默认配置中是有16个数据库:
- # 这里是 Redis 配置文件中的配置项
- databases 16
- #以下是在客户端中进行测试
- # 此处可以修改,如果没有修改使用 超过 15 索引的数据库会报错(索引从 0 开始 15 代表有 16 个库)
- 127.0.0.1:6379> SELECT 16
- (error) ERR invalid DB index
- 127.0.0.1:6379> SELECT 15
- OK
Redis3.0 中已经逐渐弱化这个功能,例如 Redis 的分布式实现 Redis Cluster 只允许使用0号数据库,只不过为了向下兼容老版本的数据库功能,该功能没有完全废弃掉,下面分析一下为什么要废弃掉这个“优秀”的功能呢?总结起来有三点:
建议如果要使用多个数据库功能,完全可以在一台机器上部署多个 Redis 实例,彼此用端口来做区分,因为现代计算机或者服务器通常是有多个 CPU 的。这样既保证了业务之间不会受到影响,又合理地使用了 CPU 资源。
自1.0.0可用。
时间复杂度:尚未明确。
清空整个 Redis 服务器的数据(删除所有数据库的所有 key )。
此命令不会失败。
Redis 4.0 版本提供了
可选项,用于将该操作另启一个线程,可以起到异步释放的效果。
- ASYNC
总是返回
。
- OK
- coderknock> DBSIZE # 0 号数据库的 key 数量
- (integer) 9
- coderknock> SELECT 1 # 切换到 1 号数据库
- OK
- coderknock[1]> DBSIZE # 1 号数据库的 key 数量
- (integer) 6
- coderknock[1]> flushall # 清空所有数据库的所有 key
- OK
- coderknock[1]> DBSIZE # 不但 1 号数据库被清空了
- (integer) 0
- coderknock[1]> SELECT 0 # 0 号数据库(以及其他所有数据库)也一样
- OK
- coderknock> DBSIZE
- (integer) 0
自1.0.0可用。
**时间复杂度:O(1)。
清空当前数据库中的所有 key。
此命令不会失败。
Redis 4.0 版本提供了
可选项,用于将该操作另启一个线程,可以起到异步释放的效果。
- ASYNC
总是返回
。
- OK
- coderknock> DBSIZE # 清空前的 key 数量
- (integer) 4
- coderknock> FLUSHDB
- OK
- coderknock> DBSIZE # 清空后的 key 数量
- (integer) 0
命令可以非常方便的清理数据,但是也带来两个问题:
- FLUSHDB/FLUSHALL
命令会将所有数据清除,一旦误操作后果不堪设想。
- FLUSHDB/FLUSHALL
存在阻塞 Redis 的可能性。
- FLUSHDB/FLUSHALL
所以在使用
一定要小心谨慎。
- FLUSHDB/FLUSHALL
Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf。
可以通过 CONFIG 命令查看或设置配置项,该命令也支持动态设置配置。
- 127.0.0.1:6379> CONFIG GET loglevel
- 1) "loglevel"
- 2) "notice"
使用 * 号获取所有配置项:
- .0.0.1:6379> CONFIG GET *
- ) "dbfilename"
- ) "dump.rdb"
- ) "requirepass"
- ) "admin123"
- ) "masterauth"
- ) "148a6f61787d4ac5:ZnKe123qwe"
- ) "unixsocket"
- ) ""
- ) "logfile"
- ) ""
- ) "pidfile"
- ) ""
- ) "maxmemory"
- ) "0"
- ) "maxmemory-samples"
- ) "5"
- ) "timeout"
- ) "0"
- ) "auto-aof-rewrite-percentage"
- ) "100"
- ) "auto-aof-rewrite-min-size"
- ) "67108864"
- ) "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"
- ) "lua-time-limit"
- ) "5000"
- ) "slowlog-log-slower-than"
- ) "10000"
- ) "latency-monitor-threshold"
- ) "0"
- ) "slowlog-max-len"
- ) "128"
- ) "port"
- ) "6379"
- ) "tcp-backlog"
- ) "511"
- ) "databases"
- ) "16"
- ) "repl-ping-slave-period"
- ) "10"
- ) "repl-timeout"
- ) "60"
- ) "repl-backlog-size"
- ) "1048576"
- ) "repl-backlog-ttl"
- ) "3600"
- ) "maxclients"
- ) "10000"
- ) "watchdog-period"
- ) "0"
- ) "slave-priority"
- ) "100"
- ) "min-slaves-to-write"
- ) "0"
- ) "min-slaves-max-lag"
- ) "10"
- ) "hz"
- ) "10"
- ) "cluster-node-timeout"
- ) "15000"
- ) "cluster-migration-barrier"
- ) "1"
- ) "cluster-slave-validity-factor"
- ) "10"
- ) "repl-diskless-sync-delay"
- ) "5"
- ) "tcp-keepalive"
- ) "0"
- ) "cluster-require-full-coverage"
- ) "yes"
- ) "no-appendfsync-on-rewrite"
- ) "no"
- ) "slave-serve-stale-data"
- ) "yes"
- ) "slave-read-only"
- ) "yes"
- ) "stop-writes-on-bgsave-error"
- ) "yes"
- ) "daemonize"
- ) "no"
- ) "rdbcompression"
- ) "yes"
- ) "rdbchecksum"
- ) "yes"
- ) "activerehashing"
- ) "yes"
- ) "protected-mode"
- ) "yes"
- ) "repl-disable-tcp-nodelay"
- ) "no"
- ) "repl-diskless-sync"
- ) "no"
- ) "aof-rewrite-incremental-fsync"
- ) "yes"
- ) "aof-load-truncated"
- ) "yes"
- ) "maxmemory-policy"
- ) "noeviction"
- ) "loglevel"
- ) "notice"
- ) "supervised"
- ) "no"
- ) "appendfsync"
- ) "everysec"
- ) "appendonly"
- ) "no"
- ) "dir"
- ) "D:\\redis"
- ) "save"
- ) "jd 900 jd 300 jd 60"
- ) "client-output-buffer-limit"
- ) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
- ) "unixsocketperm"
- ) "0"
- ) "slaveof"
- ) ""
- ) "notify-keyspace-events"
- ) ""
- ) "bind"
- ) "127.0.0.1"
你可以通过修改 redis.conf 文件或使用
命令来修改配置。
- CONFIG set
- redis 127.0.0.1 : 6379 > CONFIG SET loglevel "notice"OKredis 127.0.0.1 : 6379 > CONFIG GET loglevel 1)"loglevel"2)"notice"
redis.conf 配置项说明如下:
daemonize no
pidfile /var/run/redis.pid
port 6379
bind 127.0.0.1
timeout 300
loglevel verbose
logfile stdout
命令在连接上指定数据库id
- <dbid>
databases 16
save
Redis默认配置文件中提供了三个条件:
save 900 1
save 300 10
save 60 10000
分别表示900秒(15分钟)内有1个更改,300秒(5分钟)内有10个更改以及60秒内有10000个更改。
rdbcompression yes
dbfilename dump.rdb
dir ./
slaveof
masterauth
命令提供密码,默认关闭
- <password>
requirepass foobared
maxclients 128
maxmemory
appendonly no
appendfilename appendonly.aof
appendfsync everysec
vm-enabled no
vm-swap-file /tmp/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
glueoutputbuf yes
hash-max-zipmap-entries 64
hash-max-zipmap-value 512
activerehashing yes
include /path/to/local.conf
Slow log 是 Redis 用来记录查询执行时间的日志系统。
查询执行时间指的是不包括像客户端响应(talking)、发送回复等 IO 操作,而单单是执行一个查询命令所耗费的时间,所以没有慢查询并不代表客户端没有超时问题。
另外,slow log 保存在内存里面,读写速度非常快,因此你可以放心地使用它,不必担心因为开启 slow log 而损害 Redis 的速度。
Slow log 的行为由两个配置参数(configuration parameter)指定,可以通过改写 redis.conf 文件或者用
和
- CONFIG GET
命令对它们动态地进行修改。
- CONFIG SET
第一个选项是
,它决定要对执行时间大于多少微秒(microsecond,1秒 = 1,000,000 微秒)的查询进行记录。
- slowlog-log-slower-than
如果
等于 0 会记录所有的命令,
- slowlog-log-slower-than
小于0 对于任何命令都不会进行记录。
- slowlog-log-slowerthan
比如执行以下命令将让 slow log 记录所有查询时间大于等于 100 微秒的查询:
- CONFIG SET slowlog-log-slower-than 100
而以下命令记录所有查询时间大于 1000 微秒的查询:
- CONFIG SET slowlog-log-slower-than 1000
另一个选项是
,它决定 slow log 最多能保存多少条日志, slow log 本身是一个 FIFO(First Input First Output 先进先出) 队列(在 Redis 中实际是一个列表),当队列大小超过
- slowlog-max-len
时,最旧的一条日志将被删除,而最新的一条日志加入到 slow log ,以此类推。
- slowlog-max-len
以下命令让 slow log 最多保存 1000 条日志:
- CONFIG SET slowlog-max-len 1000
如果要 Redis 将配置持久化到本地配置文件,需要执行
命令
- CONFIG rewrite
使用
命令可以查询两个选项的当前值:
- CONFIG GET
- coderknock> CONFIG GET slowlog-log-slower-than
- 1) "slowlog-log-slower-than"
- 2) "10000"
- coderknock> CONFIG GET slowlog-max-len
- 1) "slowlog-max-len"
- 2) "128"
redis.conf:
- # The following time is expressed in microseconds, so 1000000 is equivalent
- # to one second. Note that a negative number disables the slow log, while
- # a value of zero forces the logging of every command.
- slowlog-log-slower-than 10000
- # There is no limit to this length. Just be aware that it will consume memory.
- # You can reclaim memory used by the slow log with SLOWLOG RESET.
- slowlog-max-len 128
要查看 slow log ,可以使用
或者
- SLOWLOG GET
命令,前者打印所有 slow log ,最大长度取决于
- SLOWLOG GET number
选项的值,而
- slowlog-max-len
则只打印指定数量的日志。
- SLOWLOG GET number
最新的日志会最先被打印:
- # 为测试需要,将 slowlog-log-slower-than 设成了 10 微秒
- coderknock> SLOWLOG GET
- 1) 1) (integer) 12 # 唯一性(unique)的日志标识符
- 2) (integer) 1324097834 # 被记录命令的执行时间点,以 UNIX 时间戳格式表示
- 3) (integer) 16 # 查询执行时间,以微秒为单位
- 4) 1) "CONFIG" # 执行的命令,以数组的形式排列
- 2) "GET" # 这里完整的命令是 CONFIG GET slowlog-log-slower-than
- 3) "slowlog-log-slower-than"
- 2) 1) (integer) 11
- 2) (integer) 1324097825
- 3) (integer) 42
- 4) 1) "CONFIG"
- 2) "GET"
- 3) "*"
- 3) 1) (integer) 10
- 2) (integer) 1324097820
- 3) (integer) 11
- 4) 1) "CONFIG"
- 2) "GET"
- 3) "slowlog-log-slower-than"
- # ...
日志的唯一 id 只有在 Redis 服务器重启的时候才会重置,这样可以避免对日志的重复处理(比如你可能会想在每次发现新的慢查询时发邮件通知你)。
使用命令
可以查看当前日志的数量。
- SLOWLOG LEN
请注意这个值和
的区别,它们一个是当前日志的数量,一个是允许记录的最大日志的数量。
- slower-max-len
- coderknock> SLOWLOG LEN
- (integer) 14
使用命令
可以清空 slow log 。
- SLOWLOG RESET
- coderknock> SLOWLOG LEN
- (integer) 14
- coderknock> SLOWLOG RESET
- OK
- coderknock> SLOWLOG LEN
- (integer) 0
= 2.2.12
慢查询功能可以有效地帮助我们找到Redis可能存在的瓶颈,但在实际使用过程中要注意以下几点:
配置建议:线上建议调大慢查询列表,记录慢查询时 Redis 会对长命令做截断操作,并不会占用大量内存。增大慢查询列表可以减缓慢查询被剔除的可能,例如线上可设置为1000以上。
- slowlog-max-len
配置建议:默认值超过10毫秒判定为慢查询,需要根据Redis并发量调整该值。由于 Redis 采用单线程响应命令,对于高流量的场景,如果命令执行时间在1毫秒以上,那么 Redis 最多可支撑 OPS 不到1000。因此对于高 OPS 场景的 Redis 建议设置为1毫秒。
- slowlog-log-slower-than
命令将慢查询日志持久化到其他存储中(例如MySQL),然后可以制作可视化界面进行查询。
- SLOW get
本人 Redis 方面的讲座已经上线快去看看有没有你感兴趣的吧(说不定会有优惠哟~~数量有限要抓紧呀):
提升职业竞争力的必备知识 Redis 之初识
提升职业竞争力的必备知识 Redis 之高级特性
提升职业竞争力的必备知识 Redis 之缓存与Session共享
提升职业竞争力的必备知识 Redis 之排行榜与附近的人
来源: http://blog.csdn.net/sanchan/article/details/77829273