经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Redis » 查看文章
【Redis】常用命令介绍
来源:cnblogs  作者:Janzen_Q  时间:2023/5/8 10:21:40  对本文有异议

一、Redis常用基本命令

官方文档:https://redis.io/commands/

参考文档:http://redisdoc.com/

  1. #可以使用help命令查看各redis命令用法
    [root@Client-Ubuntu-1804-250:~]# redis-cli -a redis --no-auth-warning help
  2. redis-cli 5.0.14
  3. To get help about Redis commands type:
  4. "help @<group>" to get a list of commands in <group>
  5. "help <command>" for help on <command>
  6. "help <tab>" to get a list of possible help topics
  7. "quit" to exit
  8. To set redis-cli preferences:
  9. ":set hints" enable online hints
  10. ":set nohints" disable online hints
  11. Set your preferences in ~/.redisclirc
  1. [root@Client-Ubuntu-1804-250:~]# redis-cli -a redis --no-auth-warning
  2. #查看info命令帮助
  3. 127.0.0.1:6379> help info
  4. INFO [section]
  5. summary: Get information and statistics about the server
  6. since: 1.0.0
  7. group: server
  8. #查看set命令帮助
  9. 127.0.0.1:6379> help set
  10. SET key value [expiration EX seconds|PX milliseconds] [NX|XX]
  11. summary: Set the string value of a key
  12. since: 1.0.0
  13. group: string
  14.  
  15. 127.0.0.1:6379>

 

 1、INFO

