标签:
Redis常用命令
Redis提供了丰富的命令对数据库和各种数据库类型进行操作,这些命令可以再Linux终端使用。
键值相关命令:
Keys:返回满足给定pattern的所有key
用表达式*表示取出所有的key
127.0.0.1:6379> keys * 1) "age" 2) "name" 3) "age2" 127.0.0.1:6379> keys a* 1) "age" 2) "age2" 127.0.0.1:6379> |
Exists:确认一个key是否存在
127.0.0.1:6379> exists name (integer) 1 127.0.0.1:6379> exists toto (integer) 0 127.0.0.1:6379> |
del:删除一个key.
127.0.0.1:6379> set toto1 toto OK 127.0.0.1:6379> keys * 1) "name" 2) "age2" 3) "age" 4) "toto1" 127.0.0.1:6379> del toto1 //删除toto1这个键。 (integer) 1 127.0.0.1:6379> del toto1 //再次删除的时候删除不成功 (integer) 0 127.0.0.1:6379> exists toto1 // (integer) 0 127.0.0.1:6379> keys * 1) "name" 2) "age2" 3) "age" 127.0.0.1:6379> |
expire:设置一个key的过期时间
在本例中,设置addr这个key的过期时间是10秒,然后不断的用ttl来获取这个key的有效时长,直至-1,说明此值已过期。
expiree addr 10表示:对addr设置过期时间为10秒。
ttl查询剩余的过期时间,当返回-1的时候表示已经过期了。
127.0.0.1:6379> set age 20 //设置一个key为age的值。 OK 127.0.0.1:6379> get age "20" 127.0.0.1:6379> expire age 10 (integer) 1 127.0.0.1:6379> ttl age (integer) 8 127.0.0.1:6379> ttl age (integer) 5 127.0.0.1:6379> ttl age (integer) 3 127.0.0.1:6379> ttl age (integer) -2 127.0.0.1:6379> ttl age (integer) -2 127.0.0.1:6379> ttl age (integer) -2 127.0.0.1:6379> exists age (integer) 0 127.0.0.1:6379> keys * //查看age是否还存在 1) "name" 2) "age2" 127.0.0.1:6379> |
move:将当前数据库中的key转移到其它数据库中。
选择数据库,要用到select,select 0表示选择到当前数据库。Select 1表示第二个数据库,一共是从0到15个数据库,总共是16个数据库。(注意,进入redis的时候默认进入的是0数据库)
127.0.0.1:6379> select 0 OK 127.0.0.1:6379> set age 30 OK 127.0.0.1:6379> get age "30" 127.0.0.1:6379> move age 1 (integer) 1 127.0.0.1:6379> get age (nil) 127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> get age "30" 127.0.0.1:6379[1]> |
persist:移除给定key的过期时间
127.0.0.1:6379[1]> select 1 OK 127.0.0.1:6379[1]> keys * 1) "age" 127.0.0.1:6379[1]> expire age 300 //设置age的过期时间为300 (integer) 1 127.0.0.1:6379[1]> ttl age (integer) 297 127.0.0.1:6379[1]> ttl age (integer) 293 127.0.0.1:6379[1]> persist age //取消age的过期 (integer) 1 127.0.0.1:6379[1]> ttl age (integer) -1 127.0.0.1:6379[1]> exists age (integer) 1 127.0.0.1:6379[1]> keys * 1) "age" 127.0.0.1:6379[1]> |
randomkey:随机返回key空间的一个key
127.0.0.1:6379[1]> keys * 1) "gender" 2) "name" 3) "address" 4) "age" 127.0.0.1:6379[1]> randomkey //随机获得一个key "gender" 127.0.0.1:6379[1]> randomkey "address" 127.0.0.1:6379[1]> randomkey "gender" 127.0.0.1:6379[1]> |
rename:重命名key
127.0.0.1:6379[1]> keys * 1) "gender" 2) "name" 3) "address" 4) "age" 127.0.0.1:6379[1]> rename age nianling 将age这个key重新命名为nianling OK 127.0.0.1:6379[1]> keys * 1) "gender" 2) "name" 3) "address" 4) "nianling" 127.0.0.1:6379[1]> |
type:返回键的数据类型
127.0.0.1:6379[1]> type name string 127.0.0.1:6379[1]> |
服务器的相关命令
ping:测试连接是否存活
echo:在命令行打印一些内容
127.0.0.1:6379> echo toto "toto" 127.0.0.1:6379> echo toto2 "toto2" |
select:选择数据库.Redis数据库编号从0~15,我们可以选择任意一个数据库来进行数据的存取。
当选择16时,报错,说明没有编号为16的这个数据库。
quit:退出连接,或者exit
dbsize:返回当前数据库中key的数目。
127.0.0.1:6379> keys * 1) "age2" 2) "name" 127.0.0.1:6379> dbsize (integer) 2 //返回0表示有2个key 127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> keys * 1) "gender" 2) "name" 3) "address" 4) "nianling" 127.0.0.1:6379[1]> dbsize (integer) 4 //返回4表示有4个key 127.0.0.1:6379[1]> |
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 127.0.0.1:6379[1]> |
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]> |
127.0.0.1:6379[1]> config get timeout 1) "timeout" 2) "0" 127.0.0.1:6379[1]> |
flushdb:删除当前选择数据库中的所有key(也就是说flushdb的作用是清空当前数据库)
127.0.0.1:6379[2]> select 1 OK 127.0.0.1:6379[1]> set age 20 OK 127.0.0.1:6379[1]> set name zhangsan OK 127.0.0.1:6379[1]> set gender male OK 127.0.0.1:6379[1]> keys * 1) "name" 2) "gender" 3) "age" 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]> |
flushall:删除所有数据库中的所有key(这个命令不管你在哪个数据库中执行,都会把所有数据库中的所有key都删除掉)。
127.0.0.1:6379[1]> select 0 OK 127.0.0.1:6379> dbsize (integer) 3 127.0.0.1:6379> keys * 1) "name" 2) "age" 3) "age2" 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 age 1 OK 127.0.0.1:6379[1]> set name zhangsan 左侧用来确定0 和 1 数据库是否有key OK 127.0.0.1:6379[1]> keys * 1) "name" 2) "age" 127.0.0.1:6379[1]> dbsize (integer) 2 127.0.0.1:6379[1]> flushall //执行了flushall之后0和1数据库都变成空了 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 * (empty list or set) 127.0.0.1:6379> |
05_NoSQL数据库之Redis数据库:Redis的常用命令,键值相关命令和服务器相关命令
标签:
原文地址:http://blog.csdn.net/tototuzuoquan/article/details/43817487