redis-cli命令行采集信息时遇到的一个小疑问()

给客户巡检的时候,之前想当然看-c可以通过命令行运行一些命令,方便巡检采集信息,比如:

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 -c "info" | head -n 5
# Server
redis_version:4.0.2
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:a9d11fb08914402b

然后就以为像mysql -e的参数一样。前两天去巡检,发现脚本里面执行的info replication是报错的:

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 -c "info replication"
(error) ERR unknown command 'info replication'
[root src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 -c "info Memory"
(error) ERR unknown command 'info Memory'

于是去查了下,通过–help命令查看-c的定义如下:

也就是说-c其实是指定集群模式连接(可防止moved和ask异常)。比如我们创建一个redis集群:

-c                 Enable cluster mode (follow -ASK and -MOVED redirections).
[root@xwei src]$./redis-cli -p 6480
127.0.0.1:6480> cluster nodes
325ed9e49d5d1cd0e1d275760f19dca26d530cec 127.0.0.1:6480@16480 myself,master - 0 1669262788000 2 connected 5461-10922
35fb417b9d31030e239c9b56f172173a18b25a97 127.0.0.1:6481@16481 master - 0 1669262789136 3 connected 10923-16383
429cda519db920a5e8d9e9ecbe76252c8804d885 127.0.0.1:6479@16479 master - 0 1669262788133 1 connected 0-5460
127.0.0.1:6480> 
127.0.0.1:6480> set ack ok
**(error) MOVED 12271 127.0.0.1:6481**
127.0.0.1:6480> 

如果我们加入-c参数,则可以自动重定向到集群内相关节点:

[root@xwei src]$./redis-cli -p 6480 -c
127.0.0.1:6480> set ack ok
-> Redirected to slot [12271] located at 127.0.0.1:6481
OK

或者

[root@xwei src]$./redis-cli -p 6480 -c set bck fail
OK

由此,其实通过redis-cli在命令行执行一些命令其实跟-c不是直接关联。
假如我们获取一些状态信息,则直接填写命令即可,用””括起来则会产生错误

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 "info replication"
(error) ERR unknown command 'info replication'
[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 info replication
# Replication
role:master
connected_slaves:0
master_replid:1bddf67bd42093afe70949dba492c57b64ef6219
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

获取内存信息

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 info memory
# Memory
used_memory:826680
used_memory_human:807.30K
used_memory_rss:2678784
used_memory_rss_human:2.55M
used_memory_peak:828936
used_memory_peak_human:809.51K
used_memory_peak_perc:99.73%
used_memory_overhead:815422
used_memory_startup:765688
used_memory_dataset:11258
used_memory_dataset_perc:18.46%
total_system_memory:33737867264
total_system_memory_human:31.42G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:3.24
mem_allocator:jemalloc-4.0.3
active_defrag_running:0

获取集群信息

[root@xwei src]$./redis-cli -p 6480 cluster nodes
325ed9e49d5d1cd0e1d275760f19dca26d530cec 127.0.0.1:6480@16480 myself,master - 0 1669264172000 2 connected 5461-10922
35fb417b9d31030e239c9b56f172173a18b25a97 127.0.0.1:6481@16481 master - 0 1669264172673 3 connected 10923-16383
429cda519db920a5e8d9e9ecbe76252c8804d885 127.0.0.1:6479@16479 master - 0 1669264173677 1 connected 0-5460

获取持久化信息

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 config get save
1) "save"
2) "900 1 300 10 60 10000"
[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 config get appendonly
1) "appendonly"
2) "no"

获取慢日志信息

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 config get "slowlog-max-len"
1) "slowlog-max-len"
2) "128"
[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 config get "lowlog-log-slower-than"
(empty list or set)

获取超时时间

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 config get timeout
1) "timeout"
2) "0"

另外一个小点,就是如果配置了requirepass xxxx 参数,则需要进行密码认证(redis 6.0以后具有ACL控制可以做用户,权限上的一些设置),即使redis-cli登录时没有输入密码或者密码错误,仍然可以登录到交互环境
但是当你运行一些命令时,则提示你进行认证操作。比如:

[root@xwei src]$./redis-cli -h 127.0.0.1 -p 7379 -a 111
127.0.0.1:7379> info
NOAUTH Authentication required.
127.0.0.1:7379> auth "abc123"
OK
127.0.0.1:7379> config get timeout
1) "timeout"
2) "0"
127.0.0.1:7379> 
————————