查看当前节点运行状态

  1. 127.0.0.1:6379> help info
  2. INFO [section]
  3. summary: Get information and statistics about the server
  4. since: 1.0.0
  5. group: server
  6. 127.0.0.1:6379> info
  7. # Server
  8. redis_version:5.0.14
  9. redis_git_sha1:00000000
  10. redis_git_dirty:0
  11. redis_build_id:5d32a2d9ed5f67d5
  12. redis_mode:standalone
  13. os:Linux 4.15.0-210-generic x86_64
  14. arch_bits:64
  15. multiplexing_api:epoll
  16. atomicvar_api:atomic-builtin
  17. gcc_version:7.5.0
  18. process_id:6069
  19. run_id:0e67fcd27ff6ae2589ff90ac2516bd52269f2965
  20. tcp_port:6379
  21. uptime_in_seconds:924
  22. uptime_in_days:0
  23. hz:10
  24. configured_hz:10
  25. lru_clock:5745912
  26. executable:/app/redis/bin/redis-server
  27. config_file:/app/redis/etc/redis_6379.conf
  28. # Clients
  29. connected_clients:1
  30. client_recent_max_input_buffer:2
  31. client_recent_max_output_buffer:0
  32. blocked_clients:0
  33. # Memory
  34. used_memory:943496
  35. used_memory_human:921.38K
  36. used_memory_rss:7237632
  37. used_memory_rss_human:6.90M
  38. used_memory_peak:943496
  39. used_memory_peak_human:921.38K
  40. used_memory_peak_perc:100.11%
  41. used_memory_overhead:898934
  42. used_memory_startup:791488
  43. used_memory_dataset:44562
  44. used_memory_dataset_perc:29.32%
  45. allocator_allocated:1129952
  46. allocator_active:1409024
  47. allocator_resident:14368768
  48. total_system_memory:1006243840
  49. total_system_memory_human:959.63M
  50. used_memory_lua:37888
  51. used_memory_lua_human:37.00K
  52. used_memory_scripts:0
  53. used_memory_scripts_human:0B
  54. number_of_cached_scripts:0
  55. maxmemory:0
  56. maxmemory_human:0B
  57. maxmemory_policy:noeviction
  58. allocator_frag_ratio:1.25
  59. allocator_frag_bytes:279072
  60. allocator_rss_ratio:10.20
  61. allocator_rss_bytes:12959744
  62. rss_overhead_ratio:0.50
  63. rss_overhead_bytes:-7131136
  64. mem_fragmentation_ratio:8.03
  65. mem_fragmentation_bytes:6336128
  66. mem_not_counted_for_evict:1368
  67. mem_replication_backlog:0
  68. mem_clients_slaves:0
  69. mem_clients_normal:49694
  70. mem_aof_buffer:1368
  71. mem_allocator:jemalloc-5.1.0
  72. active_defrag_running:0
  73. lazyfree_pending_objects:0
  74. # Persistence
  75. loading:0
  76. rdb_changes_since_last_save:658
  77. rdb_bgsave_in_progress:0
  78. rdb_last_save_time:1683467492
  79. rdb_last_bgsave_status:ok
  80. rdb_last_bgsave_time_sec:0
  81. rdb_current_bgsave_time_sec:-1
  82. rdb_last_cow_size:233472
  83. aof_enabled:1
  84. aof_rewrite_in_progress:0
  85. aof_rewrite_scheduled:0
  86. aof_last_rewrite_time_sec:-1
  87. aof_current_rewrite_time_sec:-1
  88. aof_last_bgrewrite_status:ok
  89. aof_last_write_status:ok
  90. aof_last_cow_size:0
  91. aof_current_size:40803
  92. aof_base_size:0
  93. aof_pending_rewrite:0
  94. aof_buffer_length:0
  95. aof_rewrite_buffer_length:0
  96. aof_pending_bio_fsync:0
  97. aof_delayed_fsync:0
  98. # Stats
  99. total_connections_received:12
  100. total_commands_processed:1019
  101. instantaneous_ops_per_sec:0
  102. total_net_input_bytes:41255
  103. total_net_output_bytes:46481
  104. instantaneous_input_kbps:0.00
  105. instantaneous_output_kbps:0.00
  106. rejected_connections:0
  107. sync_full:0
  108. sync_partial_ok:0
  109. sync_partial_err:0
  110. expired_keys:0
  111. expired_stale_perc:0.00
  112. expired_time_cap_reached_count:0
  113. evicted_keys:0
  114. keyspace_hits:0
  115. keyspace_misses:0
  116. pubsub_channels:0
  117. pubsub_patterns:0
  118. latest_fork_usec:523
  119. migrate_cached_sockets:0
  120. slave_expires_tracked_keys:0
  121. active_defrag_hits:0
  122. active_defrag_misses:0
  123. active_defrag_key_hits:0
  124. active_defrag_key_misses:0
  125. # Replication
  126. role:master
  127. connected_slaves:0
  128. master_replid:518de7012facc8a7fe780ed4eb5f4de015f01d56
  129. master_replid2:0000000000000000000000000000000000000000
  130. master_repl_offset:0
  131. second_repl_offset:-1
  132. repl_backlog_active:0
  133. repl_backlog_size:1048576
  134. repl_backlog_first_byte_offset:0
  135. repl_backlog_histlen:0
  136. # CPU
  137. used_cpu_sys:0.365552
  138. used_cpu_user:0.570244
  139. used_cpu_sys_children:0.000000
  140. used_cpu_user_children:0.001215
  141. # Cluster
  142. cluster_enabled:0
  143. # Keyspace
  144. db0:keys=1000,expires=0,avg_ttl=0
info

可以在INFO 后补充筛选内容

  1. #查看当前节点CPU信息
  2. 127.0.0.1:6379> info CPU
  3. # CPU
  4. used_cpu_sys:0.508881
  5. used_cpu_user:0.848135
  6. used_cpu_sys_children:0.000000
  7. used_cpu_user_children:0.003046
  8. #查看当前节点Server信息
  9. 127.0.0.1:6379> info Server
  10. # Server
  11. redis_version:5.0.14
  12. redis_git_sha1:00000000
  13. redis_git_dirty:0
  14. redis_build_id:5d32a2d9ed5f67d5
  15. redis_mode:standalone
  16. os:Linux 4.15.0-210-generic x86_64
  17. arch_bits:64
  18. multiplexing_api:epoll
  19. atomicvar_api:atomic-builtin
  20. gcc_version:7.5.0
  21. process_id:6069
  22. run_id:0e67fcd27ff6ae2589ff90ac2516bd52269f2965
  23. tcp_port:6379
  24. uptime_in_seconds:1358
  25. uptime_in_days:0
  26. hz:10
  27. configured_hz:10
  28. lru_clock:5746346
  29. executable:/app/redis/bin/redis-server
  30. config_file:/app/redis/etc/redis_6379.conf

 

 2、SELECT

切换数据库,类似于MySQL是 USE DATABASES;

