概述
auth password
127.0.0.1:6380> help auth
AUTH password
summary: Authenticate to the server
since: 1.0.0
group: connection
在redis-server端的配置文件中设置了密码(配置项:requirepass 123456
),客户端连接后需要验证密码才可以操作:
[root@zd-ecs03 ~]# redis-cli -p 6380
127.0.0.1:6380> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6380> auth 123456
OK
127.0.0.1:6380>
select index
select:选择一个数据库,下标值从0开始,一个新连接默认连接的数据库是DB0。
127.0.0.1:6380> help SELECT
SELECT index
summary: Change the selected database for the current connection
since: 1.0.0
group: connection
quit
请求服务器关闭连接。连接将会尽可能快的将未完成的客户端请求完成处理。
127.0.0.1:6380> help quit
QUIT -
summary: Close the connection
since: 1.0.0
group: connection
数据类型
Redis | string | hash | list | set | sorted_set |
---|---|---|---|---|---|
Java | String | HashMap | LinkedList | HashSet | TreeSet |
可以用Java的类型来类比一下Redis的类型,只是类似…
redis-server设置了密码,redis-cli连接时输入密码:
[root@zd-ecs03 ~]# redis-cli -p 6380
127.0.0.1:6380> auth IBMlc4lc4
OK
127.0.0.1:6380>
1、全局命令
1.1、keys pattern
keys *
查看所有的key
127.0.0.1:6380> set hello world
OK
127.0.0.1:6380> set java jedis
OK
127.0.0.1:6380> set python redis-py
OK
127.0.0.1:6380> keys *
keys h*
查看h开头的key
例:
127.0.0.1:6380> keys h*
1) "hello"
127.0.0.1:6380> keys jav*
1) "java"
127.0.0.1:6380> keys jav?
1) "java"
127.0.0.1:6380> keys java?
(empty list or set)
1.2 、dbsize
返回当前数据库中 key的数量
127.0.0.1:6380> help dbsize
DBSIZE -
summary: Return the number of keys in the selected database
since: 1.0.0
group: server
dbsize命令是直接获取Redis内置的键总数变量,所以dbsize命令的时间复杂度是O(1)。
而keys命令会遍历所有键,所以它的时间复杂度是O(n),当Redis保存了大量键时,线上环境禁止使用。
# 重命名KEYS命令为空,重启服务
echo "rename-command KEYS ''" >> /data/redis/standalone/6380/conf/redis6380
1.3、exists
判断key是否存在,ey存在则返回1,不存在则返回0
127.0.0.1:6380> help EXISTS
EXISTS key [key ...]
summary: Determine if a key exists
since: 1.0.0
group: generic
127.0.0.1:6380> EXISTS java
(integer) 1
127.0.0.1:6380> EXISTS Spring
(integer) 0
1.4、del key
删除key,返回值为被删除key的个数
127.0.0.1:6380> help DEL
DEL key [key ...]
summary: Delete a key
since: 1.0.0
group: generic
127.0.0.1:6380> keys *
1) "mylist"
2) "a"
3) "c"
4) "b"
5) "emps::2"
6) "python"
7) "hello"
127.0.0.1:6380> del a b c
(integer) 3
127.0.0.1:6380> del a
(integer) 0
1.5、expire key seconds
设置key的过期时间
127.0.0.1:6380> help EXPIRE
EXPIRE key seconds
summary: Set a key's time to live in seconds
since: 1.0.0
group: generic
1.6、ttl key
获取key的剩余过期时间(TTL - Time To Live)
在Redis 2.6和之前版本,如果key不存在或者已过期时返回-1
从Redis2.8开始,错误返回值的结果有如下改变:
- 如果key不存在或者已过期,返回 -2;
- 如果key存在并且没有设置过期时间(永久有效),返回 -1;
127.0.0.1:6380> help ttl
TTL key
summary: Get the time to live for a key
since: 1.0.0
group: generic
1.7、type key
返回的就是当前key对应的value的数据结构类型
127.0.0.1:6380> help type
TYPE key
summary: Determine the type stored at key
since: 1.0.0
group: generic
127.0.0.1:6380> set hello helloworld
OK
127.0.0.1:6380> rpush list1 a b c d e
(integer) 5
127.0.0.1:6380> type list1
list
127.0.0.1:6380> type hello
string