redis常用命令

Redis常用命令

Redis提供了丰富的命令对数据库和各种数据库类型进行操作,这些命令可以在Linux终端使用。
1、键值相关命令
2、服务器相关命令

键值相关命令:

keys
返回满足给定pattern的所有key 

127.0.0.1:6379> keys pattern
127.0.0.1:6379> keys *
1) "set_four"
2) "list_one"
3) "firstname"
4) "user"
5) "lastname"
6) "zset"
7) "age"
8) "set_three"
9) "set_one"
10) "set_two"
11) "one"
12) "set_five"
13) "name"
127.0.0.1:6379> keys list_*
1) "list_one"

 


exists
确认一个key是否存在

127.0.0.1:6379> exists key [key ...]
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> exists name firstname
(integer) 2
127.0.0.1:6379> exists name hi
(integer) 1

 

del
删除一个key

127.0.0.1:6379> del key [key ...]
127.0.0.1:6379> del zset
(integer) 1
127.0.0.1:6379> exists zset1
(integer) 0

  

expire
设置一个key的过期时间
ttl 查看一个key的距离过期时间

127.0.0.1:6379> set guo guozhen
OK
127.0.0.1:6379> expire guo 10
(integer) 1
127.0.0.1:6379> ttl guo
(integer) 8
127.0.0.1:6379> ttl guo
(integer) 4
127.0.0.1:6379> ttl guo
(integer) 1
127.0.0.1:6379> ttlguo
(error) ERR unknown command 'ttlguo'
127.0.0.1:6379> ttl guo
(integer) -2
127.0.0.1:6379> get guo
(nil)

  

redis数据库概念:
redis默认拥有0~15 16个数据库。默认启用redis客户端进入的就是0数据库

move
将当前数据库中的key转移到其他数据库当中

127.0.0.1:6379> exists age
(integer) 1
127.0.0.1:6379> get age
"21"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> exists age
(integer) 0
127.0.0.1:6379[1]> select 1
OK
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> move age 1
(integer) 1
127.0.0.1:6379> exists age
(integer) 0
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> exists age
(integer) 1
127.0.0.1:6379[1]> get age
"21"

 

  

persist
移除给定key的过期时间

127.0.0.1:6379> get name
"guodaxia:guozhen"
127.0.0.1:6379> expire name 40
(integer) 1
127.0.0.1:6379> ttl name
(integer) 28
127.0.0.1:6379> ttkl name
(error) ERR unknown command 'ttkl'
127.0.0.1:6379> ttl name
(integer) 14
127.0.0.1:6379> persist name
(integer) 1
127.0.0.1:6379> ttl name
(integer) -1
127.0.0.1:6379> expire name 10
(integer) 1
127.0.0.1:6379> ttl name
(integer) 4
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> get name
(nil)

ttl得到-1表示该key没有过期时间,ttl得到-2表示已经过期


randomkey
随机返回数据库里面的一个key

127.0.0.1:6379> RANDOMKEY
"set_five"
127.0.0.1:6379>

  

rename
重命名key

127.0.0.1:6379> keys *
1) "set_four"
2) "list_one"
3) "firstname"
4) "user"
5) "lastname"
6) "set_three"
7) "set_one"
8) "set_two"
9) "one"
10) "set_five"
127.0.0.1:6379> rename set_four set_four001
OK
127.0.0.1:6379> keys *
1) "list_one"
2) "firstname"
3) "user"
4) "lastname"
5) "set_three"
6) "set_one"
7) "set_two"
8) "one"
9) "set_five"
10) "set_four001"

  

type
返回值的类型

127.0.0.1:6379> type set_four001
set
127.0.0.1:6379> type list_one
list
127.0.0.1:6379> type firstname
string

  

服务器相关命令

ping
测试连接是否存活(类似网络哪种ping,表示redis是否正常通信),成功返回pong

127.0.0.1:6379> ping
PONG

  

echo
在命令行打印一些内容

127.0.0.1:6379> echo nihao
"nihao"

  

select
选择数据库

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get firstname
(nil)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> get firstname
"zhen"

  


quit
退出连接

dbsize
返回当前数据库中所有key的数量

127.0.0.1:6379> dbsize
(integer) 11
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> dbsize
(integer) 1
127.0.0.1:6379[1]> select 2
OK
127.0.0.1:6379[2]> dbsize
(integer) 0

  

info
获取服务器信息和统计

127.0.0.1:6379> info
# Server
redis_version:3.2.100
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:dd26f1f93c5130ee
redis_mode:standalone
os:Windows
arch_bits:64
multiplexing_api:WinSock_IOCP
process_id:5736
run_id:073d64df2ea79075f22979ff6bac0e97b1647506
tcp_port:6379
uptime_in_seconds:309
uptime_in_days:0
hz:10
lru_clock:13854304
executable:D:\Program Files\redis\Redis\redis-server.exe
config_file:

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

# Memory
used_memory:694248
used_memory_human:677.98K
used_memory_rss:656472
used_memory_rss_human:641.09K
used_memory_peak:770360
used_memory_peak_human:752.30K
total_system_memory:0
total_system_memory_human:0B
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:0.95
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1490249003
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
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

# Stats
total_connections_received:2
total_commands_processed:10
instantaneous_ops_per_sec:0
total_net_input_bytes:204
total_net_output_bytes:11859197
instantaneous_input_kbps:0.01
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
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:0.53
used_cpu_user:0.30
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=11,expires=0,avg_ttl=0
db1:keys=1,expires=0,avg_ttl=0

  

config get
实时传储收到的请求
例子中获取dir这个参数配置的值,如果要获取所有配置参数的值 只要执行config get *即可

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

  

flushdb
删除当前数据库中的所有key

127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> set name guozhen
OK
127.0.0.1:6379[1]> flushdb
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> keys *
1) "name"

  

flushall
删除所有数据库里面的所有key

127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> set name guozhen
OK
127.0.0.1:6379[1]> flushall
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> keys *
1) (empty list or set)

  

posted @ 2017-03-23 14:34  guodaxia  阅读(372)  评论(0编辑  收藏  举报