在Cluster模式下不支持多DB模式,会出现以下错误提示

  1. 10.0.0.20:6379> info cluster
  2. # Cluster
  3. cluster_enabled:1
  4. 10.0.0.20:6379> select 1
  5. (error) ERR SELECT is not allowed in cluster mode
  1. 127.0.0.1:6379> help select
  2. SELECT index
  3. summary: Change the selected database for the current connection
  4. since: 1.0.0
  5. group: connection
  6. 127.0.0.1:6379> select 1
  7. OK
  8. 127.0.0.1:6379[1]> select 2
  9. OK
  10. 127.0.0.1:6379[2]> select 3
  11. OK
  12. 127.0.0.1:6379[3]>
select

 

 3、KEYS

查看当前数据库下所有的key(数据量大时会对数据库造成巨大压力,此命令慎用)

此命令仅查询当前db下的数据

 

  1. 127.0.0.1:6379> select 1
  2. OK
  3. 127.0.0.1:6379[1]> keys *
  4. (empty list or set)
  5. 127.0.0.1:6379[1]> info KeySpace
  6. # Keyspace
  7. db0:keys=1000,expires=0,avg_ttl=0

 

  1. 127.0.0.1:6379> help keys
  2. KEYS pattern
  3. summary: Find all keys matching the given pattern
  4. since: 1.0.0
  5. group: generic
  6. #? 占位符
  7. 127.0.0.1:6379> keys key_?
  8. 1) "key_8"
  9. 2) "key_5"
  10. 3) "key_4"
  11. 4) "key_9"
  12. 5) "key_0"
  13. 6) "key_2"
  14. 7) "key_1"
  15. 8) "key_3"
  16. 9) "key_6"
  17. 10) "key_7"
  18. 127.0.0.1:6379> keys key_2?
  19. 1) "key_27"
  20. 2) "key_28"
  21. 3) "key_22"
  22. 4) "key_26"
  23. 5) "key_21"
  24. 6) "key_20"
  25. 7) "key_25"
  26. 8) "key_23"
  27. 9) "key_24"
  28. 10) "key_29"
  29. #* 通配
  30. #127.0.0.1:6379> keys key_9*
  31. 1) "key_995"
  32. 2) "key_957"
  33. 3) "key_988"
  34. 4) "key_98"
  35. 5) "key_990"
  36. 6) "key_959"
  37. 7) "key_949"
  38. 8) "key_984"
  39. 9) "key_999"
  40. 10) "key_950"
  41. 11) "key_976"
  42. 12) "key_912"
  43. 13) "key_991"
  44. 14) "key_944"
  45. 15) "key_987"
  46. 16) "key_954"
  47. 17) "key_981"
  48. 18) "key_951"
  49. 19) "key_998"
  50. 20) "key_916"
  51. 21) "key_986"
  52. 22) "key_933"
  53. 23) "key_996"
  54. 24) "key_918"
  55. 25) "key_942"
  56. 26) "key_906"
  57. 27) "key_925"
  58. 28) "key_965"
  59. 29) "key_964"
  60. 30) "key_917"
  61. 31) "key_9"
  62. 32) "key_95"
  63. 33) "key_994"
  64. 34) "key_94"
  65. 35) "key_961"
  66. 36) "key_901"
  67. 37) "key_962"
  68. 38) "key_909"
  69. 39) "key_971"
  70. 40) "key_966"
  71. 41) "key_968"
  72. 42) "key_913"
  73. 43) "key_960"
  74. 44) "key_937"
  75. 45) "key_977"
  76. 46) "key_90"
  77. 47) "key_926"
  78. 48) "key_93"
  79. 49) "key_955"
  80. 50) "key_970"
  81. 51) "key_939"
  82. 52) "key_908"
  83. 53) "key_929"
  84. 54) "key_972"
  85. 55) "key_902"
  86. 56) "key_948"
  87. 57) "key_956"
  88. 58) "key_930"
  89. 59) "key_978"
  90. 60) "key_99"
  91. 61) "key_924"
  92. 62) "key_915"
  93. 63) "key_943"
  94. 64) "key_932"
  95. 65) "key_928"
  96. 66) "key_914"
  97. 67) "key_982"
  98. 68) "key_905"
  99. 69) "key_974"
  100. 70) "key_979"
  101. 71) "key_940"
  102. 72) "key_941"
  103. 73) "key_958"
  104. 74) "key_997"
  105. 75) "key_993"
  106. 76) "key_922"
  107. 77) "key_92"
  108. 78) "key_980"
  109. 79) "key_934"
  110. 80) "key_911"
  111. 81) "key_946"
  112. 82) "key_923"
  113. 83) "key_945"
  114. 84) "key_992"
  115. 85) "key_969"
  116. 86) "key_921"
  117. 87) "key_910"
  118. 88) "key_936"
  119. 89) "key_983"
  120. 90) "key_903"
  121. 91) "key_989"
  122. 92) "key_931"
  123. 93) "key_919"
  124. 94) "key_953"
  125. 95) "key_947"
  126. 96) "key_96"
  127. 97) "key_935"
  128. 98) "key_91"
  129. 99) "key_907"
  130. 100) "key_952"
  131. 101) "key_927"
  132. 102) "key_967"
  133. 103) "key_904"
  134. 104) "key_900"
  135. 105) "key_938"
  136. 106) "key_973"
  137. 107) "key_97"
  138. 108) "key_920"
  139. 109) "key_985"
  140. 110) "key_963"
  141. 111) "key_975"
  142. #展示当前数据库内的所有key
  143. 127.0.0.1:6379> keys *
  144. ......
  145. 987) "key_172"
  146. 988) "key_64"
  147. 989) "key_79"
  148. 990) "key_801"
  149. 991) "key_502"
  150. 992) "key_144"
  151. 993) "key_514"
  152. 994) "key_615"
  153. 995) "key_197"
  154. 996) "key_84"
  155. 997) "key_681"
  156. 998) "key_572"
  157. 999) "key_656"
  158. 1000) "key_805"

 

 4、BGSAVE

