redis常用操作
之前已经介绍过redis里各种数据类型的赋值操作,在这里则介绍一下各个数据类型的其他常用的操作。
string类型的操作:
使用set命令对已存在的键/值进行赋值的话,会覆盖原有的值:
[root@localhost ~]# redis-cli
127.0.0.1:6379> set key1 zero
OK
127.0.0.1:6379> get key1
"zero"
127.0.0.1:6379> set key1 one
OK
127.0.0.1:6379> get key1
"one"
127.0.0.1:6379>
使用setnx命令则可以在赋值时检测目标键值是否已存在,如果存在会返回0,不会覆盖原有的键值。不存在则返回1并创建该键值:
127.0.0.1:6379> setnx key1 tow
(integer) 0
127.0.0.1:6379> get key1
"one"
127.0.0.1:6379> setnx key2 tow
(integer) 1
127.0.0.1:6379> get key2
"tow"
127.0.0.1:6379>
set命令中的ex参数可以给某个键值设置过期时间:
127.0.0.1:6379> set key3 test ex 10 # 单位为秒
OK
127.0.0.1:6379> get key3
"test"
127.0.0.1:6379> get key3 # 10秒过后数据就过期了
(nil)
127.0.0.1:6379>
setex命令则是先设置过期时间然后再设置值:
127.0.0.1:6379> setex key3 20 test # 给key3设置过期时间为20s,值为test,若key已经存在,会覆盖新的值
OK
127.0.0.1:6379> get key3
"test"
127.0.0.1:6379>
<br>
list类型的操作:
lpush命令可以从左边添加元素,或者可以理解为从上面添加元素。列表中的元素可以理解为左右关系也可以理解为上下关系,主要看个人习惯:
127.0.0.1:6379> LPUSH list2 aaa
(integer) 1
127.0.0.1:6379> LPUSH list2 bbb
(integer) 2
127.0.0.1:6379> LRANGElist2 0 -1
1) "bbb"
2) "aaa"
127.0.0.1:6379>
lpop命令则是可以从左边拿取元素:
127.0.0.1:6379> LPOP list2
"bbb"
127.0.0.1:6379>
linsert命令用于指定在某个元素的前、后位置插入元素,例如:
127.0.0.1:6379> linsert list2 before aaa ccc # 在aaa的前面插入一个元素为ccc
(integer) 2
127.0.0.1:6379> linsert list2 after ccc ddd # 在ccc的后面插入一个元素为ddd
(integer) 3
127.0.0.1:6379> LRANGE list2 0 -1
1) "ccc"
2) "ddd"
3) "aaa"
127.0.0.1:6379>
lset命令用来修改某一个指定的元素:
127.0.0.1:6379> lset list2 0 abc # 将列表中下标为0的元素修改成abc
OK
127.0.0.1:6379> LRANGE list2 0 -1
1) "abc"
2) "ddd"
3) "aaa"
127.0.0.1:6379>
lindex命令查看某个下标的元素:
127.0.0.1:6379> LINDEX list2 0
"abc"
127.0.0.1:6379> LINDEX list2 1
"ddd"
127.0.0.1:6379> LINDEX list2 2
"aaa"
127.0.0.1:6379> LINDEX list2 3
(nil)
127.0.0.1:6379>
llen命令查看某个列表中的元素数量:
127.0.0.1:6379> llen list2
(integer) 3
127.0.0.1:6379>
<br>
set类型的操作:
sadd seta aaa //向集合seta中放入元素
smembers seta //查看集合中的所有元素
127.0.0.1:6379> SADD set4 aaa
(integer) 1
127.0.0.1:6379> SADD set4 bbb
(integer) 1
127.0.0.1:6379> SMEMBERS set4
1) "bbb"
2) "aaa"
127.0.0.1:6379>
srem seta aaa //删除元素
spop seta //随机取出一个元素,删除
127.0.0.1:6379> SREM set4 aaa
(integer) 1
127.0.0.1:6379> spop set4
"bbb"
127.0.0.1:6379> SMEMBERS set4
(empty list or set)
127.0.0.1:6379>
sdiff seta setb //求差集,以seta为标准
sdiffstore setc seta setb //求seta和setb的差集并且将结果存储,存储到了setc里
127.0.0.1:6379> sdiff set1 set2
1) "f"
2) "g"
3) "h"
127.0.0.1:6379> sdiffstore set1 set2 set4
(integer) 8
127.0.0.1:6379>
sinter seta setb //求交集
sinterstore setd seta setb //将交集存储setd
127.0.0.1:6379> SINTER set1 set2
1) "bbb"
2) "aaa"
3) "3"
4) "c"
5) "2"
6) "1"
7) "b"
8) "a"
127.0.0.1:6379> SINTERSTORE set5 set1 set2
(integer) 8
127.0.0.1:6379>
sunion seta setb //求并集
sunionstore sete seta setb //求并集并存储到sete
127.0.0.1:6379> SUNION set1 set2
1) "2"
2) "b"
3) "bbb"
4) "aaa"
5) "c"
6) "3"
7) "1"
8) "a"
127.0.0.1:6379> SUNIONSTORE set6 set1 set2
(integer) 8
127.0.0.1:6379>
sismember命令可以判断一个元素是否属于一个集合,有则返回1,没有则返回0:
127.0.0.1:6379> SISMEMBER set1 aaa
(integer) 1
127.0.0.1:6379> SISMEMBER set1 10
(integer) 0
127.0.0.1:6379>
srandmember命令可以随机取出一个元素,但不删除:
127.0.0.1:6379> SRANDMEMBER set1
"bbb"
127.0.0.1:6379> SRANDMEMBER set1 2 # 末尾的数字表示指定拿取多少个
1) "bbb"
2) "aaa"
127.0.0.1:6379>
<br>
zset类型的操作:
zadd zseta 11 123 //创建有序集合
zrange zseta 0 -1 //显示所有元素,按顺序显示
127.0.0.1:6379> ZADD zset1 0 zero
(integer) 1
127.0.0.1:6379> ZADD zset1 1 one
(integer) 1
127.0.0.1:6379> ZRANGE zset1 0 -1
1) "zero"
2) "one"
127.0.0.1:6379>
zrange zseta 0 -1 withscores //可以带上分值,也就是添加元素时给的数字
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "zero"
2) "0"
3) "one"
4) "1"
127.0.0.1:6379>
zrem zseta 222 //删除指定元素
127.0.0.1:6379> ZREM zset1 zero
(integer) 1
127.0.0.1:6379> ZRANGE zset1 0 -1
1) "one"
127.0.0.1:6379>
zrank zseta 222 //返回元素的索引值也就是下标,索引值从0开始,按score正向排序
127.0.0.1:6379> ZRANK zset1 five
(integer) 5
127.0.0.1:6379> ZRANK zset1 zero
(integer) 0
127.0.0.1:6379> ZRANK zset1 tow
(integer) 2
127.0.0.1:6379> ZRANK zset1 three
(integer) 3
127.0.0.1:6379>
zrevrank zseta 222 //同上,不同的是,按score反序排序
127.0.0.1:6379> zrevrank zset1 five
(integer) 0
127.0.0.1:6379> zrevrank zset1 zero
(integer) 5
127.0.0.1:6379> zrevrank zset1 tow
(integer) 3
127.0.0.1:6379> zrevrank zset1 three
(integer) 2
127.0.0.1:6379>
zrevrange zseta 0 -1 反序显示所有元素
127.0.0.1:6379> ZREVRANGE zset1 0 -1
1) "five"
2) "four"
3) "three"
4) "tow"
5) "one"
6) "zero"
127.0.0.1:6379>
zcard zseta //返回集合中所有元素的个数
127.0.0.1:6379> ZCARD zset1
(integer) 6
127.0.0.1:6379>
zcount zseta 1 10 // 返回分值范围1-10的元素个数
127.0.0.1:6379> zcount zset1 0 10
(integer) 6
127.0.0.1:6379>
zrangebyscore zseta 1 10 // 返回分值范围1-10的元素
127.0.0.1:6379> zrangebyscore zset1 0 10
1) "zero"
2) "one"
3) "tow"
4) "three"
5) "four"
6) "five"
127.0.0.1:6379>
zremrangebyrank zseta 0 2 //删除索引范围0-2的元素,按score正向排序
127.0.0.1:6379> zremrangebyrank zset1 0 2
(integer) 3
127.0.0.1:6379> ZRANGE zset1 0 -1
1) "three"
2) "four"
3) "five"
127.0.0.1:6379>
zremrangebyscore zseta 1 10 //删除分值范围1-10的元素
127.0.0.1:6379> zremrangebyscore zset1 3 4
(integer) 2
127.0.0.1:6379> ZRANGE zset1 0 -1
1) "five"
127.0.0.1:6379>
<br>
hash类型的操作:
//单个创建hash键值
hset user1 name aming
hset user1 age 30
hset user1 job it
127.0.0.1:6379> hset user1 name aming
(integer) 1
127.0.0.1:6379> hset user1 age 30
(integer) 1
127.0.0.1:6379> hset user1 job it
(integer) 1
127.0.0.1:6379>
//批量建立键值对
hmset user2 name aming age 30 job it
127.0.0.1:6379> hmset user2 name aming age 30 job it
OK
127.0.0.1:6379>
// 批量获得键值对
hmget user2 name age job
127.0.0.1:6379> hmget user2 name age job
1) "aming"
2) "30"
3) "it"
127.0.0.1:6379>
//得到所有的键值对
hgetall user1
127.0.0.1:6379> hgetall user1
1) "name"
2) "aming"
3) "age"
4) "30"
5) "job"
6) "it"
127.0.0.1:6379>
//删除指定filed
hdel user2 job
127.0.0.1:6379> hdel user2 job
(integer) 1
127.0.0.1:6379> hgetall user2
1) "name"
2) "aming"
3) "age"
4) "30"
127.0.0.1:6379>
//打印所有的key
hkeys user2
127.0.0.1:6379> hkeys user2
1) "name"
2) "age"
127.0.0.1:6379>
//打印所有的values
hvals user2
127.0.0.1:6379> hvals user2
1) "aming"
2) "30"
127.0.0.1:6379>
//查看hash有几个filed
hlen user2
127.0.0.1:6379> hlen user2
(integer) 2
127.0.0.1:6379>
redis操作键值
keys *可以打印当前redis存储的键:
127.0.0.1:6379> keys *
1) "set3"
2) "set2"
3) "set1"
4) "hash1"
5) "mykey"
6) "hash2"
7) "user2"
8) "user1"
9) "zset1"
10) "list2"
11) "key2"
12) "k2"
13) "key1"
14) "k1"
15) "k3"
16) "set6"
17) "set5"
127.0.0.1:6379>
keys支持模糊匹配:
127.0.0.1:6379> keys k* # 打印k开头的键
1) "key2"
2) "k2"
3) "key1"
4) "k1"
5) "k3"
127.0.0.1:6379>
exists命令可以判断某个键是否存在,存在返回1 ,否则返回0:
127.0.0.1:6379> exists key1
(integer) 1
127.0.0.1:6379> exists key100
(integer) 0
127.0.0.1:6379>
del命令可以删除一个key ,删除成功返回1 ,否则返回0:
127.0.0.1:6379> del key1
(integer) 1
127.0.0.1:6379> get key1
(nil)
127.0.0.1:6379>
EXPIRE命令可以给指定的键设置一个过期时间:
127.0.0.1:6379> EXPIRE key2 100 # 设置key2 100s后过期
(integer) 1
127.0.0.1:6379>
ttl 命令可以查看指定的键还有多长时间过期,单位是秒,当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 否则,返回 key 的剩余生存时间。
127.0.0.1:6379> ttl key2
(integer) 49
127.0.0.1:6379> ttl key2
(integer) -2
127.0.0.1:6379> get key2
(nil)
127.0.0.1:6379> ttl user1
(integer) -1
127.0.0.1:6379>
select命令可以选择一个数据库,redis默认有6个数据库,通过0-6的数字就可以选择进入哪一个库:
127.0.0.1:6379> select 0 # 代表选择当前数据库,默认进入 0 数据库
OK
127.0.0.1:6379> SELECT 1 # 选择 1 数据库
OK
127.0.0.1:6379[1]> keys * # 1 数据库下没有键值数据
(empty list or set)
127.0.0.1:6379[1]>
move set1 1 // 把set1移动到 1 数据库里:
127.0.0.1:6379> MOVE set1 1
(integer) 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
1) "set1"
127.0.0.1:6379[1]>
persist key1 //取消key1的过期时间
127.0.0.1:6379> EXPIRE mykey 1000
(integer) 1
127.0.0.1:6379> TTL mykey
(integer) 992
127.0.0.1:6379> PERSIST mykey
(integer) 1
127.0.0.1:6379> TTL mykey
(integer) -1
127.0.0.1:6379>
randomkey //随机返回一个key
127.0.0.1:6379> randomkey
"zset1"
127.0.0.1:6379> randomkey
"user1"
127.0.0.1:6379> randomkey
"user1"
127.0.0.1:6379> randomkey
"list2"
127.0.0.1:6379>
rename oldname newname //重命名key
127.0.0.1:6379> rename mykey newmykey
OK
127.0.0.1:6379>
type key1 //返回键的类型
127.0.0.1:6379> type newmykey
string
127.0.0.1:6379> type set2
set
127.0.0.1:6379> type list2
list
127.0.0.1:6379> type hash2
hash
127.0.0.1:6379> type zset1
zset
127.0.0.1:6379> type key4 # 不存在的key则返回none
none
127.0.0.1:6379>
redis服务
dbsize命令可以返回当前数据库中key的数目
127.0.0.1:6379> dbsize
(integer) 14
127.0.0.1:6379>
info //返回redis数据库状态信息
127.0.0.1:6379> info
# Server
redis_version:4.0.1
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:6a13dbd019c997f4
redis_mode:standalone
os:Linux 3.10.0-327.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:2801
run_id:a61419362a08cf720c7e7048ec1b0f9816a7594c
tcp_port:6379
uptime_in_seconds:9412
uptime_in_days:0
hz:10
lru_clock:4959909
executable:/root/redis-server
config_file:/etc/redis.conf
# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:830600
used_memory_human:811.13K
used_memory_rss:7970816
used_memory_rss_human:7.60M
used_memory_peak:830600
used_memory_peak_human:811.13K
used_memory_peak_perc:100.09%
used_memory_overhead:816126
used_memory_startup:765576
used_memory_dataset:14474
used_memory_dataset_perc:22.26%
total_system_memory:1913536512
total_system_memory_human:1.78G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:9.59
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0
# Persistence
loading:0
rdb_changes_since_last_save:4
rdb_bgsave_in_progress:0
rdb_last_save_time:1514908805
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:6561792
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_last_cow_size:0
aof_current_size:3007
aof_base_size:955
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:1
total_commands_processed:156
instantaneous_ops_per_sec:0
total_net_input_bytes:5717
total_net_output_bytes:13143
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:3
evicted_keys:0
keyspace_hits:74
keyspace_misses:16
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:4229
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
# Replication
role:master
connected_slaves:0
master_replid:c4ed4074d76e9db7ebacfdcc16f6e6115f910bf2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:15.41
used_cpu_user:0.49
used_cpu_sys_children:0.12
used_cpu_user_children:0.00
# Cluster
cluster_enabled:0
# Keyspace
db0:keys=14,expires=0,avg_ttl=0
db1:keys=1,expires=0,avg_ttl=0
127.0.0.1:6379>
flushdb //清空当前数据库中所有的键:
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
1) "set1"
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
bgsave //保存数据到 rdb文件中,会在后台运行:
127.0.0.1:6379[1]> bgsave
Background saving started
127.0.0.1:6379[1]>
save //作用同上,但是在前台运行
config get * //获取所有配置参数,奇数行是键,偶数行是值:
127.0.0.1:6379[1]> config get *
1) "dbfilename"
2) "dump.rdb"
3) "requirepass"
4) ""
5) "masterauth"
6) ""
7) "cluster-announce-ip"
8) ""
9) "unixsocket"
10) ""
11) "logfile"
12) "/var/log/redis.log"
13) "pidfile"
14) "/var/run/redis_6379.pid"
15) "slave-announce-ip"
16) ""
17) "maxmemory"
18) "0"
19) "maxmemory-samples"
20) "5"
21) "timeout"
22) "0"
23) "active-defrag-threshold-lower"
24) "10"
25) "active-defrag-threshold-upper"
26) "100"
27) "active-defrag-ignore-bytes"
28) "104857600"
29) "active-defrag-cycle-min"
30) "25"
31) "active-defrag-cycle-max"
32) "75"
33) "auto-aof-rewrite-percentage"
34) "100"
35) "auto-aof-rewrite-min-size"
36) "67108864"
37) "hash-max-ziplist-entries"
38) "512"
39) "hash-max-ziplist-value"
40) "64"
41) "list-max-ziplist-size"
42) "-2"
43) "list-compress-depth"
44) "0"
45) "set-max-intset-entries"
46) "512"
47) "zset-max-ziplist-entries"
48) "128"
49) "zset-max-ziplist-value"
50) "64"
51) "hll-sparse-max-bytes"
52) "3000"
53) "lua-time-limit"
54) "5000"
55) "slowlog-log-slower-than"
56) "10000"
57) "latency-monitor-threshold"
58) "0"
59) "slowlog-max-len"
60) "128"
61) "port"
62) "6379"
63) "cluster-announce-port"
64) "0"
65) "cluster-announce-bus-port"
66) "0"
67) "tcp-backlog"
68) "511"
69) "databases"
70) "16"
71) "repl-ping-slave-period"
72) "10"
73) "repl-timeout"
74) "60"
75) "repl-backlog-size"
76) "1048576"
77) "repl-backlog-ttl"
78) "3600"
79) "maxclients"
80) "10000"
81) "watchdog-period"
82) "0"
83) "slave-priority"
84) "100"
85) "slave-announce-port"
86) "0"
87) "min-slaves-to-write"
88) "0"
89) "min-slaves-max-lag"
90) "10"
91) "hz"
92) "10"
93) "cluster-node-timeout"
94) "15000"
95) "cluster-migration-barrier"
96) "1"
97) "cluster-slave-validity-factor"
98) "10"
99) "repl-diskless-sync-delay"
100) "5"
101) "tcp-keepalive"
102) "300"
103) "cluster-require-full-coverage"
104) "yes"
105) "no-appendfsync-on-rewrite"
106) "no"
107) "slave-serve-stale-data"
108) "yes"
109) "slave-read-only"
110) "yes"
111) "stop-writes-on-bgsave-error"
112) "yes"
113) "daemonize"
114) "yes"
115) "rdbcompression"
116) "yes"
117) "rdbchecksum"
118) "yes"
119) "activerehashing"
120) "yes"
121) "activedefrag"
122) "no"
123) "protected-mode"
124) "yes"
125) "repl-disable-tcp-nodelay"
126) "no"
127) "repl-diskless-sync"
128) "no"
129) "aof-rewrite-incremental-fsync"
130) "yes"
131) "aof-load-truncated"
132) "yes"
133) "aof-use-rdb-preamble"
134) "no"
135) "lazyfree-lazy-eviction"
136) "no"
137) "lazyfree-lazy-expire"
138) "no"
139) "lazyfree-lazy-server-del"
140) "no"
141) "slave-lazy-flush"
142) "no"
143) "maxmemory-policy"
144) "noeviction"
145) "loglevel"
146) "notice"
147) "supervised"
148) "no"
149) "appendfsync"
150) "everysec"
151) "syslog-facility"
152) "local0"
153) "appendonly"
154) "yes"
155) "dir"
156) "/data/redis_data"
157) "save"
158) "900 1 300 10 60 10000"
159) "client-output-buffer-limit"
160) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
161) "unixsocketperm"
162) "0"
163) "slaveof"
164) ""
165) "notify-keyspace-events"
166) ""
167) "bind"
168) "127.0.0.1"
127.0.0.1:6379[1]>
config get dir //通过键获取某个配置参数
127.0.0.1:6379[1]> config get dir
1) "dir"
2) "/data/redis_data"
127.0.0.1:6379[1]>
config set dir //更改配置参数
127.0.0.1:6379[1]> CONFIG SET timeout 100
OK
127.0.0.1:6379[1]> CONFIG GET timeout
1) "timeout"
2) "100"
127.0.0.1:6379[1]>
数据恢复: 首先定义或者确定dir目录和dbfilename,然后把备份的rdb文件放到dir目录下面,重启redis服务即可恢复数据。
redis安全设置
redis默认会监听所有ip,这样的话所有ip都能来进行连接,所以需要设置指定的监听ip来提高安全性。
redis默认端口是6379,如果使用默认端口很容易被黑客扫到,所以设置一个其他的监听端口也可以提高安全性。
redis默认是没有密码的,所以还需要设置密码:
[root@localhost ~]# vim /etc/redis.conf
# 配置文件中增加这一行,password改为你的密码
requirepass password
修改完之后重启redis服务:
killall redis-server
redis-server /etc/redis.conf
这时候虽然可以还进入redis命令行,但是不能够执行命令了:
[root@localhost ~]# redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379>
需要通过 -a 选项指定密码登录:
[root@localhost ~]# redis-cli -a 'password'
127.0.0.1:6379> keys *
1) "newmykey"
2) "user1"
3) "user2"
4) "hash2"
5) "list2"
6) "set3"
7) "k1"
8) "zset1"
9) "set6"
10) "hash1"
11) "k2"
12) "k3"
13) "set2"
14) "set5"
127.0.0.1:6379>
因为config命令可以获得所有的配置参数,所以有一定的风险,但是我们可以将config命令改名,同样的需要编辑配置文件:
[root@localhost ~]# vim /etc/redis.conf
# 配置文件中增加这一行,newcommand自定义为你的命令
rename-command CONFIG newcommand
修改完之后重启redis服务:
killall redis-server
redis-server /etc/redis.conf
再次进入命令行可以发现原本的config命令不能用了:
[root@localhost ~]# redis-cli -a 'password'
127.0.0.1:6379> config get dir
(error) ERR unknown command 'config'
127.0.0.1:6379> newcommand get dir # 使用自定义的命令
1) "dir"
2) "/data/redis_data"
127.0.0.1:6379>
如果不需要使用config命令的话,可以在配置文件中添加这一行禁用config命令:
rename-command CONFIG ""