Redis操作手册
1. 通用
1.1 进程检查
ps -ef|grep redis
1.2 如何连接
redis-cli -h <xx.xx.xx.xx> -p <6389> -a <passwd>
1.3 连接数检查
总连接数
config get maxclients
当前连接数
info clients
output:
10.200.161.98:6389> info clients
#Clients
<mark>connected_clients:1</mark>
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
当前连接详情
client list
output:
10.200.161.98:6389> client list
id=5 addr=10.200.161.99:35682 fd=23 name= age=359955 <mark>idle=0</mark> flags=S db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r <mark>cmd=replconf</mark>
id=188 addr=10.200.161.98:49684 fd=19 name= age=1670 <mark>idle=0</mark> flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r <mark>cmd=client</mark>
1.4 内存检查
info memory
output:
10.200.161.98:6389> info memory
#Memory
used_memory:2468392
used_memory_human:2.35M
used_memory_rss:4087808
<mark>used_memory_rss_human:3.90M</mark>
used_memory_peak:2509136
used_memory_peak_human:2.39M
total_system_memory:16594661376
total_system_memory_human:15.45G
used_memory_lua:37888
used_memory_lua_human:37.00K
<mark>maxmemory:0</mark>
maxmemory_human:0B
maxmemory_policy:noeviction
<mark>mem_fragmentation_ratio:1.66</mark>
mem_allocator:jemalloc-4.0.3
1.5 配置检查
info server
output:
10.200.161.98:6389> info server
#Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:c395fbbb4e05f728
redis_mode:cluster
os:Linux 3.0.101-63-default x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.3.4
process_id:24824
run_id:50053cc92fbbeb94f38cadbf2f3c5faa718dab56
tcp_port:6389
uptime_in_seconds:360273
uptime_in_days:4
hz:10
lru_clock:1538332
executable:/redis/data/redis-server
<mark>config_file:/redis/conf/6389.conf</mark>
1.6 配置修改
config set <xx> #修改配置项
config get <xx> #查看配置项
config rewrite #将服务器当前配置保存到 redis.conf 文件中
1.7 慢查询
slowlog get <n>
output:
10.200.161.98:6389> slowlog get 10
1) 1) (integer) 1
2) (integer) 1545041978
3) (integer) 10936
4) 1) "info"
2) "replication"
2) 1) (integer) 0
2) (integer) 1544776478
3) (integer) 10400
4) 1) "INFO"
1.8 键的个数
dbsize
禁止在生产上执行keys *
1.9 启停
redis-server /redis/conf/redis.conf
shutdown nosave/save 或者 kill -9
1.10 持久化
AOF
bgrewriteaof
RDB
bgsave
查看上次保存的时间
lastsave
2. redis sentinel
2.1 master状态检查
info sentinel #sentinel节点操作
2.2 slave状态检查
sentinel slaves mymaster
2.3 复制关系
info replication
output:
10.200.161.98:6389> info replication
#Replication
<mark>role:master</mark>
<mark>connected_slaves:1</mark>
slave0:ip=10.200.161.99,port=6390,state=online,offset=500725,lag=0
master_repl_offset:500739
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:500738
2.4 指定从节点
slaveof <master> <port>
slaveof no one 取消从节点角色
3. redis cluster
3.1 集群状态检查
cluster info
output:
10.200.161.98:6389> cluster info
<mark>cluster_state:ok</mark>
cluster_slots_assigned:16384
<mark>cluster_slots_ok:16384</mark>
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:10
cluster_size:5
cluster_current_epoch:60
cluster_my_epoch:55
cluster_stats_messages_sent:1069513
cluster_stats_messages_received:1061488
cluster nodes
output:
10.200.161.98:6389> cluster nodes
357439392aedc968347cdf83605d79602f02f66f 10.200.161.99:6389 <mark>master</mark> - 0 1545041781751 58 connected <mark>3277-6553</mark>
3a588b48ba2bbfa17b6d9f062d10ddc644139b96 10.200.161.98:6389 <mark>myself,master</mark> - 0 0 55 connected <mark>0-3276</mark>
1a93be3593b15f0dfc0e58bf39a08d34ecb2eb8a 10.200.161.99:6390 slave 3a588b48ba2bbfa17b6d9f062d10ddc644139b96 0 1545041783252 55 connected
78b6a6a6a585cb4294f05f5ef8f3ba2254e53cda 10.200.161.102:6390 <mark>master</mark> - 0 1545041786259 53 connected <mark>13107-16383</mark>
995939c5055b2dfe198950a6f70b7557888bd9c4 10.200.161.100:6390 slave 83fbf91616ab6580f2fbbb1a15abdb869a0724bb 0 1545041785257 59 connected
83fbf91616ab6580f2fbbb1a15abdb869a0724bb 10.200.161.101:6389 <mark>master</mark> - 0 1545041782251 59 connected <mark>9830-13106</mark>
35d5830da7af01a8948e876d50357c16b4aeb288 10.200.161.98:6390 slave 357439392aedc968347cdf83605d79602f02f66f 0 1545041780249 58 connected
2446d535229d968fd0d94f0927512167ff4bd021 10.200.161.100:6389 slave 2a0ddbc5fdb22927b6ace2edeea95d67dbba9861 0 1545041780750 60 connected
c47c2cd16bdce7b12d9b165889ba977119abeb8d 10.200.161.102:6389 slave 78b6a6a6a585cb4294f05f5ef8f3ba2254e53cda 0 1545041779247 53 connected
2a0ddbc5fdb22927b6ace2edeea95d67dbba9861 10.200.161.101:6390 <mark>master</mark> - 0 1545041784254 60 connected <mark>6554-9829</mark>
3.2 节点
cluster meet <ip> <port> :#将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
cluster forget <node_id> :#从集群中移除 node_id 指定的节点。
cluster replicate <master_node_id> :#将当前从节点设置为 node_id 指定的master节点的slave节点。只能针对slave节点操作。
cluster failover:#将当前从节点提升为主节点。只能针对slave节点操作。
cluster saveconfig :#将节点的配置文件保存到硬盘里面。
3.3 槽
cluster addslots <slot> [slot ...] :#将一个或多个槽( slot)指派( assign)给当前节点。
cluster delslots <slot> [slot ...] :#移除一个或多个槽对当前节点的指派。
cluster flushslots :#移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
cluster setslot <slot> node <node_id> :#将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽,然后再进行指派。
cluster setslot <slot> migrating <node_id> :#将本节点的槽 slot 迁移到 node_id 指定的节点中。
cluster setslot <slot> importing <node_id> :#从 node_id 指定的节点中导入槽 slot 到本节点。
cluster setslot <slot> stable :#取消对槽 slot 的导入( import)或者迁移( migrate)。
3.4 键
cluster keyslot <key> :#计算键 key 应该被放置在哪个槽上。
cluster countkeysinslot <slot> :#返回槽 slot 目前包含的键值对数量。
cluster getkeysinslot <slot> <count> :#返回 count 个 slot 槽中的键 。
3.5 trib 助手
redis@MINI-DBS-A-01:/home/redis$redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>
<mark>create</mark> host1:port1 ... hostN:portN
--replicas <arg>
check host:port
info host:port
fix host:port
--timeout <arg>
<mark>reshard</mark> host:port
--from <arg>
--to <arg>
--slots <arg>
--yes
--timeout <arg>
--pipeline <arg>
rebalance host:port
--weight <arg>
--auto-weights
--use-empty-masters
--timeout <arg>
--simulate
--pipeline <arg>
--threshold <arg>
add-node new_host:new_port existing_host:existing_port
--slave
--master-id <arg>
del-node host:port node_id
set-timeout host:port milliseconds
call host:port command arg arg .. arg
import host:port
--from <arg>
--copy
--replace
help (show this help)
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.