Redis 常用命令
官方文档:https://redis.io/commands
CONFIG
config 命令用于查看当前redis配置、以及不重启redis服务,动态更改redis配置等
注意:不是所有配置都可以动态修改
更改最大内存
127.0.0.1:6379> CONFIG SET maxmemory 8589934592 OK 127.0.0.1:6379> CONFIG GET maxmemory 1) "maxmemory" 2) "8589934592"
1 2 3 4 5 | 127.0.0.1 : 6379 > CONFIG SET maxmemory 8589934592 OK 127.0.0.1 : 6379 > CONFIG GET maxmemory 1 ) "maxmemory" 2 ) "8589934592" |
设置连接密码
127.0.0.1:6379> CONFIG SET requirepass 123456 OK
1 2 | 127.0.0.1 : 6379 > CONFIG SET requirepass 123456 OK |
获取当前配置
#奇数行为键,偶数行为值 127.0.0.1:6379> CONFIG GET * 1) "dbfilename" 2) "dump.rdb" 3) "requirepass" 4) "" 5) "masterauth" 6) "" 7) "cluster-announce-ip" 8) "" 9) "unixsocket" 10) "" 11) "logfile" 12) "/var/log/redis/redis.log" 13) "pidfile" 14) "/var/run/redis_6379.pid" 15) "slave-announce-ip" 16) "" 17) "replica-announce-ip" 18) "" 19) "maxmemory" 20) "0" ......
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #奇数行为键,偶数行为值 127.0.0.1 : 6379 > CONFIG GET * 1 ) "dbfilename" 2 ) "dump.rdb" 3 ) "requirepass" 4 ) "" 5 ) "masterauth" 6 ) "" 7 ) "cluster-announce-ip" 8 ) "" 9 ) "unixsocket" 10 ) "" 11 ) "logfile" 12 ) "/var/log/redis/redis.log" 13 ) "pidfile" 14 ) "/var/run/redis_6379.pid" 15 ) "slave-announce-ip" 16 ) "" 17 ) "replica-announce-ip" 18 ) "" 19 ) "maxmemory" 20 ) "0" . . . . . . |
INFO
显示当前节点redis运行状态信息
127.0.0.1:6379> INFO # Server redis_version:5.0.3 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:8c0bf22bfba82c8f redis_mode:standalone os:Linux 4.18.0-147.el8.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:8.2.1 process_id:725 run_id:8af0d3fba2b7c5520e0981b125cc49c3ce4d2a2f tcp_port:6379 uptime_in_seconds:18552 ......
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 127.0.0.1 : 6379 > INFO # Server redis_version : 5.0.3 redis_git_sha1 : 00000000 redis_git_dirty : 0 redis_build_id : 8c0bf22bfba82c8f redis_mode : standalone os : Linux 4.18.0 - 147.el8.x86_64 x86_64 arch_bits : 64 multiplexing_api : epoll atomicvar_api : atomic - builtin gcc_version : 8.2.1 process_id : 725 run_id : 8af0d3fba2b7c5520e0981b125cc49c3ce4d2a2f tcp_port : 6379 uptime_in_seconds : 18552 . . . . . . |
SELECT
切换数据库,等于MySQL的use DBNAME指令。
127.0.0.1:6379[15]> SELECT 0 OK 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 127.0.0.1:6379[15]>
1 2 3 4 5 6 7 8 9 | 127.0.0.1 : 6379 [ 15 ] > SELECT 0 OK 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 127.0.0.1 : 6379 [ 15 ] > |
KEYS
查看当前库下的所有key,此命令慎用!
1 27.0.0.1:6379[15]> SELECT 0 OK 127.0.0.1:6379> KEYS * 1) "9527" 2) "9526" 3) "paihangbang" 4) "list1" 127.0.0.1:6379> SELECT 1 OK 127.0.0.1:6379[1]> KEYS * (empty list or set) 127.0.0.1:6379[1]>
1 2 3 4 5 6 7 8 9 10 11 12 | 1 27.0.0.1 : 6379 [ 15 ] > SELECT 0 OK 127.0.0.1 : 6379 > KEYS * 1 ) "9527" 2 ) "9526" 3 ) "paihangbang" 4 ) "list1" 127.0.0.1 : 6379 > SELECT 1 OK 127.0.0.1 : 6379 [ 1 ] > KEYS * ( empty list or set ) 127.0.0.1 : 6379 [ 1 ] > |
BGSAVE
手动在后台执行RDB持久化操作
#交互式执行 127.0.0.1:6379[1]> BGSAVE Background saving started #非交互式执行 [root@centos8 ~]#ll /var/lib/redis/ total 4 -rw-r--r-- 1 redis redis 326 Feb 18 22:45 dump.rdb [root@centos8 ~]#redis-cli -h 127.0.0.1 -a '123456' BGSAVE Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Background saving started [root@centos8 ~]#ll /var/lib/redis/ total 4 -rw-r--r-- 1 redis redis 92 Feb 18 22:54 dump.rdb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #交互式执行 127.0.0.1 : 6379 [ 1 ] > BGSAVE Background saving started #非交互式执行 [ root @ centos8 ~ ] #ll /var/lib/redis/ total 4 - rw - r -- r -- 1 redis redis 326 Feb 18 22 : 45 dump . rdb [ root @ centos8 ~ ] #redis-cli -h 127.0.0.1 -a '123456' BGSAVE Warning : Using a password with '-a' or '-u' option on the command line interface may not be safe . Background saving started [ root @ centos8 ~ ] #ll /var/lib/redis/ total 4 - rw - r -- r -- 1 redis redis 92 Feb 18 22 : 54 dump . rdb |
DBSIZE
返回当前库下的所有key 数量
127.0.0.1:6379> DBSIZE (integer) 4 127.0.0.1:6379> SELECT 1 OK 127.0.0.1:6379[1]> DBSIZE (integer) 0
1 2 3 4 5 6 | 127.0.0.1 : 6379 > DBSIZE ( integer ) 4 127.0.0.1 : 6379 > SELECT 1 OK 127.0.0.1 : 6379 [ 1 ] > DBSIZE ( integer ) 0 |
FLUSHDB
强制清空当前库中的所有key
127.0.0.1:6379[1]> SELECT 0 OK 127.0.0.1:6379> DBSIZE (integer) 4 127.0.0.1:6379> FLUSHDB OK 127.0.0.1:6379> DBSIZE (integer) 0 127.0.0.1:6379>
1 2 3 4 5 6 7 8 9 | 127.0.0.1 : 6379 [ 1 ] > SELECT 0 OK 127.0.0.1 : 6379 > DBSIZE ( integer ) 4 127.0.0.1 : 6379 > FLUSHDB OK 127.0.0.1 : 6379 > DBSIZE ( integer ) 0 127.0.0.1 : 6379 > |
FLUSHALL
强制清空当前redis服务器所有数据库中的所有key,即删除所有数据
127.0.0.1:6379> FLUSHALL OK
1 2 | 127.0.0.1 : 6379 > FLUSHALL OK |