跳转至

53. redis服务器管理命令

1. 常见命令

1.info
2.clinet list
3.client kill ip:port
4.config get *
5.config get/set   #动态修改
6.config resetstat #重置
7.dbsize
8.flushall        #清空所有数据
9.select 1        #选择指定redis库
10.flushdb        #清空当前库
11.monitor        #监控实时指令
12.shutdown       #关闭redis服务

2. info

info                #查看信息

#常用的是下面的2个
info memory         #查看内存使用
info replication    #查看主从状态
#1.查看所有状态
127.0.0.1:6379> info
# Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:978db570fbe4c62f
redis_mode:standalone
os:Linux 3.10.0-957.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:73723
run_id:b5c0c51b5807a69848f32456a9ea40abc47e3c97
tcp_port:6379
uptime_in_seconds:4580
uptime_in_days:0
hz:10
lru_clock:9950697
executable:/data/6379/redis-server
config_file:/data/6379/redis.conf

# Clients
connected_clients:4
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:886544
used_memory_human:865.77K
used_memory_rss:7925760
used_memory_rss_human:7.56M
used_memory_peak:886544
used_memory_peak_human:865.77K
total_system_memory:1019797504
total_system_memory_human:972.55M
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:8.94
mem_allocator:jemalloc-4.0.3

# Persistence
loading:0
rdb_changes_since_last_save:2
rdb_bgsave_in_progress:0
rdb_last_save_time:1620563153
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:1
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_current_size:1259
aof_base_size:0
aof_pending_rewrite:0
aof_buffer_length:0
aof_rewrite_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0

# Stats
total_connections_received:11
total_commands_processed:149
instantaneous_ops_per_sec:0
total_net_input_bytes:4465
total_net_output_bytes:111430
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
evicted_keys:0
keyspace_hits:45
keyspace_misses:27
pubsub_channels:0
pubsub_patterns:3
latest_fork_usec:16545
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:1.10
used_cpu_user:13.82
used_cpu_sys_children:0.02
used_cpu_user_children:0.01

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=11,expires=0,avg_ttl=0
#2.查看内存使用状态
127.0.0.1:6379> info memory
# Memory
used_memory:822688
used_memory_human:803.41K
used_memory_rss:7925760
used_memory_rss_human:7.56M
used_memory_peak:886544
used_memory_peak_human:865.77K
total_system_memory:1019797504
total_system_memory_human:972.55M
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:9.63
mem_allocator:jemalloc-4.0.3


#3.查看主从复制状态
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

3. client

client list     #查看所有客户端连接情况

client kill 127.0.0.1:36698 #关闭上面命令查到的指定的某个连接
#1.查看客户端连接情况
127.0.0.1:6379> client list
id=11 addr=127.0.0.1:44890 fd=7 name= age=678 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client


#2.再多开2个窗口,一共开启3个窗口
redis-cli -a 123456

#3.再次在窗口1查看连接
127.0.0.1:6379> client list
id=11 addr=127.0.0.1:44890 fd=7 name= age=780 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
id=13 addr=127.0.0.1:44894 fd=8 name= age=62 idle=62 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=command
id=14 addr=127.0.0.1:44896 fd=9 name= age=54 idle=54 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=command

#4.关闭某个连接
127.0.0.1:6379> client kill 127.0.0.1:44896
OK
127.0.0.1:6379> client list
id=11 addr=127.0.0.1:44890 fd=7 name= age=845 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
id=13 addr=127.0.0.1:44894 fd=8 name= age=127 idle=127 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=command

#发现窗口2和窗口3依旧好用。。。不知道为何?
#再次查看连接,发现有是3个了。怎么回事?
127.0.0.1:6379> client list
id=11 addr=127.0.0.1:44890 fd=7 name= age=963 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
id=13 addr=127.0.0.1:44894 fd=8 name= age=245 idle=6 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get
id=15 addr=127.0.0.1:44898 fd=9 name= age=88 idle=18 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get

4. config

config get maxmemory   #得到最大使用内存

