redis-7.4.4使用
redis-7.4.4 使用
1.redis数据库-info 的使用
redis-cli -h 地址 -p 端口 -a 密码
推荐如下方式登录
redis-cli --askpass
Please input password: *********
127.0.0.1:6379> ping
PONG
[root@zabbix71 bin]# redis-cli -a Root_123!
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info Server
# Server
redis_version:7.4.4
redis_git_sha1:00000000
redis_git_dirty:1
redis_build_id:54d55e8918e5ab68
redis_mode:standalone
os:Linux 4.19.90-89.11.v2401.ky10.x86_64 x86_64
arch_bits:64
monotonic_clock:POSIX clock_gettime
multiplexing_api:epoll
atomicvar_api:c11-builtin
gcc_version:7.3.0
process_id:197423
process_supervised:no
run_id:daad50e6795d89374e90e3e7d770664118982627
tcp_port:6379
server_time_usec:1749782438068836
uptime_in_seconds:62845
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:4951974
executable:/usr/local/bin/redis-server
config_file:/etc/redis/redis.conf
io_threads_active:0
listener0:name=tcp,bind=127.0.0.1,bind=-::1,port=6379
- info Server: 关于 Redis 服务器的基本信息,包括版本号、运行时长、操作系统等
- info Clients: 客户端连接的相关统计信息,比如已连接客户端的数量
- info Memory: 内存使用情况,包括 Redis 使用了多少内存、峰值内存使用量等
- info Persistence: RDB 和 AOF 持久化相关的状态和统计信息
- info Stats: 通用统计信息,例如处理了多少命令、网络流量等
- info Replication: 主从复制的状态信息
- info CPU: CPU 使用情况,包括用户态和内核态的时间消耗
- info Keyspace: 数据库中键值对的统计信息,包括键的数量、过期键的数量等
可以接多个
127.0.0.1:6379> info keyspace cpu Cluster
# CPU
used_cpu_sys:22.954634
used_cpu_user:128.059632
used_cpu_sys_children:0.003687
used_cpu_user_children:0.000000
used_cpu_sys_main_thread:22.953574
used_cpu_user_main_thread:128.059577# Cluster
cluster_enabled:0# Keyspace
db0:keys=1,expires=0,avg_ttl=0,subexpiry=0
2.常用
# 1. 创建key
127.0.0.1:6379> set name 2222
OK# 2.获取key
127.0.0.1:6379> get name
"2222"# 3.查看key的数量
127.0.0.1:6379> dbsize
(integer) 1# 4.查看是否存在
127.0.0.1:6379> exists name1
(integer) 0
127.0.0.1:6379> exists name
(integer) 1# 5.设置有效期
127.0.0.1:6379> set name1 333 ex 30
OK# 6.查看有效期
127.0.0.1:6379> ttl name1
(integer) 19# 7.设置key 的过期时间,单位:s
127.0.0.1:6379> expire name2 30
(integer) 1
127.0.0.1:6379> ttl name2
(integer) 26
127.0.0.1:6379> ttl name2
(integer) -2
127.0.0.1:6379> get name2
(nil)# 8.删除key
127.0.0.1:6379> get name3
"111"
127.0.0.1:6379> del name3
(integer) 1
127.0.0.1:6379> get name3
(nil)# 9.INCR: 将键对应的整数值增加1。
127.0.0.1:6379> set name2 23
OK
127.0.0.1:6379> get name2
"23"
127.0.0.1:6379> incr name2
(integer) 24# 10.INCRBY: 将键对应的整数值增加指定的整数
127.0.0.1:6379> incrby name2 100
(integer) 124# 11.DECR: 将键对应的整数值减少1
127.0.0.1:6379> decr name2
(integer) 123# 12.DECRBY: 将键对应的整数值减少指定的整数。
127.0.0.1:6379> decrby name2 10
(integer) 113
127.0.0.1:6379> get name2
"113"# 13.查看key的数量
127.0.0.1:6379> dbsize
3# 14.查看key 的类型
127.0.0.1:6379> type name5
string
非交互式的获取值(有告警,密码显示,不推荐)
[root@zabbix71 bin]# redis-cli -a Root_123! get name2
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"113"
3. 脚本
vi get_info.sh#!/bin/bash
redis-cli <<EOF
AUTH Root_123!
GET name2
info cpu
EOFchmod +x get_info.sh
./get_info.sh./get_info.sh
OK
"113"
# CPU
used_cpu_sys:26.437929
used_cpu_user:145.338280
used_cpu_sys_children:0.009529
used_cpu_user_children:0.001899
used_cpu_sys_main_thread:26.436822
used_cpu_user_main_thread:145.337989
4.获取key的中文值
#普通模式下
[root@zabbix71 ~]# redis-cli --askpass
Please input password: *********
127.0.0.1:6379> ping
PONG127.0.0.1:6379> set name5 张三▒
OK
127.0.0.1:6379> get name5
"\xe5\xbc\xa0\xe4\xb8\x89\xe6\x98"
127.0.0.1:6379> get name5 --raw
(error) ERR wrong number of arguments for 'get' command# 显示中文
[root@zabbix71 ~]# redis-cli --raw --askpass
Please input password: *********
127.0.0.1:6379> get name5
张三▒
127.0.0.1:6379> set name5 脚本解码
OK
127.0.0.1:6379> get name5
脚本解码