> For the complete documentation index, see [llms.txt](https://redis.sai.show/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://redis.sai.show/di-4-zhang-shi-jian-redis-fu-wu-qi-he-ke-hu-duan-de-cao-zuo/4.2-cha-kan-redis-fu-wu-qi-de-xiang-xi-xin-xi.md).

# 4.2 查看Redis服务器的详细信息

## 4.2.1 通过`INFO`命令查看服务器信息

* `INFO`
  * 功能:查看服务端相关信息

例:

```
127.0.0.1:6379> INFO
# Server
redis_version:6.2.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:b61f37314a089f19
redis_mode:standalone
os:Linux 5.15.49-linuxkit x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:10.2.1
process_id:1
process_supervised:no
run_id:ec3f7b5d6eda8fa1c55555f82ede6dfd23e9a50c
tcp_port:6379
server_time_usec:1691911379981145
uptime_in_seconds:300
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:14189779
executable:/data/redis-server
config_file:
io_threads_active:0

# Clients
connected_clients:1
cluster_connections:0
maxclients:10000
client_recent_max_input_buffer:24
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0

# Memory
used_memory:873648
used_memory_human:853.17K
used_memory_rss:7856128
used_memory_rss_human:7.49M
used_memory_peak:933848
used_memory_peak_human:911.96K
used_memory_peak_perc:93.55%
used_memory_overhead:830384
used_memory_startup:809880
used_memory_dataset:43264
used_memory_dataset_perc:67.85%
allocator_allocated:1071104
allocator_active:1290240
allocator_resident:3805184
total_system_memory:8346099712
total_system_memory_human:7.77G
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.20
allocator_frag_bytes:219136
allocator_rss_ratio:2.95
allocator_rss_bytes:2514944
rss_overhead_ratio:2.06
rss_overhead_bytes:4050944
mem_fragmentation_ratio:9.46
mem_fragmentation_bytes:7025240
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:20504
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
lazyfreed_objects:0

# Persistence
loading:0
current_cow_size:0
current_cow_size_age:0
current_fork_perc:0.00
current_save_keys_processed:0
current_save_keys_total:0
rdb_changes_since_last_save:20
rdb_bgsave_in_progress:0
rdb_last_save_time:1691911079
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
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
module_fork_in_progress:0
module_fork_last_cow_size:0

# Stats
total_connections_received:1
total_commands_processed:3
instantaneous_ops_per_sec:0
total_net_input_bytes:74
total_net_output_bytes:20555
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
expire_cycle_cpu_milliseconds:5
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
total_forks:0
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
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_error_replies:0
dump_payload_sanitizations:0
total_reads_processed:4
total_writes_processed:3
io_threaded_reads_processed:0
io_threaded_writes_processed:0

# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:557e052fd9196b70141b0e18a6c609dfceb06773
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.487641
used_cpu_user:0.287673
used_cpu_sys_children:0.003459
used_cpu_user_children:0.001504
used_cpu_sys_main_thread:0.485370
used_cpu_user_main_thread:0.284587

# Modules

# Errorstats

# Cluster
cluster_enabled:0

# Keyspace
```

|      属性     |               含义              |
| :---------: | :---------------------------: |
|    Server   |            服务端相关信息            |
|   Clients   |           已连接的客户端信息           |
|    Memory   |         Redis服务端内存相关信息        |
| Persistence |            持久化相关信息            |
|    Stats    |          和服务端相关的统计信息          |
| Replication |            主从复制相关信息           |
|     CPU     |        服务端所在机器的CPU相关信息        |
|   Cluster   |          Redis集群相关信息          |
|   Keyspace  | 和Redis数据库相关的统计信息.比如键的数量和超时时间等 |

## 4.2.2 查看客户端连接状况

* `INFO CLIENTS`
  * 功能:查看客户端的连接情况

例:

```
127.0.0.1:6379> INFO CLIENTS
# Clients
connected_clients:1
cluster_connections:0
maxclients:10000
client_recent_max_input_buffer:16
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0
```

其中:

* `connected_clients`:正在连接的客户端数量
* `maxclients`:服务端允许的最大连接数

## 4.2.3 观察最大连接数

* `INFO STATS`
  * 功能:查看服务端运行情况

[INFO STATS命令详解](https://blog.csdn.net/HiJamesChen/article/details/108998197)

例:

```
127.0.0.1:6379> INFO STATS
# Stats
total_connections_received:1
total_commands_processed:6
instantaneous_ops_per_sec:0
total_net_input_bytes:150
total_net_output_bytes:24839
instantaneous_input_kbps:0.01
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
expire_cycle_cpu_milliseconds:12
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
total_forks:0
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
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_error_replies:0
dump_payload_sanitizations:0
total_reads_processed:7
total_writes_processed:6
io_threaded_reads_processed:0
io_threaded_writes_processed:0
```

其中:

* `rejected_connections`:因超过最大连接数而被拒绝的客户端连接次数

查看最大连接数:

```
127.0.0.1:6379> CONFIG GET maxclients
1) "maxclients"
2) "10000"
```

可以看到,一般情况下是不会出现`rejected_connections`大于0的情况

## 4.2.4 查看每秒执行多少条指令

在`INFO STATS`命令的返回值中,有一项名为`instantaneous_ops_per_sec`.

* `instantaneous_ops_per_sec`:表示当前每秒执行的指令数量

若集群中某台Redis服务器中该数值过大或过小,就需要观察负载均衡的相关配置了.或者当数据库压力较大而通过该命令发现作为缓存的Redis服务器执行的指令过少时,就需要调整缓存策略

## 4.2.5 观察内存用量

* `INFO MEMORY`
  * 功能:观察当前Redis服务器的内存使用情况

由于Redis是在内存中缓存数据,如果缓存数据太多,或者大量键没有设置过期时间(expired time),就会造成内存使用过大,从而导致OOM问题.此时需要通过`INFO MEMORY`命令观察Redis服务器的内存使用情况.

```
127.0.0.1:6379> INFO MEMORY
# Memory
used_memory:871904
used_memory_human:851.47K
used_memory_rss:7839744
used_memory_rss_human:7.48M
used_memory_peak:933848
used_memory_peak_human:911.96K
used_memory_peak_perc:93.37%
used_memory_overhead:830392
used_memory_startup:809880
used_memory_dataset:41512
used_memory_dataset_perc:66.93%
allocator_allocated:1133568
allocator_active:1368064
allocator_resident:3883008
total_system_memory:8346099712
total_system_memory_human:7.77G
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.21
allocator_frag_bytes:234496
allocator_rss_ratio:2.84
allocator_rss_bytes:2514944
rss_overhead_ratio:2.02
rss_overhead_bytes:3956736
mem_fragmentation_ratio:9.44
mem_fragmentation_bytes:7008848
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:20512
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
lazyfreed_objects:0
```

其中:

* `used_memory_human`:以人类可读形式展示Redis已使用的内存
* `used_memory_peak_human`:以人类可读形式展示Redis使用的内存峰值
* `used_memory_lua_human`:以人类可读形式展示LUA脚本占用的内存大小
* `used_memory_scripts_human`:以人类可读形式展示脚本占用的内存大小
* `mem_clients_slaves`:因客户端主从复制而使用的内存大小

## 4.2.6 通过`COMMAND`命令查看Redis命令

* `COMMAND`
  * 功能:返回Redis的命令信息

例:

```
127.0.0.1:6379> COMMAND
  1) 1) "psubscribe"
     2) (integer) -2
     3) 1) pubsub
        2) noscript
        3) loading
        4) stale
     4) (integer) 0
     5) (integer) 0
     6) (integer) 0
     7) 1) @pubsub
        2) @slow
  2) 1) "lpos"
     2) (integer) -3
     3) 1) readonly
     4) (integer) 1
     5) (integer) 1
     6) (integer) 1
     7) 1) @read
        2) @list
        3) @slow
  3) 1) "rpushx"
     2) (integer) -3
     3) 1) write
        2) denyoom
        3) fast
     4) (integer) 1
     5) (integer) 1
     6) (integer) 1
     7) 1) @write
        2) @list
        3) @fast
  4) 1) "setbit"
     2) (integer) 4
     3) 1) write
        2) denyoom
     4) (integer) 1
     5) (integer) 1
     6) (integer) 1
     7) 1) @write
        2) @bitmap
        3) @slow
  5) 1) "georadius_ro"
     2) (integer) -6
     3) 1) readonly
     4) (integer) 1
     5) (integer) 1
     6) (integer) 1
     7) 1) @read
        2) @geo
        3) @slow
  6) 1) "command"
     2) (integer) -1
     3) 1) random
        2) loading
        3) stale
     4) (integer) 0
     5) (integer) 0
     6) (integer) 0
     7) 1) @slow
        2) @connection
  7) 1) "debug"
     2) (integer) -2
     3) 1) admin
        2) noscript
        3) loading
        4) stale
     4) (integer) 0
     5) (integer) 0
     6) (integer) 0
     7) 1) @admin
        2) @slow
        3) @dangerous
  8) 1) "bzpopmin"
     2) (integer) -3
     3) 1) write
        2) noscript
        3) fast
     4) (integer) 1
     5) (integer) -2
     6) (integer) 1
     7) 1) @write
        2) @sortedset
        3) @fast
        4) @blocking
  9) 1) "xrevrange"
     2) (integer) -4
     3) 1) readonly
     4) (integer) 1
     5) (integer) 1
     6) (integer) 1
     7) 1) @read
        2) @stream
        3) @slow
...
223) 1) "hrandfield"
     2) (integer) -2
     3) 1) readonly
        2) random
     4) (integer) 1
     5) (integer) 1
     6) (integer) 1
     7) 1) @read
        2) @hash
        3) @slow
224) 1) "scan"
     2) (integer) -2
     3) 1) readonly
        2) random
     4) (integer) 0
     5) (integer) 0
     6) (integer) 0
     7) 1) @keyspace
        2) @read
        3) @slow
```

## 4.2.7 查看指定Redis命令的信息

* `COMMAND INFO`
  * 语法:`COMMAND INFO key [key ...]`
  * 功能:查看指定命令的详细信息

例:

```
127.0.0.1:6379> COMMAND INFO set
1) 1) "set"
   2) (integer) -3
   3) 1) write
      2) denyoom
   4) (integer) 1
   5) (integer) 1
   6) (integer) 1
   7) 1) @write
      2) @string
      3) @slow
```

```
127.0.0.1:6379> COMMAND INFO get
1) 1) "get"
   2) (integer) 2
   3) 1) readonly
      2) fast
   4) (integer) 1
   5) (integer) 1
   6) (integer) 1
   7) 1) @read
      2) @string
      3) @fast
```

## 4.2.8 获取指定命令的所有键

* `COMMAND GETKEYS`
  * 语法:`COMMAND GETKEYS command`
  * 功能:获取command命令中所有的键

例:

```
127.0.0.1:6379> COMMAND GETKEYS MSET name Peter age 18 score 100
1) "name"
2) "age"
3) "score"
```

`MSET`命令可以设置多个键值对,前边加上`COMMAND GETKEYS`即可获得该命令操作的所有key名

注:`COMMAND GETKEYS`命令后的command部分实际上并没有真正执行

```
127.0.0.1:6379> GET name
(nil)
127.0.0.1:6379> GET age
(nil)
127.0.0.1:6379> GET score
(nil)
```

例:

```
127.0.0.1:6379> COMMAND GETKEYS SET name Mary
1) "name"
```

```
127.0.0.1:6379> GET name
(nil)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://redis.sai.show/di-4-zhang-shi-jian-redis-fu-wu-qi-he-ke-hu-duan-de-cao-zuo/4.2-cha-kan-redis-fu-wu-qi-de-xiang-xi-xin-xi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
