天天看點

05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令



redis常用指令

redis提供了豐富的指令對資料庫和各種資料庫類型進行操作,這些指令可以再linux終端使用。

鍵值相關指令:

keys:傳回滿足給定pattern的所有key

05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令

用表達式*表示取出所有的key

127.0.0.1:6379> keys *

1) "age"

2) "name"

3) "age2"

127.0.0.1:6379> keys a*

2) "age2"

127.0.0.1:6379>

exists:确認一個key是否存在

05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令

127.0.0.1:6379> exists name

(integer) 1

127.0.0.1:6379> exists toto

(integer) 0

del:删除一個key.

05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令

127.0.0.1:6379> set toto1 toto

ok

1) "name"

3) "age"

4) "toto1"

127.0.0.1:6379> del toto1  

  //删除toto1這個鍵。

  //再次删除的時候删除不成功

127.0.0.1:6379> exists toto1  

//

expire:設定一個key的過期時間

05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令

在本例中,設定addr這個key的過期時間是10秒,然後不斷的用ttl來擷取這個key的有效時長,直至-1,說明此值已過期。

expiree addr 10表示:對addr設定過期時間為10秒。

ttl查詢剩餘的過期時間,當傳回-1的時候表示已經過期了。

127.0.0.1:6379> set age 20   

//設定一個key為age的值。

127.0.0.1:6379> get age

"20"

127.0.0.1:6379> expire age 10

127.0.0.1:6379> ttl age

(integer) 8                                                                     

(integer) 5

(integer) 3

(integer) -2

127.0.0.1:6379> exists age

127.0.0.1:6379> keys *   

//檢視age是否還存在

move:将目前資料庫中的key轉移到其它資料庫中。

選擇資料庫,要用到select,select 0表示選擇到目前資料庫。select

1表示第二個資料庫,一共是從0到15個資料庫,總共是16個資料庫。(注意,進入redis的時候預設進入的是0資料庫)

127.0.0.1:6379> select 0

127.0.0.1:6379> set age 30

"30"

127.0.0.1:6379> move age 1

(nil)

127.0.0.1:6379> select 1

127.0.0.1:6379[1]> get age

127.0.0.1:6379[1]>

persist:移除給定key的過期時間

05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令

127.0.0.1:6379[1]> select 1

127.0.0.1:6379[1]> keys *

127.0.0.1:6379[1]> expire age 300 

 //設定age的過期時間為300

127.0.0.1:6379[1]> ttl age

(integer) 297

(integer) 293

127.0.0.1:6379[1]> persist age  

 //取消age的過期

(integer) -1

127.0.0.1:6379[1]> exists age

randomkey:随機傳回key空間的一個key

05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令

1) "gender"

3) "address"

4) "age"

127.0.0.1:6379[1]> randomkey      

//随機獲得一個key

"gender"

127.0.0.1:6379[1]> randomkey

"address"

rename:重命名key

rename age nianling    将age這個key重新命名為nianling

4) "nianling"

type:傳回鍵的資料類型

05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令

127.0.0.1:6379[1]> type name

string

伺服器的相關指令

ping:測試連接配接是否存活

05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令
05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令

echo:在指令行列印一些内容

127.0.0.1:6379> echo toto

"toto"

127.0.0.1:6379> echo toto2

"toto2"

select:選擇資料庫.redis資料庫編号從0~15,我們可以選擇任意一個資料庫來進行資料的存取。

05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令

當選擇16時,報錯,說明沒有編号為16的這個資料庫。

quit:退出連接配接,或者exit

05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令

dbsize:傳回目前資料庫中key的數目。

1) "age2"

127.0.0.1:6379> dbsize    

(integer) 2               

//傳回0表示有2個key

127.0.0.1:6379[1]> dbsize

(integer) 4                

//傳回4表示有4個key

info:擷取伺服器的資訊和統計

127.0.0.1:6379[1]> info

# server

redis_version:2.8.19

redis_git_sha1:00000000

redis_git_dirty:0

redis_build_id:b80fb73838c77f5a

redis_mode:standalone

os:linux 2.6.32-358.el6.x86_64 x86_64

arch_bits:64

multiplexing_api:epoll

gcc_version:4.4.7

process_id:5136

run_id:607e299bec2e4e492d138ee1a5a29871d0062e24

tcp_port:6379

uptime_in_seconds:2863

uptime_in_days:0

hz:10

