一、Redis常用基本命令
官方文档:https://redis.io/commands/
参考文档:http://redisdoc.com/
- #可以使用help命令查看各redis命令用法
[root@Client-Ubuntu-1804-250:~]# redis-cli -a redis --no-auth-warning help - redis-cli 5.0.14
- To get help about Redis commands type:
- "help @<group>" to get a list of commands in <group>
- "help <command>" for help on <command>
- "help <tab>" to get a list of possible help topics
- "quit" to exit
- To set redis-cli preferences:
- ":set hints" enable online hints
- ":set nohints" disable online hints
- Set your preferences in ~/.redisclirc
- [root@Client-Ubuntu-1804-250:~]# redis-cli -a redis --no-auth-warning
- #查看info命令帮助
- 127.0.0.1:6379> help info
- INFO [section]
- summary: Get information and statistics about the server
- since: 1.0.0
- group: server
- #查看set命令帮助
- 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>
1、INFO
查看当前节点运行状态

info
可以在INFO 后补充筛选内容
- #查看当前节点CPU信息
- 127.0.0.1:6379> info CPU
- # CPU
- used_cpu_sys:0.508881
- used_cpu_user:0.848135
- used_cpu_sys_children:0.000000
- used_cpu_user_children:0.003046
- #查看当前节点Server信息
- 127.0.0.1:6379> info Server
- # Server
- redis_version:5.0.14
- redis_git_sha1:00000000
- redis_git_dirty:0
- redis_build_id:5d32a2d9ed5f67d5
- redis_mode:standalone
- os:Linux 4.15.0-210-generic x86_64
- arch_bits:64
- multiplexing_api:epoll
- atomicvar_api:atomic-builtin
- gcc_version:7.5.0
- process_id:6069
- run_id:0e67fcd27ff6ae2589ff90ac2516bd52269f2965
- tcp_port:6379
- uptime_in_seconds:1358
- uptime_in_days:0
- hz:10
- configured_hz:10
- lru_clock:5746346
- executable:/app/redis/bin/redis-server
- config_file:/app/redis/etc/redis_6379.conf
2、SELECT
切换数据库,类似于MySQL是 USE DATABASES;
在Cluster模式下不支持多DB模式,会出现以下错误提示
- 10.0.0.20:6379> info cluster
- # Cluster
- cluster_enabled:1
- 10.0.0.20:6379> select 1
- (error) ERR SELECT is not allowed in cluster mode

