Redis基础命令及运维文档。
# 查看所有key值,慎用
127.0.0.1:6379> keys *
# 设置key
127.0.0.1:6379> set keyA valueB
# 设置过期时间(单位:秒)
127.0.0.1:6379> expire keyA 60
# 删除key
127.0.0.1:6379> del keyA
# 清空当前库
127.0.0.1:6379> flushdb
# 清空所有库
127.0.0.1:6379> flushall
Redis内存满了,则按照maxmemory-policy策略对key进行过期。
maxmemory-policy 六种策略:
# 查看redis最大内存
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "0"
# 最大内存为1MB
127.0.0.1:6379> config set maxmemory 1mb
# 查看当前过期策略
127.0.0.1:6379> config get maxmemory-policy
1) "maxmemory-policy"
2) "noeviction"
# 设置过期策略
127.0.0.1:6379> config set maxmemory-policy allkeys-lru
Redis自带的性能检测工具redis-benchmark,可以模拟 N 个客户端同时发出 Y 个请求。
# 执行1000个请求来检测性能
redis-benchmark -n 1000 -q
# 50个并发请求,10000个请求来检测性能
redis-benchmark -h localhost -p 6379 -c 50 -n 10000
monitor命令,该命令可以实时监控redis正在执行的命令。
# 实时查看正在执行的命令
192.168.0.39:6379> monitor
# 过滤hget命令
redis-cli -h 192.168.0.39 -p 6379 -a "adc" monitor|grep -v "hget"
# 写入到文件(-c:集群,-a:登录密码)
redis-cli -h 192.168.0.39 -p 6379 -a "adc" -c monitor > test.log
# 查看操作系统日志
vim /var/log/messages
# 查看redis日志
tail -500f redis_6379.log
# 使用info查看redis相关信息
info
Redis提供了info指令,它会返回关于Redis服务器的各种信息和统计数值,可用于排查问题。
# 登录redis客户端,本地:redis-cli
redis-cli -h 10.0.6.9 -p 6379
# 查看内存使用情况(单位是byte)
127.0.0.1:6379> info memory
# Memory
used_memory:871808 # Redis 保存数据申请的内存空间(单位 Byte)
used_memory_human:851.38K
used_memory_rss:7692288 # 操作系统分配给 Redis 进程的内存空间
used_memory_rss_human:7.34M
used_memory_peak:933584 # Redis 进程在运行过程中占用的内存峰值
used_memory_peak_human:911.70K
used_memory_peak_perc:93.38%
used_memory_overhead:830320
used_memory_startup:809824
used_memory_dataset:41488
used_memory_dataset_perc:66.93%
allocator_allocated:1056640
allocator_active:1273856
allocator_resident:3936256
total_system_memory:4127088640
total_system_memory_human:3.84G
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 # Redis 最大可用内存,0表示不限制,一般推荐为最大物理内存的四分之三
maxmemory_human:0B
maxmemory_policy:noeviction # 当前策略
allocator_frag_ratio:1.21
allocator_frag_bytes:217216
allocator_rss_ratio:3.09
allocator_rss_bytes:2662400
rss_overhead_ratio:1.95
rss_overhead_bytes:3756032
mem_fragmentation_ratio:9.26 # 内存碎片率,used_memory_rss / used_memory
mem_fragmentation_bytes:6861504
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:20496
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
lazyfreed_objects:0
# memory help
192.168.0.39:6379> info clients
# Clients
connected_clients:1 # 已连接客户端的数量(不包括通过从属服务器连接的客户端)
cluster_connections:0
maxclients:10000
client_recent_max_input_buffer:24
client_recent_max_output_buffer:0
blocked_clients:0 # 阻塞客户端数量:最好应该为0
tracking_clients:0
clients_in_timeout_table:0
#################################
192.168.0.39:6379> client list # 所有连接到服务器的客户端信息和统计数据
dbsize=Keyspace中keys,建议所有的key都设置expire,那么info keyspace结果中dbsize=keys=expires。
# 当前库key的数量
127.0.0.1:6379> dbsize
(integer) 0
# 设置key
127.0.0.1:6379> set keyA valueB
# 统计key的总数和设置了过期的key的总数
127.0.0.1:6379> info keyspace
# Keyspace
db0:keys=1,expires=0,avg_ttl=0
# 设置过期时间(单位:秒)
127.0.0.1:6379> expire keyA 60 # 单位:秒
# 统计key的总数和设置了过期的key的总数
127.0.0.1:6379> info keyspace
# Keyspace
db0:keys=1,expires=1,avg_ttl=23088 # 单位:毫秒
keys : 当前库的key总数
expires : 当前库设置了过期时间key总数
avg_ttl : 平均剩余时间(avg_ttl=0:0表示没有采样记录设置过期,单位:毫秒)
集群中使用info keyspace,出现数量偏差严重则有问题,如果写入OOM,或者 info memory 返回 used_memory 大于 maxmemory,则将maxmemory调大即可,maxmemory默认为0,不限制 。
# 节点1
192.168.0.39:6379> info keyspace
# Keyspace
db0:keys=1014,expires=1003,avg_ttl=293025000
# 节点2:少量keys差异可能是由于时间差导致的
192.168.0.53:6379> info keyspace
# Keyspace
db0:keys=1009,expires=998,avg_ttl=3027986
# 节点3:该节点keys明显少于其他节点,对节点进行分析
192.168.0.38:6379> info keyspace
# Keyspace
db0:keys=600,expires=580,avg_ttl=0
https://mp.weixin.qq.com/s/0U4ZOBYzJB1GVbD-3q-Z9w