`

redis命令行管理

 
阅读更多

redis命令行管理

本文主要介绍使用redis命令行来查看和管理redis数据库。redis命令行的全集链接:http://redis.io/commands

使用redis-cli登录redis数据库:

[baichuan@zjdw-odmz-0009 ~]$ ./redis-2.8.19/bin/redis-cli -h
redis-cli 2.8.19

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
  -s <socket>        Server socket (overrides hostname and port).
  -a <password>      Password to use when connecting to the server.
  -r <repeat>        Execute specified command N times.
  -i <interval>      When -r is used, waits <interval> seconds per command.
                     It is possible to specify sub-second times like -i 0.1.
  -n <db>            Database number.
  -x                 Read last argument from STDIN.
  -d <delimiter>     Multi-bulk delimiter in for raw formatting (default: \n).
  -c                 Enable cluster mode (follow -ASK and -MOVED redirections).
  --raw              Use raw formatting for replies (default when STDOUT is
                     not a tty).
  --no-raw           Force formatted output even when STDOUT is not a tty.
  --csv              Output in CSV format.
  --stat             Print rolling stats about server: mem, clients, ...
  --latency          Enter a special mode continuously sampling latency.
  --latency-history  Like --latency but tracking latency changes over time.
                     Default time interval is 15 sec. Change it using -i.
  --slave            Simulate a slave showing commands received from the master.
  --rdb <filename>   Transfer an RDB dump from remote server to local file.
  --pipe             Transfer raw Redis protocol from stdin to server.
  --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.
                     no reply is received within <n> seconds.
                     Default timeout: 30. Use 0 to wait forever.
  --bigkeys          Sample Redis keys looking for big keys.
  --scan             List all keys using the SCAN command.
  --pattern <pat>    Useful with --scan to specify a SCAN pattern.
  --intrinsic-latency <sec> Run a test to measure intrinsic system latency.
                     The test will run for the specified amount of seconds.
  --eval <file>      Send an EVAL command using the Lua script at <file>.
  --help             Output this help and exit.
  --version          Output version and exit.

Examples:
  cat /etc/passwd | redis-cli -x set mypasswd
  redis-cli get mypasswd
  redis-cli -r 100 lpush mylist x
  redis-cli -r 100 -i 1 info | grep used_memory_human:
  redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3
  redis-cli --scan --pattern '*:12345*'

  (Note: when using --eval the comma separates KEYS[] from ARGV[] items)

When no command is given, redis-cli starts in interactive mode.
Type "help" in interactive mode for information on available commands.

[baichuan@zjdw-odmz-0009 ~]$ ./redis-2.8.19/bin/redis-cli -h 115.239.138.137 -p 45981
115.239.138.137:45981>

 登录上去后,需要使用AUTH命令来进行授权:

115.239.138.137:45981[14]> help AUTH

  AUTH password
  summary: Authenticate to the server
  since: 1.0.0
  group: connection

115.239.138.137:45981[14]> 
115.239.138.137:45981> AUTH xxx   (redis数据库的密码)
OK

 然后就可以使用其他命令来操作redis数据库了。

1、SELECT命令

Select the DB with having the specified zero-based numeric index. New connections always use DB 0.

SELECT命令从当前连接切换到指定的数据库,这个数据库ID是在redis配置文件中配置好的从0开始的数字索引。新连接总是使用数据库0。

115.239.138.137:45981[14]> help SELECT

  SELECT index
  summary: Change the selected database for the current connection
  since: 1.0.0
  group: connection

115.239.138.137:45981[14]> SELECT 6
OK
115.239.138.137:45981[6]> 

 2、PING命令

Returns PONG if no argument is provided, otherwise return a copy of the argument as a bulk. This command is often used to test if a connection is still alive, or to measure latency.

PING命令如果没有提供参数,则返回PONG,否则返回参数的一个复制作为回应。这个命令通常被用测试连接是否还是活着的,或者测试延迟。

115.239.138.137:45981[6]> help ping

  PING -
  summary: Ping the server
  since: 1.0.0
  group: connection

115.239.138.137:45981[6]> ping 
PONG
115.239.138.137:45981[6]> ping 16
"16"
115.239.138.137:45981[6]> 

 3、QUIT命令

Ask the server to close the connection. The connection is closed as soon as all pending replies have been written to the client.

QUIT命令用于告诉服务器关闭这个连接。这个连接在所有挂起的回复都已经写到客户端后马上关闭。

115.239.138.137:45981[6]> help QUIT

  QUIT -
  summary: Close the connection
  since: 1.0.0
  group: connection

115.239.138.137:45981[6]> QUIT
[baichuan@zjdw-odmz-0009 ~]$

 4、SET命令

Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.

设置key来保持字符串value。如果key已经保持了一个值,它会被覆盖,无论它的类型是什么。任何以前和这个key关联的时间都在SET操作成功后被丢弃。

Options

Starting with Redis 2.6.12 SET supports a set of options that modify its behavior:

  • EX seconds -- Set the specified expire time, in seconds.
  • PX milliseconds -- Set the specified expire time, in milliseconds.
  • NX -- Only set the key if it does not already exist.
  • XX -- Only set the key if it already exist.

Note: Since the SET command options can replace SETNX, SETEX, PSETEX, it is possible that in future versions of Redis these three commands will be deprecated and finally removed.

Return value

Simple string reply: OK if SET was executed correctly. Null reply: a Null Bulk Reply is returned if the SET operation was not performed because the user specified the NX or XX option but the condition was not met.

115.239.138.137:45981[6]> help SET

  SET key value [EX seconds] [PX milliseconds] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0
  group: string

115.239.138.137:45981[6]> 127.0.0.1:6379[1]> SET hello "welcome to the redis"
OK
127.0.0.1:6379[1]> GET hello
"welcome to the redis"
127.0.0.1:6379[1]> SET hello "already exist" NX
(nil)
127.0.0.1:6379[1]> GET hello
"welcome to the redis"
127.0.0.1:6379[1]> SET hello "already exist" XX
OK
127.0.0.1:6379[1]> GET hello
"already exist"
127.0.0.1:6379[1]> 127.0.0.1:6379[1]> KEYS *
1) "hello"
127.0.0.1:6379[1]> DEL hello
(integer) 1
127.0.0.1:6379[1]> GET hello
(nil)
127.0.0.1:6379[1]> KEYS *
(empty list or set)
127.0.0.1:6379[1]> 

 5、GET命令

Get the value of key. If the key does not exist the special value nil is returned. An error is returned if the value stored at key is not a string, because GET only handles string values.

获取key的对应的值。如果key不存在,则特殊值nil被返回。如果key所存储的值不是一个string,则返回错误,因为GET只能处理string值。

Return value

Bulk string reply: the value of key, or nil when key does not exist.

115.239.138.137:45981[6]> help GET

  GET key
  summary: Get the value of a key
  since: 1.0.0
  group: string

115.239.138.137:45981[6]> get hello
(nil)

 6、KEYS命令

Returns all keys matching pattern.

返回所有的复合pattern模式的key。

While the time complexity for this operation is O(N), the constant times are fairly low. For example, Redis running on an entry level laptop can scan a 1 million key database in 40 milliseconds.

尽管这个操作的时间复杂度是O(N),但是它的常量时间非常小。例如,在一个普通的笔记本上运行的Redis,只需要40毫秒就可以扫面一个100万个key的数据库。

Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.

注意:把KEYS当成一个命令,它在生成环境中使用时需要极其小心。当它在一个大型数据库上执行时会破坏性能。这个命令是为调试和特殊操作设计的,例如改变key空间的布局。不要在你的普通应用程序代码中使用KEYS。如果你在寻找一种方式来查找你的key空间的一个子集的keys,考虑使用SCAN或sets。

Supported glob-style patterns:

  • h?llo matches hello, hallo and hxllo
  • h*llo matches hllo and heeeello
  • h[ae]llo matches hello and hallo, but not hillo
  • h[^e]llo matches hallo, hbllo, ... but not hello
  • h[a-b]llo matches hallo and hbllo

Use \ to escape special characters if you want to match them verbatim.

Return value

Array reply: list of keys matching pattern.

115.239.138.137:45981[6]> help KEYS

  KEYS pattern
  summary: Find all keys matching the given pattern
  since: 1.0.0
  group: generic

115.239.138.137:45981[6]> 
OK

115.239.138.137:45981[6]>  MSET one 1 two 2 three 3 four 4

OK
115.239.138.137:45981[6]>  KEYS *o*

1) "four"
2) "two"
3) "one"

115.239.138.137:45981[6]>  KEYS t??

1) "two"

115.239.138.137:45981[6]>  KEYS *

1) "four"
2) "two"
3) "three"
4) "one"

115.239.138.137:45981[6]>  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics