1. 启动redis-server
在Linux下建议先修改/创建一个redis的conf文件
一个简单的conf文件示例如下:
- /ect/softwares/redis/conf/6379.conf
#配置文件进行了精简,完整配置可自行和官方提供的完整conf文件进行对照。端口号自行>对应修改
#后台后动的意思
daemonize yes
#端口号
port 6379
#IP绑定,redis不建议对公网开放,直接绑定0.o.o.0没毛病
bind 0.0.0.0
redis-server /ect/softwares/redis/conf/6379.conf
2. redis-cli 连接 redis-server
可以使用--help
查看redis-cli的各种参数意义:
[root@lazyfennec redis-5.0.14]# redis-cli --help
redis-cli 5.0.14
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.
You can also use the REDISCLI_AUTH environment
variable to pass this password more safely
(if both are used, this argument takes predecence).
-u <uri> Server URI.
-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.
If you use this mode in an interactive session it runs
forever displaying real-time stats. Otherwise if --raw or
--csv is specified, or if you redirect the output to a non
TTY, it samples the latency for 1 second (you can use
-i to change the interval), then produces a single output
and exits.
--latency-history Like --latency but tracking latency changes over time.
Default time interval is 15 sec. Change it using -i.
--latency-dist Shows latency as a spectrum, requires xterm 256 colors.
Default time interval is 1 sec. Change it using -i.
--lru-test <keys> Simulate a cache workload with an 80-20 distribution.
--replica Simulate a replica 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 keys with many elements (complexity).
--memkeys Sample Redis keys looking for keys consuming a lot of memory.
--memkeys-samples <n> Sample Redis keys looking for keys consuming a lot of memory.
And define number of key elements to sample
--hotkeys Sample Redis keys looking for hot keys.
only works when maxmemory-policy is *lfu.
--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>.
--ldb Used with --eval enable the Redis Lua debugger.
--ldb-sync-mode Like --ldb but uses the synchronous Lua debugger, in
this mode the server is blocked and script changes are
not rolled back from the server memory.
--cluster <command> [args...] [opts...]
Cluster Manager command and arguments (see below).
--verbose Verbose mode.
--no-auth-warning Don't show warning message when using password on command
line interface.
--help Output this help and exit.
--version Output version and exit.
Cluster Manager Commands:
Use --cluster help to list all available cluster manager commands.
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
and settings.
- 连接redis-server
可以根据以上help的内容可以得知,如果是连接127.0.0.1 则可以不写 -h 127.0.0.1,因为默认就是127.0.0.1,如果端口号是 6379 那么也可以不写,下面给出连接IP为192.168.1.7 端口号为6666的连接方式。
redis-cli -h 192.168.1.7 -p 6666
3. String 类型相关
-
set
: 设置一个key的值,可以是数字哦~
127.0.0.1:6379> set hello "hello world"
OK
-
get
: 获取一个key的值
127.0.0.1:6379> get hello
"hello world"
-
incr
: 将一个值为数字的内容自增1,如果这个key原来是nil(也就是没有存储数据),那么第一次会自动设置成1哦
127.0.0.1:6379> incr num
(integer) 1
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> incr num
(integer) 3
-
decr
: 将一个值为数字的内容自减1
127.0.0.1:6379> decr num
(integer) 2
127.0.0.1:6379> decr num
(integer) 1
127.0.0.1:6379> decr num
(integer) 0
127.0.0.1:6379> decr num
(integer) -1
-
append
: 将新的内容添加到一个值的后面
127.0.0.1:6379> append test "hello "
(integer) 6
127.0.0.1:6379> append test "neco"
(integer) 10
127.0.0.1:6379> get test
"hello neco"
- 其他内容
4. Hash 类型相关
-
hset
: 在一个hash类型中设置一个key-value值
127.0.0.1:6379> hset hash1 field1 value1
(integer) 1
127.0.0.1:6379> hset hash1 field2 value2
(integer) 1
127.0.0.1:6379> hset hash1 field3 value3
(integer) 1
-
hget
: 获取一个hash类型中的一个key的value值
127.0.0.1:6379> hget hash1 field1
"value1"
-
hgetall
: 获取一个hash类型中的所有 key-value 键值对
127.0.0.1:6379> hgetall hash1
1) "field1"
2) "value1"
3) "field2"
4) "value2"
5) "field3"
6) "value3"
-
hlen
: 获取一个hash类型中的 key-value 数量
127.0.0.1:6379> hlen hash1
(integer) 3
-
hkeys
: 获取一个hash类型的所有keys
127.0.0.1:6379> hkeys hash1
1) "field1"
2) "field2"
3) "field3"
-
hvals
: 获取一个hash类型的所有values
127.0.0.1:6379> hvals hash1
1) "value1"
2) "value2"
3) "value3"
-
hmset
: 设置多个 key-value 键值对到一个hash类型中
127.0.0.1:6379> hmset hash2 field1 "hello " field2 "world"
OK
-
hmget
: 获取一个hash类型中的多个key的值
127.0.0.1:6379> hmget hash2 field1 field2
1) "hello "
2) "world"
- 其他内容
5. List 类型相关
-
lpush
: 往一个list前面插入一个或多个值
127.0.0.1:6379> lpush list1 1
(integer) 1
127.0.0.1:6379> lpush list1 2 3
(integer) 3
-
lpop
: 获取并从列表中删除第一个值
127.0.0.1:6379> lpop list1
"3"
127.0.0.1:6379> lpop list1
"2"
127.0.0.1:6379> lpop list1
"1"
127.0.0.1:6379> lpop list1
(nil)
-
lrange
: 获取list中指定范围内的内容,其中-1 表示最后一个哦~
127.0.0.1:6379> lrange list1 0 -1
1) "one"
2) "2"
3) "-1"
127.0.0.1:6379> lrange list1 0 10
1) "one"
2) "2"
3) "-1"
-
lset
: 根据index设置list中的一个值
127.0.0.1:6379> lpush list1 1 2 3
(integer) 3
127.0.0.1:6379> lrange list1 0 10
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> lset list1 0 one
OK
127.0.0.1:6379> lrange list1 0 10
1) "one"
2) "2"
3) "1"
127.0.0.1:6379> lset list1 -1 -1
OK
127.0.0.1:6379> lrange list1 0 10
1) "one"
2) "2"
3) "-1"
-
lindex
: 获取list指定index的值
127.0.0.1:6379> lindex list1 0
"one"
127.0.0.1:6379> lindex list1 1
"2"
127.0.0.1:6379> lindex list1 2
"-1"
127.0.0.1:6379> lindex list1 -1
"-1"
-
llen
: 获取list的容量大小
127.0.0.1:6379> llen list1
(integer) 3
- 其他内容
6. Set 类型相关
-
sadd
: 添加一个或多个值到set中
127.0.0.1:6379> sadd set1 1 2 3
(integer) 3
127.0.0.1:6379> sadd set1 4
(integer) 1
-
spop
: 从set中删除并返回随机元素,后边可以加上要删除并且返回的个数,如果不加上,那么默认是一个。
127.0.0.1:6379> spop set1
"2"
127.0.0.1:6379> spop set1 2
1) "3"
2) "1"
-
smembers
: 罗列set中的所有元素
127.0.0.1:6379> smembers set1
1) "1"
2) "2"
3) "3"
4) "4"
-
scard
: 获取set的元素个数
127.0.0.1:6379> scard set1
(integer) 4
-
sdiff
: 将多个set相减,返回内容,比如set1 - set2, 那么返回的将是set1中除了set2中所有元素的元素
127.0.0.1:6379> sadd set1 1 2 3 4
(integer) 4
127.0.0.1:6379> sadd set2 2 3 4 5
(integer) 4
127.0.0.1:6379> sdiff set1 set2
1) "1"
-
sinter
: 相交,即数学意义上的集合相交,返回的是相交的内容
127.0.0.1:6379> sinter set1 set2
1) "2"
2) "3"
3) "4"
-
sunion
: 合并,即数学意义上的集合并集,返回的是并集内容
127.0.0.1:6379> sunion set1 set2
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
-
sismember
: 判断一个值是否在一个set中
127.0.0.1:6379> sismember set1 1
(integer) 1
127.0.0.1:6379> sismember set1 5
(integer) 0
- 其他内容
7. Sorted Set 内容相关
这个类型表示 可排序的集合 重要的一点,就是这个类型在添加的时候,需要加上一个分数
-
zadd
: 添加一个或多个到一个 sorted set中
127.0.0.1:6379> zadd zset1 1 "one"
(integer) 1
127.0.0.1:6379> zadd zset1 2 "two" 3 "three"
(integer) 2
-
zrange
: 返回根据分数带正序(从小到大)排序的范围内的列表,可以在后边加上withscores 把分数一并返回
127.0.0.1:6379> ZRANGE zset1 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
-
zrevrange
: 返回根据分数带逆序(从大到小)排序的范围内的列表,可以在后边加上withscores 把分数一并返回
127.0.0.1:6379> zrevrange zset1 0 -1 withscores
1) "three"
2) "3"
3) "two"
4) "2"
5) "one"
6) "1"
-
zcard
: 获取sorted set中元素的数量
127.0.0.1:6379> zcard zset1
(integer) 3
-
zcount
: 获取sorted set 中分数在一定范围内的元素数量
127.0.0.1:6379> zcount zset1 1 2
(integer) 2
127.0.0.1:6379> zcount zset1 1 3
(integer) 3
127.0.0.1:6379> zcount zset1 1 1
(integer) 1
-
zscore
: 获取sorted set中一个元素的分数
127.0.0.1:6379> zscore zset1 one
"1"
127.0.0.1:6379> zscore zset1 two
"2"
- 其他内容
8. 其他一些比较重要的操作指令
-
del
: 删除指定的key值
127.0.0.1:6379> del hello
(integer) 1
-
exists
: 判断key是否存在
127.0.0.1:6379> exists hello
(integer) 0
127.0.0.1:6379> exists zset1
(integer) 1
-
type
: 获取key值中的数据类型是什么类型
127.0.0.1:6379> type hello
string
127.0.0.1:6379> type hash1
hash
127.0.0.1:6379> type list1
list
127.0.0.1:6379> type zset1
zset
127.0.0.1:6379> type set1
set
-
flushdb
: 清空数据
127.0.0.1:6379> flushdb
OK
9. 更多内容
请查看官网 https://redis.io/
- 官网指令查看页面
https://redis.io/commands/
如果觉得有收获,欢迎点赞和评论,更多知识,请点击关注查看我的主页信息哦~
网友评论