lru_clock:14609760

config_file:

# clients

connected_clients:1

client_longest_output_list:0

client_biggest_input_buf:0

blocked_clients:0

# memory

used_memory:504152

used_memory_human:492.34k

used_memory_rss:7958528

used_memory_peak:504152

used_memory_peak_human:492.34k

used_memory_lua:35840

mem_fragmentation_ratio:15.79

mem_allocator:jemalloc-3.6.0

# persistence

loading:0

rdb_changes_since_last_save:8

rdb_bgsave_in_progress:0

rdb_last_save_time:1423893041

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:1

total_commands_processed:40

instantaneous_ops_per_sec:0

total_net_input_bytes:969

total_net_output_bytes:584

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:0

evicted_keys:0

keyspace_hits:7

keyspace_misses:1

pubsub_channels:0

pubsub_patterns:0

latest_fork_usec: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:3.51

used_cpu_user:0.93

used_cpu_sys_children:0.00

used_cpu_user_children:0.00

# keyspace

db0:keys=2,expires=0,avg_ttl=0

db1:keys=4,expires=0,avg_ttl=0

config get:實時傳儲收到的請求。

127.0.0.1:6379[1]> config get *

1) "dbfilename"

2) "dump.rdb"

3) "requirepass"

4) ""

5) "masterauth"

6) ""

7) "unixsocket"

8) ""

9) "logfile"

 10) ""

 11) "pidfile"

 12) "/var/run/redis.pid"

 13) "maxmemory"

 14) "0"

 15) "maxmemory-samples"

 16) "3"

 17) "timeout"

 18) "0"

 19) "tcp-keepalive"

 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-entries"

 30) "512"

 31) "list-max-ziplist-value"

 32) "64"

 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) "4064"

 65) "watchdog-period"

 66) "0"

 67) "slave-priority"

 68) "100"

 69) "min-slaves-to-write"

 70) "0"

 71) "min-slaves-max-lag"

 72) "10"

 73) "hz"

 74) "10"

 75) "repl-diskless-sync-delay"

 76) "5"

 77) "no-appendfsync-on-rewrite"

 78) "no"

 79) "slave-serve-stale-data"

 80) "yes"

 81) "slave-read-only"

 82) "yes"

 83) "stop-writes-on-bgsave-error"

 84) "yes"

 85) "daemonize"

 86) "no"

 87) "rdbcompression"

 88) "yes"

 89) "rdbchecksum"

 90) "yes"

 91) "activerehashing"

 92) "yes"

 93) "repl-disable-tcp-nodelay"

 94) "no"

 95) "repl-diskless-sync"

 96) "no"

 97) "aof-rewrite-incremental-fsync"

 98) "yes"

 99) "aof-load-truncated"

100) "yes"

101) "appendonly"

102) "no"

103) "dir"

104) "/usr/local/redis/bin"

105) "maxmemory-policy"

106) "volatile-lru"

107) "appendfsync"

108) "everysec"

109) "save"

110) "3600 1 300 100 60 10000"

111) "loglevel"

112) "notice"

113) "client-output-buffer-limit"

114) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"

115) "unixsocketperm"

116) "0"

117) "slaveof"

118) ""

119) "notify-keyspace-events"

120) ""

121) "bind"

122) ""

127.0.0.1:6379[1]> config get timeout

1) "timeout"

2) "0"

flushdb:删除目前選擇資料庫中的所有key(也就是說flushdb的作用是清空目前資料庫)

05_NoSQL資料庫之Redis資料庫:Redis的常用指令,鍵值相關指令和伺服器相關指令

127.0.0.1:6379[2]> select 1

127.0.0.1:6379[1]> set age 20

127.0.0.1:6379[1]> set name zhangsan

127.0.0.1:6379[1]> set gender male

2) "gender"

127.0.0.1:6379[1]> flushdb

(empty list or set)

flushall:删除所有資料庫中的所有key(這個指令不管你在哪個資料庫中執行,都會把所有資料庫中的所有key都删除掉)。

127.0.0.1:6379[1]> select 0

127.0.0.1:6379> dbsize

2) "age"

127.0.0.1:6379[1]> set age 1

127.0.0.1:6379[1]> set name zhangsan     

左側用來确定0

和 1

資料庫是否有key

(integer) 2

127.0.0.1:6379[1]> flushall         

 //執行了flushall之後0和1資料庫都變成空了