系统环境
主机名称: debian04
系统信息: debian 11
Redis 版本:7.0.5
安装
懒人多合一安装命令:
sudo apt install lsb-release
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install redis -y
这是官方推荐的安装方式, 其它系统详见:
可执行命令
redis-server: redis 服务器redis-cli: redis 命令行客户端redis-benchmark: redis 性能测试工具redis-check-aof: aof 文件检查工具redis-check-dump: rdb 文件检查工具
启动与关闭
启动 redis (前台):
redis-server
启动 redis (后台):
redis-server &
使用配置文件启动 redis (后台):
redis-server /etc/redis/redis.conf
查看 redis 配置文件位置:
redis-cli -h debian04 info | grep config_file
关闭 redis:
redis-cli shutdown
基础信息
连接本地默认配置的 redis:
redis-cli
完整连接 redis 命令:
redis-cli -h debian04 -p 6379 -a password
查看信息:
redis-cli -h debian04 info
信息的内容很多, 但是有分块, 查看分块:
redis-cli -h debian04 info |grep "#"
可得如下分块信息
# Server
# Clients
# Memory
# Persistence
# Stats
# Replication
# CPU
# Modules
# Errorstats
# Cluster
# Keyspace
查看特定分块的信息, 以 Server 分块为例:
redis-cli -h debian04 info Server
其余分块类似, 不再赘述.
获取默认的 redis 目录:
redis-cli -h debian04 config get dir
和 rdb 文件名:
redis-cli -h debian04 config get dbfilename
如果出现如下错误:
(error) DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a an authentication password for the default user. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
那是因为 redis 7 的保护模式默认是打开的, 如果要关闭则按照提示里 4 种方法任意一种去做就可以了, 按需选择.
基本操作
以下操作均在 redis-cli 控制台内进行.
查看所有 key (生产环境高危命令):
keys *
删除所有库 (生产环境高危命令):
flushall
删除当前库(生产环境高危命令):
flushdb
以上三个命令仅在测试和学习的时候使用,
且最好在生产环境中的配置文件中直接屏蔽, 配置文件写法:
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command KEYS ""
存入键值对 k1 - v1:
set k1 v1
或者
set k1 "v1"
获取 k1 的值:
get k1
删除键值对 k1 - v1 (成功返回 1, 否则返回 0):
del k1
检查 k1 存在性:
exists k1
进阶操作
序列化 k1, 并返回被序列化的值:
dump k1
为 k1设置过期时间 10 秒
expire k1 10
为 k1设置秒级过期时间戳 2021-11-01 11:58:00:
expireat k1 1635739080
为 k1设置过期时间 10 毫秒
pexpire k1 10
为 k1设置毫秒级过期时间戳 2021-11-01 11:58:00:
pexpireat k1 1635739080000
设置 k1 永不过期:
persist k1
以秒为单位,返回给定 k1 的剩余生存时间(TTL, time to live):
ttl k1
以毫秒为单位返回 k1 的剩余的过期时间:
pttl k1
从当前数据库中随机返回一个 key:
randomkey
修改 k1 的名称为 k2:
rename k1 k2
仅当 k2 不存在时,将 k1 改名为 k2:
renamenx k1 k2
迭代数据库中的数据库键(生产环境常用):
SCAN cursor [MATCH pattern] [COUNT count]
- cursor - 游标
- pattern - 匹配的模式
- count - 指定从数据集里返回多少元素,默认值为 10
查看 k1 的值的数据类型:
type k1