config set maxmemory    #设置最大使用内存
#1.获取所有配置信息(共70个)
127.0.0.1:6379> config get *
  1) "dbfilename"
  2) "dump.rdb"
  3) "requirepass"
  4) "123456"
  5) "masterauth"
  6) ""
  7) "unixsocket"
  8) ""
  9) "logfile"
 10) "/data/6379/redis.log"
 11) "pidfile"
 12) "/var/run/redis.pid"
 13) "slave-announce-ip"
 14) ""
 15) "maxmemory"
 16) "0"
 17) "maxmemory-samples"
 18) "5"
 19) "timeout"
 20) "0"
 21) "auto-aof-rewrite-percentage"
 22) "100"
 23) "auto-aof-rewrite-min-size"
 24) "67108864"
 25) "hash-max-ziplist-entries"
 26) "512"
 27) "hash-max-ziplist-value"
 28) "64"
 29) "list-max-ziplist-size"
 30) "-2"
 31) "list-compress-depth"
 32) "0"
 33) "set-max-intset-entries"
 34) "512"
 35) "zset-max-ziplist-entries"
 36) "128"
 37) "zset-max-ziplist-value"
 38) "64"
 39) "hll-sparse-max-bytes"
 40) "3000"
 41) "lua-time-limit"
 42) "5000"
 43) "slowlog-log-slower-than"
 44) "10000"
 45) "latency-monitor-threshold"
 46) "0"
 47) "slowlog-max-len"
 48) "128"
 49) "port"
 50) "6379"
 51) "tcp-backlog"
 52) "511"
 53) "databases"
 54) "16"
 55) "repl-ping-slave-period"
 56) "10"
 57) "repl-timeout"
 58) "60"
 59) "repl-backlog-size"
 60) "1048576"
 61) "repl-backlog-ttl"
 62) "3600"
 63) "maxclients"
 64) "10000"
 65) "watchdog-period"
 66) "0"
 67) "slave-priority"
 68) "100"
 69) "slave-announce-port"
 70) "0"
 71) "min-slaves-to-write"
 72) "0"
 73) "min-slaves-max-lag"
 74) "10"
 75) "hz"
 76) "10"
 77) "cluster-node-timeout"
 78) "15000"
 79) "cluster-migration-barrier"
 80) "1"
 81) "cluster-slave-validity-factor"
 82) "10"
 83) "repl-diskless-sync-delay"
 84) "5"
 85) "tcp-keepalive"
 86) "300"
 87) "cluster-require-full-coverage"
 88) "yes"
 89) "no-appendfsync-on-rewrite"
 90) "no"
 91) "slave-serve-stale-data"
 92) "yes"
 93) "slave-read-only"
 94) "yes"
 95) "stop-writes-on-bgsave-error"
 96) "yes"
 97) "daemonize"
 98) "yes"
 99) "rdbcompression"
100) "yes"
101) "rdbchecksum"
102) "yes"
103) "activerehashing"
104) "yes"
105) "protected-mode"
106) "yes"
107) "repl-disable-tcp-nodelay"
108) "no"
109) "repl-diskless-sync"
110) "no"
111) "aof-rewrite-incremental-fsync"
112) "yes"
113) "aof-load-truncated"
114) "yes"
115) "maxmemory-policy"
116) "noeviction"
117) "loglevel"
118) "notice"
119) "supervised"
120) "no"
121) "appendfsync"
122) "everysec"
123) "syslog-facility"
124) "local0"
125) "appendonly"
126) "yes"
127) "dir"
128) "/data/6379"
129) "save"
130) "900 1 300 10 60 10000"
131) "client-output-buffer-limit"
132) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
133) "unixsocketperm"
134) "0"
135) "slaveof"
136) ""
137) "notify-keyspace-events"
138) ""
139) "bind"
140) "192.168.178.152 127.0.0.1"

#2.获取某个配置信息
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "0"

#3.设置配置信息
127.0.0.1:6379> CONFIG SET maxmemory 512
OK
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "512"

5. dbsize

dbsize      #查看所有键值对个数
#1.获取所有键个数
127.0.0.1:6379> DBSIZE
(integer) 13
127.0.0.1:6379> dbsize
(integer) 13

#2.获取所有键
127.0.0.1:6379> keys *
 1) "topn"
 2) "a"
 3) "aa"
 4) "aaa"
 5) "chupeng"
 6) "a2"
 7) "wangyao"
 8) "a1"
 9) "name"
10) "num"
11) "stu"
12) "ticket"
13) "wechat"

6. flush

flushall    #清空所有库  (危险,慎用)
flushdb     #清空当前库  (同上)

select 1    #选定库1(redis默认16个库,默认使用0号库)此操作老师说只能在本地使用,集群只能使用默认的0号库
#1.选择库
127.0.0.1:6379>
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> 
127.0.0.1:6379[2]> select 15
OK
127.0.0.1:6379[15]> select 16
(error) ERR invalid DB index
127.0.0.1:6379[15]>
#可以看出,只有15个库

#2.清空当前库
127.0.0.1:6379[15]> select 0
OK
127.0.0.1:6379> keys *
 1) "topn"
 2) "a"
 3) "aa"
 4) "aaa"
 5) "chupeng"
 6) "a2"
 7) "wangyao"
 8) "a1"
 9) "name"
