Redis 提供了几个面向 Redis 数据库的操做,例如以前已经介绍过或者使用过的 DBSIZE
、SELECT
、FLUSHDB/FLUSHALL
本节将经过具体的使用场景介绍这些命令。node
自1.0.0可用。redis
时间复杂度:O(1)。算法
切换到指定的数据库,数据库索引号 index
用数字值指定,以 0
做为起始索引值。shell
默认使用 0
号数据库。数据库
OKexpress
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个数据库:segmentfault
# 这里是 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
命令会将全部数据清除,一旦误操做后果不堪设想。FLUSHDB/FLUSHALL
存在阻塞 Redis 的可能性。因此在使用FLUSHDB/FLUSHALL
必定要当心谨慎。
Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf。
能够经过 CONFIG 命令查看或设置配置项,该命令也支持动态设置配置。
127.0.0.1:6379> CONFIG GET loglevel 1) "loglevel" 2) "notice"
使用 * 号获取全部配置项:
127.0.0.1:6379> CONFIG GET * 1) "dbfilename" 2) "dump.rdb" 3) "requirepass" 4) "admin123" 5) "masterauth" 6) "148a6f61787d4ac5:ZnKe123qwe" 7) "unixsocket" 8) "" 9) "logfile" 10) "" 11) "pidfile" 12) "" 13) "maxmemory" 14) "0" 15) "maxmemory-samples" 16) "5" 17) "timeout" 18) "0" 19) "auto-aof-rewrite-percentage" 20) "100" 21) "auto-aof-rewrite-min-size" 22) "67108864" 23) "hash-max-ziplist-entries" 24) "512" 25) "hash-max-ziplist-value" 26) "64" 27) "list-max-ziplist-size" 28) "-2" 29) "list-compress-depth" 30) "0" 31) "set-max-intset-entries" 32) "512" 33) "zset-max-ziplist-entries" 34) "128" 35) "zset-max-ziplist-value" 36) "64" 37) "hll-sparse-max-bytes" 38) "3000" 39) "lua-time-limit" 40) "5000" 41) "slowlog-log-slower-than" 42) "10000" 43) "latency-monitor-threshold" 44) "0" 45) "slowlog-max-len" 46) "128" 47) "port" 48) "6379" 49) "tcp-backlog" 50) "511" 51) "databases" 52) "16" 53) "repl-ping-slave-period" 54) "10" 55) "repl-timeout" 56) "60" 57) "repl-backlog-size" 58) "1048576" 59) "repl-backlog-ttl" 60) "3600" 61) "maxclients" 62) "10000" 63) "watchdog-period" 64) "0" 65) "slave-priority" 66) "100" 67) "min-slaves-to-write" 68) "0" 69) "min-slaves-max-lag" 70) "10" 71) "hz" 72) "10" 73) "cluster-node-timeout" 74) "15000" 75) "cluster-migration-barrier" 76) "1" 77) "cluster-slave-validity-factor" 78) "10" 79) "repl-diskless-sync-delay" 80) "5" 81) "tcp-keepalive" 82) "0" 83) "cluster-require-full-coverage" 84) "yes" 85) "no-appendfsync-on-rewrite" 86) "no" 87) "slave-serve-stale-data" 88) "yes" 89) "slave-read-only" 90) "yes" 91) "stop-writes-on-bgsave-error" 92) "yes" 93) "daemonize" 94) "no" 95) "rdbcompression" 96) "yes" 97) "rdbchecksum" 98) "yes" 99) "activerehashing" 100) "yes" 101) "protected-mode" 102) "yes" 103) "repl-disable-tcp-nodelay" 104) "no" 105) "repl-diskless-sync" 106) "no" 107) "aof-rewrite-incremental-fsync" 108) "yes" 109) "aof-load-truncated" 110) "yes" 111) "maxmemory-policy" 112) "noeviction" 113) "loglevel" 114) "notice" 115) "supervised" 116) "no" 117) "appendfsync" 118) "everysec" 119) "appendonly" 120) "no" 121) "dir" 122) "D:\\redis" 123) "save" 124) "jd 900 jd 300 jd 60" 125) "client-output-buffer-limit" 126) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60" 127) "unixsocketperm" 128) "0" 129) "slaveof" 130) "" 131) "notify-keyspace-events" 132) "" 133) "bind" 134) "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
<dbid>
命令在链接上指定数据库id databases 16
save <seconds> <changes>
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 <masterip> <masterport>
masterauth <master-password>
<password>
命令提供密码,默认关闭 requirepass foobared
maxclients 128
maxmemory <bytes>
appendonly no
appendfilename appendonly.aof
指定更新日志条件,共有3个可选值:
**no**:表示等操做系统进行数据缓存同步到磁盘(快) **always**:表示每次更新操做后手动调用fsync()将数据写到磁盘(慢,安全) **everysec**:表示每秒同步一次(折衷,默认值)
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
命令对它们动态地进行修改。
第一个选项是 slowlog-log-slower-than
,它决定要对执行时间大于多少微秒(microsecond,1秒 = 1,000,000 微秒)的查询进行记录。
若是 slowlog-log-slower-than
等于 0 会记录全部的命令,slowlog-log-slowerthan
小于0 对于任何命令都不会进行记录。
好比执行如下命令将让 slow log 记录全部查询时间大于等于 100 微秒的查询:
CONFIG SET slowlog-log-slower-than 100
而如下命令记录全部查询时间大于 1000 微秒的查询:
CONFIG SET slowlog-log-slower-than 1000
另外一个选项是 slowlog-max-len
,它决定 slow log 最多能保存多少条日志, slow log 自己是一个 FIFO(First Input First Output 先进先出) 队列(在 Redis 中实际是一个列表),当队列大小超过 slowlog-max-len
时,最旧的一条日志将被删除,而最新的一条日志加入到 slow log ,以此类推。
如下命令让 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
或者 SLOWLOG GET number
命令,前者打印全部 slow log ,最大长度取决于 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
使用命令 SLOWLOG RESET
能够清空 slow log 。
coderknock> SLOWLOG LEN (integer) 14 coderknock> SLOWLOG RESET OK coderknock> SLOWLOG LEN (integer) 0
= 2.2.12
O(1)
取决于不一样命令,返回不一样的值。
取决于不一样命令,返回不一样的值。
慢查询功能能够有效地帮助咱们找到Redis可能存在的瓶颈,但在实际使用过程当中要注意如下几点:
slowlog-max-len
配置建议:线上建议调大慢查询列表,记录慢查询时 Redis 会对长命令作截断操做,并不会占用大量内存。增大慢查询列表能够减缓慢查询被剔除的可能,例如线上可设置为1000以上。slowlog-log-slower-than
配置建议:默认值超过10毫秒断定为慢查询,须要根据Redis并发量调整该值。因为 Redis 采用单线程响应命令,对于高流量的场景,若是命令执行时间在1毫秒以上,那么 Redis 最多可支撑 OPS 不到1000。所以对于高 OPS 场景的 Redis 建议设置为1毫秒。SLOW get
命令将慢查询日志持久化到其余存储中(例如MySQL),而后能够制做可视化界面进行查询。本人 Redis 方面的讲座已经上线快去看看有没有你感兴趣的吧(说不定会有优惠哟~~数量有限要抓紧呀):