给客户巡检的时候,之前想当然看-c可以通过命令行运行一些命令,方便巡检采集信息,比如:

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 -c "info" | head -n 5
# Server
redis_version:4.0.2
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:a9d11fb08914402b

然后就以为像mysql -e的参数一样。前两天去巡检,发现脚本里面执行的info replication是报错的:

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 -c "info replication"
(error) ERR unknown command 'info replication'
[root src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 -c "info Memory"
(error) ERR unknown command 'info Memory'

于是去查了下,通过–help命令查看-c的定义如下:

也就是说-c其实是指定集群模式连接(可防止moved和ask异常)。比如我们创建一个redis集群:

-c                 Enable cluster mode (follow -ASK and -MOVED redirections).
[root@xwei src]$./redis-cli -p 6480
127.0.0.1:6480> cluster nodes
325ed9e49d5d1cd0e1d275760f19dca26d530cec 127.0.0.1:6480@16480 myself,master - 0 1669262788000 2 connected 5461-10922
35fb417b9d31030e239c9b56f172173a18b25a97 127.0.0.1:6481@16481 master - 0 1669262789136 3 connected 10923-16383
429cda519db920a5e8d9e9ecbe76252c8804d885 127.0.0.1:6479@16479 master - 0 1669262788133 1 connected 0-5460
127.0.0.1:6480> 
127.0.0.1:6480> set ack ok
**(error) MOVED 12271 127.0.0.1:6481**
127.0.0.1:6480> 

如果我们加入-c参数,则可以自动重定向到集群内相关节点:

[root@xwei src]$./redis-cli -p 6480 -c
127.0.0.1:6480> set ack ok
-> Redirected to slot [12271] located at 127.0.0.1:6481
OK

或者

[root@xwei src]$./redis-cli -p 6480 -c set bck fail
OK

由此,其实通过redis-cli在命令行执行一些命令其实跟-c不是直接关联。
假如我们获取一些状态信息,则直接填写命令即可,用””括起来则会产生错误

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 "info replication"
(error) ERR unknown command 'info replication'
[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 info replication
# Replication
role:master
connected_slaves:0
master_replid:1bddf67bd42093afe70949dba492c57b64ef6219
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

获取内存信息

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 info memory
# Memory
used_memory:826680
used_memory_human:807.30K
used_memory_rss:2678784
used_memory_rss_human:2.55M
used_memory_peak:828936
used_memory_peak_human:809.51K
used_memory_peak_perc:99.73%
used_memory_overhead:815422
used_memory_startup:765688
used_memory_dataset:11258
used_memory_dataset_perc:18.46%
total_system_memory:33737867264
total_system_memory_human:31.42G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:3.24
mem_allocator:jemalloc-4.0.3
active_defrag_running:0

获取集群信息

[root@xwei src]$./redis-cli -p 6480 cluster nodes
325ed9e49d5d1cd0e1d275760f19dca26d530cec 127.0.0.1:6480@16480 myself,master - 0 1669264172000 2 connected 5461-10922
35fb417b9d31030e239c9b56f172173a18b25a97 127.0.0.1:6481@16481 master - 0 1669264172673 3 connected 10923-16383
429cda519db920a5e8d9e9ecbe76252c8804d885 127.0.0.1:6479@16479 master - 0 1669264173677 1 connected 0-5460

获取持久化信息

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 config get save
1) "save"
2) "900 1 300 10 60 10000"
[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 config get appendonly
1) "appendonly"
2) "no"

获取慢日志信息

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 config get "slowlog-max-len"
1) "slowlog-max-len"
2) "128"
[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 config get "lowlog-log-slower-than"
(empty list or set)

获取超时时间

[root@xwei src]$./redis-cli -h 127.0.0.1 -a abc123 -p 7379 config get timeout
1) "timeout"
2) "0"

另外一个小点,就是如果配置了requirepass xxxx 参数,则需要进行密码认证(redis 6.0以后具有ACL控制可以做用户,权限上的一些设置),即使redis-cli登录时没有输入密码或者密码错误,仍然可以登录到交互环境
但是当你运行一些命令时,则提示你进行认证操作。比如:

[root@xwei src]$./redis-cli -h 127.0.0.1 -p 7379 -a 111
127.0.0.1:7379> info
NOAUTH Authentication required.
127.0.0.1:7379> auth "abc123"
OK
127.0.0.1:7379> config get timeout
1) "timeout"
2) "0"
127.0.0.1:7379>