手动触发后台执行save操作

  1. 127.0.0.1:6379>
  2. 127.0.0.1:6379> help bgsave
  3. BGSAVE -
  4. summary: Asynchronously save the dataset to disk
  5. since: 1.0.0
  6. group: server
  7. 127.0.0.1:6379> bgsave
  8. Background saving started

 

 5、DBSIZE

返回当前数据库下的所有key数量

  1. 127.0.0.1:6379> help dbsize
  2. DBSIZE -
  3. summary: Return the number of keys in the selected database
  4. since: 1.0.0
  5. group: server
  6. 127.0.0.1:6379> dbsize
  7. (integer) 1000
  8. 127.0.0.1:6379> select 1
  9. OK
  10. 127.0.0.1:6379[1]> dbsize
  11. (integer) 0

 

 6、FLUSHDB

强制清空当前数据库中的所有key,不会影响其他db中的数据,谨慎使用

  1. 127.0.0.1:6379> help flushdb
  2. FLUSHDB [ASYNC]
  3. summary: Remove all keys from the current database
  4. since: 1.0.0
  5. group: server
  6. 127.0.0.1:6379[1]> info KeySpace
  7. # Keyspace
  8. db0:keys=1000,expires=0,avg_ttl=0
  9. db1:keys=4,expires=0,avg_ttl=0
  10. #清空db1中的所有键值,不会影响到db0中的数据
  11. 127.0.0.1:6379[1]> flushdb
  12. OK
  13. 127.0.0.1:6379[1]> info KeySpace
  14. # Keyspace
  15. db0:keys=1000,expires=0,avg_ttl=0
  16. 127.0.0.1:6379[1]>

 

 7、FLUSHALL

强制清空当前redis节点上所有数据库中的所有key,即删除当前节点所有数据,谨慎使用,必要情况建议禁用此命令

  1. 127.0.0.1:6379> info KeySpace
  2. # Keyspace
  3. db0:keys=1000,expires=0,avg_ttl=0
  4. db1:keys=4,expires=0,avg_ttl=0
  5. 127.0.0.1:6379> flushall
  6. OK
  7. 127.0.0.1:6379> info KeySpace
  8. # Keyspace
  9. 127.0.0.1:6379>

  

 8、SHUTDOWN

时间复杂度:O(N) N为需要保存的数据库键数量

SHUTDOWN执行过程:

  • 停止所有客户端连接
  • 如果至少有一个保存节点在等待,执行SAVE操作
  • 如果AOF开启,更新AOF内容
  • 关闭 Redis Server 

