一、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 @" to get a list of commands in "help " for help on "help " 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
查看当前节点运行状态
127.0.0.1:6379> help info INFO [section] summary: Get information and statistics about the server since: 1.0.0 group: server 127.0.0.1:6379> info # 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:924 uptime_in_days:0 hz:10 configured_hz:10 lru_clock:5745912 executable:/app/redis/bin/redis-server config_file:/app/redis/etc/redis_6379.conf # Clients connected_clients:1 client_recent_max_input_buffer:2 client_recent_max_output_buffer:0 blocked_clients:0 # Memory used_memory:943496 used_memory_human:921.38K used_memory_rss:7237632 used_memory_rss_human:6.90M used_memory_peak:943496 used_memory_peak_human:921.38K used_memory_peak_perc:100.11% used_memory_overhead:898934 used_memory_startup:791488 used_memory_dataset:44562 used_memory_dataset_perc:29.32% allocator_allocated:1129952 allocator_active:1409024 allocator_resident:14368768 total_system_memory:1006243840 total_system_memory_human:959.63M used_memory_lua:37888 used_memory_lua_human:37.00K used_memory_scripts:0 used_memory_scripts_human:0B number_of_cached_scripts:0 maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction allocator_frag_ratio:1.25 allocator_frag_bytes:279072 allocator_rss_ratio:10.20 allocator_rss_bytes:12959744 rss_overhead_ratio:0.50 rss_overhead_bytes:-7131136 mem_fragmentation_ratio:8.03 mem_fragmentation_bytes:6336128 mem_not_counted_for_evict:1368 mem_replication_backlog:0 mem_clients_slaves:0 mem_clients_normal:49694 mem_aof_buffer:1368 mem_allocator:jemalloc-5.1.0 active_defrag_running:0 lazyfree_pending_objects:0 # Persistence loading:0 rdb_changes_since_last_save:658 rdb_bgsave_in_progress:0 rdb_last_save_time:1683467492 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:0 rdb_current_bgsave_time_sec:-1 rdb_last_cow_size:233472 aof_enabled:1 aof_rewrite_in_progress:0 aof_rewrite_scheduled:0 aof_last_rewrite_time_sec:-1 aof_current_rewrite_time_sec:-1 aof_last_bgrewrite_status:ok aof_last_write_status:ok aof_last_cow_size:0 aof_current_size:40803 aof_base_size:0 aof_pending_rewrite:0 aof_buffer_length:0 aof_rewrite_buffer_length:0 aof_pending_bio_fsync:0 aof_delayed_fsync:0 # Stats total_connections_received:12 total_commands_processed:1019 instantaneous_ops_per_sec:0 total_net_input_bytes:41255 total_net_output_bytes:46481 instantaneous_input_kbps:0.00 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 expired_stale_perc:0.00 expired_time_cap_reached_count:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:523 migrate_cached_sockets:0 slave_expires_tracked_keys:0 active_defrag_hits:0 active_defrag_misses:0 active_defrag_key_hits:0 active_defrag_key_misses:0 # Replication role:master connected_slaves:0 master_replid:518de7012facc8a7fe780ed4eb5f4de015f01d56 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 # CPU used_cpu_sys:0.365552 used_cpu_user:0.570244 used_cpu_sys_children:0.000000 used_cpu_user_children:0.001215 # Cluster cluster_enabled:0 # Keyspace db0:keys=1000,expires=0,avg_ttl=0
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
127.0.0.1:6379> help keys KEYS pattern summary: Find all keys matching the given pattern since: 1.0.0 group: generic #? 占位符 127.0.0.1:6379> keys key_? 1) "key_8" 2) "key_5" 3) "key_4" 4) "key_9" 5) "key_0" 6) "key_2" 7) "key_1" 8) "key_3" 9) "key_6" 10) "key_7" 127.0.0.1:6379> keys key_2? 1) "key_27" 2) "key_28" 3) "key_22" 4) "key_26" 5) "key_21" 6) "key_20" 7) "key_25" 8) "key_23" 9) "key_24" 10) "key_29" #* 通配 #127.0.0.1:6379> keys key_9* 1) "key_995" 2) "key_957" 3) "key_988" 4) "key_98" 5) "key_990" 6) "key_959" 7) "key_949" 8) "key_984" 9) "key_999" 10) "key_950" 11) "key_976" 12) "key_912" 13) "key_991" 14) "key_944" 15) "key_987" 16) "key_954" 17) "key_981" 18) "key_951" 19) "key_998" 20) "key_916" 21) "key_986" 22) "key_933" 23) "key_996" 24) "key_918" 25) "key_942" 26) "key_906" 27) "key_925" 28) "key_965" 29) "key_964" 30) "key_917" 31) "key_9" 32) "key_95" 33) "key_994" 34) "key_94" 35) "key_961" 36) "key_901" 37) "key_962" 38) "key_909" 39) "key_971" 40) "key_966" 41) "key_968" 42) "key_913" 43) "key_960" 44) "key_937" 45) "key_977" 46) "key_90" 47) "key_926" 48) "key_93" 49) "key_955" 50) "key_970" 51) "key_939" 52) "key_908" 53) "key_929" 54) "key_972" 55) "key_902" 56) "key_948" 57) "key_956" 58) "key_930" 59) "key_978" 60) "key_99" 61) "key_924" 62) "key_915" 63) "key_943" 64) "key_932" 65) "key_928" 66) "key_914" 67) "key_982" 68) "key_905" 69) "key_974" 70) "key_979" 71) "key_940" 72) "key_941" 73) "key_958" 74) "key_997" 75) "key_993" 76) "key_922" 77) "key_92" 78) "key_980" 79) "key_934" 80) "key_911" 81) "key_946" 82) "key_923" 83) "key_945" 84) "key_992" 85) "key_969" 86) "key_921" 87) "key_910" 88) "key_936" 89) "key_983" 90) "key_903" 91) "key_989" 92) "key_931" 93) "key_919" 94) "key_953" 95) "key_947" 96) "key_96" 97) "key_935" 98) "key_91" 99) "key_907" 100) "key_952" 101) "key_927" 102) "key_967" 103) "key_904" 104) "key_900" 105) "key_938" 106) "key_973" 107) "key_97" 108) "key_920" 109) "key_985" 110) "key_963" 111) "key_975" #展示当前数据库内的所有key 127.0.0.1:6379> keys * ...... 987) "key_172" 988) "key_64" 989) "key_79" 990) "key_801" 991) "key_502" 992) "key_144" 993) "key_514" 994) "key_615" 995) "key_197" 996) "key_84" 997) "key_681" 998) "key_572" 999) "key_656" 1000) "key_805"
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 “” 对原有命令进行重命名(通常用于对高危命令进行管控)
#在启用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>
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net