Redis 笔记(16)— info 指令和命令行工具(查看内存、状态、客户端连接数、监控服务器、扫描大key、采样服务器、执行批量命令等)
Info 命令返回關(guān)于 Redis 服務(wù)器的各種信息和統(tǒng)計(jì)數(shù)值。通過(guò)給定可選的參數(shù) section ,可以讓命令只返回某一部分的信息。
1. 顯示模塊
server: 一般Redis服務(wù)器信息,包含以下域:redis_versionredis_git_sha1redis_git_dirtyosarch_bits:架構(gòu)(32 或 64 位)multiplexing_api: Redis 所使用的事件處理機(jī)制gcc_version: 編譯 Redis 時(shí)所使用的 GCC 版本process_id: 服務(wù)器進(jìn)程的 PIDrun_id: Redis 服務(wù)器的隨機(jī)標(biāo)識(shí)符(用于 Sentinel 和集群)tcp_port:uptime_in_seconds: 自 Redis 服務(wù)器啟動(dòng)以來(lái),經(jīng)過(guò)的秒數(shù)uptime_in_days: 自 Redis 服務(wù)器啟動(dòng)以來(lái),經(jīng)過(guò)的天數(shù)lru_clock: 以分鐘為單位進(jìn)行自增的時(shí)鐘,用于 LRU 管理
clients: 已連接客戶端信息,包含以下域:connected_clients: 已連接客戶端的數(shù)量(不包括通過(guò)從屬服務(wù)器連接的客戶端)client_longest_output_list: 當(dāng)前連接的客戶端當(dāng)中,最長(zhǎng)的輸出列表client_longest_input_buf: 當(dāng)前連接的客戶端當(dāng)中,最大輸入緩存blocked_clients: 正在等待阻塞命令(BLPOP、BRPOP、BRPOPLPUSH)的客戶端的數(shù)量
memory: 內(nèi)存信息used_memory: 由 Redis 分配器分配的內(nèi)存總量,以字節(jié)(byte)為單位used_memory_human: 以人類可讀的格式返回 Redis 分配的內(nèi)存總量used_memory_rss: 從操作系統(tǒng)的角度,返回 Redis 已分配的內(nèi)存總量(俗稱常駐集大小)。這個(gè)值和 top 、 ps 等命令的輸出一致。used_memory_peak: Redis 的內(nèi)存消耗峰值(以字節(jié)為單位)used_memory_peak_human: 以人類可讀的格式返回 Redis 的內(nèi)存消耗峰值used_memory_lua: Lua 引擎所使用的內(nèi)存大小(以字節(jié)為單位)mem_fragmentation_ratio: used_memory_rss 和 used_memory 之間的比率mem_allocator: 在編譯時(shí)指定的, Redis 所使用的內(nèi)存分配器。可以是 libc 、 jemalloc 或者 tcmalloc 。
persistence: RDB 和 AOF 的相關(guān)信息stats: 一般統(tǒng)計(jì)信息replication: 主/從復(fù)制信息cpu: CPU 計(jì)算量統(tǒng)計(jì)信息commandstats: Redis 命令統(tǒng)計(jì)信息cluster: Redis 集群信息keyspace: 數(shù)據(jù)庫(kù)相關(guān)的統(tǒng)計(jì)信息
2. 使用示例
info 命令基本語(yǔ)法如下:
redis 127.0.0.1:6379> INFO [section]
info 可以一次性獲取所有的信息,也可以按塊取信息。
# 獲取所有信息
> info
# 獲取內(nèi)存相關(guān)信息
> info memory
# 獲取復(fù)制相關(guān)信息
> info replication
2.1 獲取所有信息
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 4.4.0-117-generic 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:931c8be0f80eb6ed2e3d787d3e8c324bb536adba
tcp_port:6379
server_time_usec:1640706998540791
uptime_in_seconds:400892
uptime_in_days:4
hz:10
configured_hz:10
lru_clock:13317046
executable:/data/redis-server
config_file:
io_threads_active:0# Clients
connected_clients:2 # 這個(gè)就是正在連接的客戶端數(shù)量
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:895312
used_memory_human:874.33K
used_memory_rss:8028160
used_memory_rss_human:7.66M
used_memory_peak:953480
used_memory_peak_human:931.13K
used_memory_peak_perc:93.90%
used_memory_overhead:851480
used_memory_startup:809880
used_memory_dataset:43832
used_memory_dataset_perc:51.31%
allocator_allocated:938320
allocator_active:1196032
allocator_resident:3428352
total_system_memory:2097278976
total_system_memory_human:1.95G
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.27
allocator_frag_bytes:257712
allocator_rss_ratio:2.87
allocator_rss_bytes:2232320
rss_overhead_ratio:2.34
rss_overhead_bytes:4599808
mem_fragmentation_ratio:9.42
mem_fragmentation_bytes:7175616
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:41000
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:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1640505723
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:192512
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:8
total_commands_processed:84
instantaneous_ops_per_sec:0
total_net_input_bytes:2508
total_net_output_bytes:124338
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:1
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:8174
evicted_keys:0
keyspace_hits:14
keyspace_misses:6
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:315
total_forks:7
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:14
dump_payload_sanitizations:0
total_reads_processed:123
total_writes_processed:105
io_threaded_reads_processed:0
io_threaded_writes_processed:0# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:9361c697e9805a63571591ac1e01eb07d8fec1d1
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:344.796000
used_cpu_user:310.832000
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000
used_cpu_sys_main_thread:344.788000
used_cpu_user_main_thread:310.832000# Modules# Errorstats
errorstat_ERR:count=14# Cluster
cluster_enabled:0# Keyspace
db0:keys=9,expires=0,avg_ttl=0
db5:keys=2,expires=0,avg_ttl=0
127.0.0.1:6379>
2.2 獲取內(nèi)存信息
127.0.0.1:6379> info memory
# Memory
used_memory:893568
used_memory_human:872.62K
used_memory_rss:8024064
used_memory_rss_human:7.65M
used_memory_peak:953480
used_memory_peak_human:931.13K
used_memory_peak_perc:93.72%
used_memory_overhead:851480
used_memory_startup:809880
used_memory_dataset:42088
used_memory_dataset_perc:50.29%
allocator_allocated:1089360
allocator_active:1355776
allocator_resident:3588096
total_system_memory:2097278976
total_system_memory_human:1.95G
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.24
allocator_frag_bytes:266416
allocator_rss_ratio:2.65
allocator_rss_bytes:2232320
rss_overhead_ratio:2.24
rss_overhead_bytes:4435968
mem_fragmentation_ratio:9.41
mem_fragmentation_bytes:7171520
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:41000
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
lazyfreed_objects:0
127.0.0.1:6379>
2.3 查詢每秒執(zhí)行指令數(shù)
> ./redis-cli info stats |grep ops
instantaneous_ops_per_sec:234
ops_per_sec: operations per second,也就是每秒操作數(shù)。
也就是所有客戶端每秒會(huì)發(fā)送 234 條指令到服務(wù)器執(zhí)行。
?
2.4 客戶端連接數(shù)量
這個(gè)信息在 Clients 塊里,可以通過(guò) info clients 看到。
connected_clients:124 # 這個(gè)就是正在連接的客戶端數(shù)量
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
這個(gè)信息也是比較有用的,通過(guò)觀察這個(gè)數(shù)量可以確定是否存在意料之外的連接。如果發(fā)現(xiàn)這個(gè)數(shù)量不對(duì)勁,接著就可以使用 client list指令列出所有的客戶端鏈接地址來(lái)確定源頭。
關(guān)于客戶端的數(shù)量還有個(gè)重要的參數(shù)需要觀察,那就是 rejected_connections,它表示因?yàn)槌鲎畲筮B接數(shù)限制而被拒絕的客戶端連接次數(shù),如果這個(gè)數(shù)字很大,意味著服務(wù)器的最大連接數(shù)設(shè)置的過(guò)低需要調(diào)整 maxclients 參數(shù)。
redis-cli info stats |grep reject
rejected_connections:0
3. 命令行工具
3.1 執(zhí)行單條命令
通過(guò)將命令參數(shù)直接傳遞給 redis-cli 來(lái)執(zhí)行指令并獲取輸出結(jié)果。
$ redis-cli incrby a 1
(integer) 1
$ redis-cli incrby a 1
(integer) 2
如果輸出的內(nèi)容較大,還可以將輸出重定向到外部文件
$ redis-cli info > info.txt
$ wc -l info.txt120 info.txt
上面的命令指向的服務(wù)器是默認(rèn)服務(wù)器地址,如果想指向特定的服務(wù)器可以這樣
// -n 2 表示使用第2個(gè)庫(kù),相當(dāng)于 select 2
$ redis-cli -h localhost -p 6379 -n 2 ping
PONG
3.2 執(zhí)行批量命令
直接使用 redis-cli 來(lái)批量執(zhí)行一系列指令。
$ cat cmds.txt
set foo1 bar1
set foo2 bar2
set foo3 bar3
......
$ cat cmds.txt | redis-cli
OK
OK
OK
...
上面的指令使用了 Unix 管道將 cat 指令的標(biāo)準(zhǔn)輸出連接到 redis-cli 的標(biāo)準(zhǔn)輸入。其實(shí)還可以直接使用輸入重定向來(lái)批量執(zhí)行指令。
$ redis-cli < cmds.txt
OK
OK
OK
...
3.3 重復(fù)執(zhí)行指令
redis-cli 還支持重復(fù)執(zhí)行指令多次,每條指令執(zhí)行之間設(shè)置一個(gè)間隔時(shí)間,如此便可以觀察某條指令的輸出內(nèi)容隨時(shí)間變化。
// 間隔1s,執(zhí)行5次,觀察qps的變化
$ redis-cli -r 5 -i 1 info | grep ops
instantaneous_ops_per_sec:43469
instantaneous_ops_per_sec:47460
instantaneous_ops_per_sec:47699
instantaneous_ops_per_sec:46434
instantaneous_ops_per_sec:47216
如果將次數(shù)設(shè)置為 -1 那就是重復(fù)無(wú)數(shù)次永遠(yuǎn)執(zhí)行下去。如果不提供 -i 參數(shù),那就沒(méi)有間隔,連續(xù)重復(fù)執(zhí)行。在交互模式下也可以重復(fù)執(zhí)行指令,形式上比較怪異,在指令前面增加次數(shù)
127.0.0.1:6379> 5 ping
PONG
PONG
PONG
PONG
PONG
3.4 監(jiān)控服務(wù)器狀態(tài)
我們可以使用 --stat 參數(shù)來(lái)實(shí)時(shí)監(jiān)控服務(wù)器的狀態(tài),間隔 1s 實(shí)時(shí)輸出一次。
$ redis-cli --stat
------- data ------ --------------------- load -------------------- - child -
keys mem clients blocked requests connections
2 6.66M 100 0 11591628 (+0) 335
2 6.66M 100 0 11653169 (+61541) 335
2 6.66M 100 0 11706550 (+53381) 335
2 6.54M 100 0 11758831 (+52281) 335
2 6.66M 100 0 11803132 (+44301) 335
2 6.66M 100 0 11854183 (+51051) 335
如果覺(jué)得間隔太長(zhǎng)或是太短,可以使用 -i 參數(shù)調(diào)整輸出間隔。
3.5 掃描大 KEY
每次遇到 Redis 偶然卡頓問(wèn)題,第一個(gè)想到的就是實(shí)例中是否存在大 KEY,大 KEY的內(nèi)存擴(kuò)容以及釋放都會(huì)導(dǎo)致主線程卡頓。如果知道里面有沒(méi)有大 KEY,可以自己寫(xiě)程序掃描,不過(guò)這太繁瑣了。redis-cli 提供了 --bigkeys 參數(shù)可以很快掃出內(nèi)存里的大 KEY,使用 -i 參數(shù)控制掃描間隔,避免掃描指令導(dǎo)致服務(wù)器的 ops 陡增報(bào)警。
$ ./redis-cli --bigkeys -i 0.01
# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).[00.00%] Biggest zset found so far 'hist:aht:main:async_finish:20180425:17' with 1440 members
[00.00%] Biggest zset found so far 'hist:qps:async:authorize:20170311:27' with 2465 members
[00.00%] Biggest hash found so far 'job:counters:6ya9ypu6ckcl' with 3 fields
[00.01%] Biggest string found so far 'rt:aht:main:device_online:68:{-4}' with 4 bytes
[00.01%] Biggest zset found so far 'machine:load:20180709' with 2879 members
[00.02%] Biggest string found so far '6y6fze8kj7cy:{-7}' with 90 bytes
redis-cli 對(duì)于每一種對(duì)象類型都會(huì)記錄長(zhǎng)度最大的 KEY,對(duì)于每一種對(duì)象類型,刷新一次最高記錄就會(huì)立即輸出一次。它能保證輸出長(zhǎng)度為 Top1 的 KEY,但是 Top2、Top3等 KEY 是無(wú)法保證可以掃描出來(lái)的。一般的處理方法是多掃描幾次,或者是消滅了 Top1 的 KEY 之后再掃描確認(rèn)還有沒(méi)有次大的 KEY。
3.6 采樣服務(wù)器指令
線上 Redis 服務(wù)器的 OPS 太高,有很多業(yè)務(wù)模塊都在使用這個(gè) Redis,如何才能判斷出來(lái)是哪個(gè)業(yè)務(wù)導(dǎo)致了 OPS 異常的高。這時(shí)可以對(duì)線上服務(wù)器的指令進(jìn)行采樣,觀察采樣的指令大致就可以分析出 OPS 占比高的業(yè)務(wù)點(diǎn)。這時(shí)就要使用 monitor 指令,它會(huì)將服務(wù)器瞬間執(zhí)行的指令全部顯示出來(lái)。不過(guò)使用的時(shí)候要注意即使使用 ctrl+c 中斷,否則你的顯示器太多的指令瞬間讓你眼花繚亂。
$ redis-cli --host 192.168.x.x --port 6379 monitor
1539853410.458483 [0 10.100.90.62:34365] "GET" "6yax3eb6etq8:{-7}"
1539853410.459212 [0 10.100.90.61:56659] "PFADD" "growth:dau:20181018" "2klxkimass8w"
1539853410.462938 [0 10.100.90.62:20681] "GET" "6yax3eb6etq8:{-7}"
1539853410.467231 [0 10.100.90.61:40277] "PFADD" "growth:dau:20181018" "2kei0to86ps1"
1539853410.470319 [0 10.100.90.62:34365] "GET" "6yax3eb6etq8:{-7}"
1539853410.473927 [0 10.100.90.61:58128] "GET" "6yax3eb6etq8:{-7}"
1539853410.475712 [0 10.100.90.61:40277] "PFADD" "growth:dau:20181018" "2km8sqhlefpc"
1539853410.477053 [0 10.100.90.62:61292] "GET" "6yax3eb6etq8:{-7}"
3.7 診斷服務(wù)器時(shí)延
平時(shí)我們?cè)\斷兩臺(tái)機(jī)器的時(shí)延一般是使用 Unix 的 ping 指令。Redis 也提供了時(shí)延診斷指令,不過(guò)它的原理不太一樣,它是診斷當(dāng)前機(jī)器和 Redis 服務(wù)器之間的指令(PING指令)時(shí)延,它不僅僅是物理網(wǎng)絡(luò)的時(shí)延,還和當(dāng)前的 Redis 主線程是否忙碌有關(guān)。如果你發(fā)現(xiàn) Unix 的 ping 指令時(shí)延很小,而 Redis 的時(shí)延很大,那說(shuō)明 Redis 服務(wù)器在執(zhí)行指令時(shí)有微弱卡頓。
$ redis-cli --host 192.168.x.x --port 6379 --latency
min: 0, max: 5, avg: 0.08 (305 samples)
時(shí)延單位是 ms。
3.8 遠(yuǎn)程 rdb 備份
執(zhí)行下面的命令就可以將遠(yuǎn)程的 Redis 實(shí)例備份到本地機(jī)器,遠(yuǎn)程服務(wù)器會(huì)執(zhí)行一次 bgsave操作,然后將 rdb 文件傳輸?shù)娇蛻舳恕?/p>
$ ./redis-cli --host 192.168.x.x --port 6379 --rdb ./user.rdb
SYNC sent to master, writing 2501265095 bytes to './user.rdb'
Transfer finished with success.
參考:
https://juejin.cn/book/6844733724618129422/section/6844733724739764231
?
總結(jié)
以上是生活随笔為你收集整理的Redis 笔记(16)— info 指令和命令行工具(查看内存、状态、客户端连接数、监控服务器、扫描大key、采样服务器、执行批量命令等)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2022-2028年中国新零售行业深度调
- 下一篇: 2022-2028年中国超声波探伤仪行业