如果开启了持久化配置,SHUTDOWN命令可以保障服务器正常关闭而数据不丢失

如果单纯执行SAVE之后,执行 QUIT 命令,则无法保证数据不丢失,SAVE执行完成后,执行 QUIT 命令的过程中,服务器可能依旧存在与客户端的访问连接,会造成这期间数据的丢失。

  1. 127.0.0.1:6379> help shutdown
  2. SHUTDOWN [NOSAVE|SAVE]
  3. summary: Synchronously save the dataset to disk and then shut down the server
  4. since: 1.0.0
  5. group: server
  6. 127.0.0.1:6379> shutdown
  7. not connected>
  8. not connected>
  9. not connected> exit
  10. [root@Client-Ubuntu-1804-250:~]# ps -aux | grep redis | grep ^grep
  11. [root@Client-Ubuntu-1804-250:~]# ss -ntlp
  12. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  13. LISTEN 0 128 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=867,fd=13))
  14. LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=978,fd=3))
  15. LISTEN 0 128 127.0.0.1:6010 0.0.0.0:* users:(("sshd",pid=1661,fd=10))
  16. LISTEN 0 128 127.0.0.1:6011 0.0.0.0:* users:(("sshd",pid=1661,fd=15))
  17. LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=978,fd=4))
  18. LISTEN 0 128 [::1]:6010 [::]:* users:(("sshd",pid=1661,fd=9))
  19. LISTEN 0 128 [::1]:6011 [::]:* users:(("sshd",pid=1661,fd=12))

 

禁用Redis命令(rename配置)

可通过配置文件中的 rename-comand <command> "" 对原有命令进行重命名(通常用于对高危命令进行管控)

 

  1. #在启用aof情况下,禁用或重命名命令可能会导致redis服务无法启动,是因为实例在之前执行被重命名的命令,导致加载aof时,命令执行失败
    6682
    :C 07 May 2023 23:20:57.411 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  2. 6682:C 07 May 2023 23:20:57.411 # Redis version=5.0.14, bits=64, commit=00000000, modified=0, pid=6682, just started
  3. 6682:C 07 May 2023 23:20:57.411 # Configuration loaded
  4. 6682:C 07 May 2023 23:20:57.411 * supervised by systemd, will signal readiness
  5. 6682:M 07 May 2023 23:20:57.417 * Running mode=standalone, port=6379.
  6. 6682:M 07 May 2023 23:20:57.419 # Server initialized
  7. 6682:M 07 May 2023 23:20:57.420 # Unknown command 'flushdb' reading the append only file

 

解决方案:

1、禁用aof选项

2、修改配置重启服务之前,执行 bgrewriteaof 重新生成aof内容

 

  1. # Command renaming.
  2. #
  3. # It is possible to change the name of dangerous commands in a shared
  4. # environment. For instance the CONFIG command may be renamed into something
  5. # hard to guess so that it will still be available for internal-use tools
  6. # but not available for general clients.
  7. #
  8. # Example:
  9. #
  10. # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
  11. #
  12. # It is also possible to completely kill a command by renaming it into
  13. # an empty string:
  14. #
  15. # rename-command CONFIG ""
  16. #
  17. # Please note that changing the name of commands that are logged into the
  18. # AOF file or transmitted to replicas may cause problems.
  19. renmae-comand FLUSHALL ""
  20. rename-comand FULSHDB "REMOVE-THIS-DATABASE"
  1. 127.0.0.1:6379> info keySpace
  2. # Keyspace
  3. db0:keys=1000,expires=0,avg_ttl=0
  4. 127.0.0.1:6379> flushall
  5. (error) ERR unknown command `flushall`, with args beginning with:
  6. 127.0.0.1:6379> flushdb
  7. (error) ERR unknown command `flushdb`, with args beginning with:
  8. 127.0.0.1:6379> info keySpace
  9. # Keyspace
  10. db0:keys=1000,expires=0,avg_ttl=0
  11. 127.0.0.1:6379>
  12. 127.0.0.1:6379> REMOVE-THIS-DATABASE
  13. OK
  14. 127.0.0.1:6379> info keySpace
  15. # Keyspace
  16. 127.0.0.1:6379>

 

原文链接:https://www.cnblogs.com/Janzen-q/p/17320421.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号