10) "num"
11) "stu"
12) "ticket"
13) "wechat"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> keys *
(empty list or set)

#3.写入数据
127.0.0.1:6379> set a 1
(error) OOM command not allowed when used memory > 'maxmemory'.

#发现报错,查看问题
127.0.0.1:6379> info memory
# Memory
used_memory:862776
used_memory_human:842.55K
used_memory_rss:7954432
used_memory_rss_human:7.59M
used_memory_peak:886544
used_memory_peak_human:865.77K
total_system_memory:1019797504
total_system_memory_human:972.55M
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:512
maxmemory_human:512B
maxmemory_policy:noeviction
mem_fragmentation_ratio:9.22
mem_allocator:jemalloc-4.0.3
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> config get memory
(empty list or set)
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "512"
127.0.0.1:6379> config set maxmemory 0
OK
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "0"
#再次写入成功
127.0.0.1:6379> set a 1
OK
#写入1号库数据
127.0.0.1:6379> get a
"1"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get a
(nil)
127.0.0.1:6379[1]> set b 2
OK
127.0.0.1:6379[1]> get b
"2"


#4.清空所有库
127.0.0.1:6379[1]> flushall
OK
127.0.0.1:6379[1]> get b
(nil)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> get a
(nil)

7. monitor

monitor     #监控别的窗口的所有操作

#可以放入后台运行,导入某个文件中,做审计,不常用
redis-cli -a 123 -h 192.168.178.151 monitor >/tmp/a.log &
#1.使用IP连接redis测试
[root@152 6379]# redis-cli -a 123456 -h 192.168.178.152
192.168.178.152:6379> get a
(nil)
192.168.178.152:6379> set a 1
OK
192.168.178.152:6379> get a
"1"
192.168.178.152:6379> client list
id=13 addr=127.0.0.1:44894 fd=8 name= age=1051 idle=812 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get
id=15 addr=127.0.0.1:44898 fd=9 name= age=894 idle=824 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get
id=16 addr=192.168.178.152:49738 fd=7 name= age=58 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
192.168.178.152:6379>

#2.窗口1开启monitor
[root@152 6379]# redis-cli -a 123456 -h 192.168.178.152 monitor
OK

#3.窗口2 进行操作
[root@152 ~]# redis-cli -a 123456
127.0.0.1:6379> get a
"1"
127.0.0.1:6379> get aa
(nil)
127.0.0.1:6379> set aa 2
OK


#4.查看窗口1 日志信息
[root@152 6379]# redis-cli -a 123456 -h 192.168.178.152 monitor
OK
1620564819.531226 [0 127.0.0.1:44904] "AUTH" "123456"
1620564819.532577 [0 127.0.0.1:44904] "COMMAND"
1620564825.431498 [0 127.0.0.1:44904] "get" "a"
1620564832.529652 [0 127.0.0.1:44904] "get" "aa"
1620564835.504752 [0 127.0.0.1:44904] "set" "aa" "2"

#5.窗口1将监控信息写入文件
^C
[root@152 6379]# redis-cli -a 123456 -h 192.168.178.152 monitor > /tmp/a.log &
[1] 102571
[root@152 6379]# tail -F /tmp/a.log
OK


#6.窗口2进行操作
[root@152 ~]# redis-cli -a 123456
127.0.0.1:6379> get aa
"2"
127.0.0.1:6379> set bb 3
OK
127.0.0.1:6379> get bb
"3"


#7.查看窗口1日志文件
[root@152 6379]# tail -F /tmp/a.log
OK
1620564970.978272 [0 127.0.0.1:44908] "AUTH" "123456"
1620564970.979808 [0 127.0.0.1:44908] "COMMAND"
1620564974.367621 [0 127.0.0.1:44908] "get" "aa"
1620564979.571422 [0 127.0.0.1:44908] "set" "bb" "3"
1620564981.219638 [0 127.0.0.1:44908] "get" "bb"

^C
[root@152 6379]# cat /tmp/a.log
OK
1620564970.978272 [0 127.0.0.1:44908] "AUTH" "123456"
1620564970.979808 [0 127.0.0.1:44908] "COMMAND"
1620564974.367621 [0 127.0.0.1:44908] "get" "aa"
1620564979.571422 [0 127.0.0.1:44908] "set" "bb" "3"
1620564981.219638 [0 127.0.0.1:44908] "get" "bb"

8. 重点命令如下

1.info memory
2.info replication
3.config get
4.config set
5.shutdown

最后更新: 2022-02-20 08:44:07