- 127.0.0.1:6379> help select
- SELECT index
- summary: Change the selected database for the current connection
- since: 1.0.0
- group: connection
- 127.0.0.1:6379> select 1
- OK
- 127.0.0.1:6379[1]> select 2
- OK
- 127.0.0.1:6379[2]> select 3
- OK
- 127.0.0.1:6379[3]>
select
3、KEYS
查看当前数据库下所有的key(数据量大时会对数据库造成巨大压力,此命令慎用)
此命令仅查询当前db下的数据
- 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]> info KeySpace
- # Keyspace
- db0:keys=1000,expires=0,avg_ttl=0
4、BGSAVE
手动触发后台执行save操作
- 127.0.0.1:6379>
- 127.0.0.1:6379> help bgsave
- BGSAVE -
- summary: Asynchronously save the dataset to disk
- since: 1.0.0
- group: server
- 127.0.0.1:6379> bgsave
- Background saving started
5、DBSIZE
返回当前数据库下的所有key数量
- 127.0.0.1:6379> help dbsize
- DBSIZE -
- summary: Return the number of keys in the selected database
- since: 1.0.0
- group: server
- 127.0.0.1:6379> dbsize
- (integer) 1000
- 127.0.0.1:6379> select 1
- OK
- 127.0.0.1:6379[1]> dbsize
- (integer) 0
6、FLUSHDB
强制清空当前数据库中的所有key,不会影响其他db中的数据,谨慎使用
- 127.0.0.1:6379> help flushdb
- FLUSHDB [ASYNC]
- summary: Remove all keys from the current database
- since: 1.0.0
- group: server
- 127.0.0.1:6379[1]> info KeySpace
- # Keyspace
- db0:keys=1000,expires=0,avg_ttl=0
- db1:keys=4,expires=0,avg_ttl=0
- #清空db1中的所有键值,不会影响到db0中的数据
- 127.0.0.1:6379[1]> flushdb
- OK
- 127.0.0.1:6379[1]> info KeySpace
- # Keyspace
- db0:keys=1000,expires=0,avg_ttl=0
- 127.0.0.1:6379[1]>
7、FLUSHALL
强制清空当前redis节点上所有数据库中的所有key,即删除当前节点所有数据,谨慎使用,必要情况建议禁用此命令
- 127.0.0.1:6379> info KeySpace
- # Keyspace
- db0:keys=1000,expires=0,avg_ttl=0
- db1:keys=4,expires=0,avg_ttl=0
- 127.0.0.1:6379> flushall
- OK
- 127.0.0.1:6379> info KeySpace
- # Keyspace
- 127.0.0.1:6379>
8、SHUTDOWN
时间复杂度:O(N) N为需要保存的数据库键数量
SHUTDOWN执行过程:
- 停止所有客户端连接
- 如果至少有一个保存节点在等待,执行SAVE操作
- 如果AOF开启,更新AOF内容
- 关闭 Redis Server
如果开启了持久化配置,SHUTDOWN命令可以保障服务器正常关闭而数据不丢失
如果单纯执行SAVE之后,执行 QUIT 命令,则无法保证数据不丢失,SAVE执行完成后,执行 QUIT 命令的过程中,服务器可能依旧存在与客户端的访问连接,会造成这期间数据的丢失。
- 127.0.0.1:6379> help shutdown
- SHUTDOWN [NOSAVE|SAVE]
- summary: Synchronously save the dataset to disk and then shut down the server
- since: 1.0.0
- group: server
- 127.0.0.1:6379> shutdown
- not connected>
- not connected>
- not connected> exit
- [root@Client-Ubuntu-1804-250:~]# ps -aux | grep redis | grep ^grep
- [root@Client-Ubuntu-1804-250:~]# ss -ntlp
- State Recv-Q Send-Q Local Address:Port Peer Address:Port
- LISTEN 0 128 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=867,fd=13))
- LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=978,fd=3))
- LISTEN 0 128 127.0.0.1:6010 0.0.0.0:* users:(("sshd",pid=1661,fd=10))
- LISTEN 0 128 127.0.0.1:6011 0.0.0.0:* users:(("sshd",pid=1661,fd=15))
- LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=978,fd=4))
- LISTEN 0 128 [::1]:6010 [::]:* users:(("sshd",pid=1661,fd=9))
- LISTEN 0 128 [::1]:6011 [::]:* users:(("sshd",pid=1661,fd=12))
禁用Redis命令(rename配置)
可通过配置文件中的 rename-comand <command> "" 对原有命令进行重命名(通常用于对高危命令进行管控)
- #在启用aof情况下,禁用或重命名命令可能会导致redis服务无法启动,是因为实例在之前执行被重命名的命令,导致加载aof时,命令执行失败
6682:C 07 May 2023 23:20:57.411 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo - 6682:C 07 May 2023 23:20:57.411 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=6682, just started
- 6682:C 07 May 2023 23:20:57.411 # Configuration loaded
- 6682:C 07 May 2023 23:20:57.411 * supervised by systemd, will signal readiness
- 6682:M 07 May 2023 23:20:57.417 * Running mode=standalone, port=6379.
- 6682:M 07 May 2023 23:20:57.419 # Server initialized
- 6682:M 07 May 2023 23:20:57.420 # Unknown command 'flushdb' reading the append only file
解决方案:
1、禁用aof选项
2、修改配置重启服务之前,执行 bgrewriteaof 重新生成aof内容
- # Command renaming.
- #
- # It is possible to change the name of dangerous commands in a shared
- # environment. For instance the CONFIG command may be renamed into something
- # hard to guess so that it will still be available for internal-use tools
- # but not available for general clients.
- #
- # Example:
- #
- # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
- #
- # It is also possible to completely kill a command by renaming it into
- # an empty string:
- #
- # rename-command CONFIG ""
- #
- # Please note that changing the name of commands that are logged into the
- # AOF file or transmitted to replicas may cause problems.
- renmae-comand FLUSHALL ""
- rename-comand FULSHDB "REMOVE-THIS-DATABASE"
- 127.0.0.1:6379> info keySpace
- # Keyspace
- db0:keys=1000,expires=0,avg_ttl=0
- 127.0.0.1:6379> flushall
- (error) ERR unknown command `flushall`, with args beginning with:
- 127.0.0.1:6379> flushdb
- (error) ERR unknown command `flushdb`, with args beginning with:
- 127.0.0.1:6379> info keySpace
- # Keyspace
- db0:keys=1000,expires=0,avg_ttl=0
- 127.0.0.1:6379>
- 127.0.0.1:6379> REMOVE-THIS-DATABASE
- OK
- 127.0.0.1:6379> info keySpace
- # Keyspace
- 127.0.0.1:6379>