Redis安装与常用命令

Redis安装与常用命令

Redis安装

客户端连接Redis

复制代码
# 语法
# redis-cli -h IP/HOSTNAME -p PORT -a PASSWORD

[root@localhost ~]# redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> info
# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:9529b692c0384fb7
redis_mode:standalone
os:Linux 4.18.0-553.6.1.el8.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:8.4.1
process_id:10350
run_id:fd68b669756ef777ee5d2d4f2a74e851cc243d6a
tcp_port:6379
uptime_in_seconds:676
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:9272993
executable:/usr/bin/redis-server
config_file:/etc/redis.conf
# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
# Memory
used_memory:853896
used_memory_human:833.88K
used_memory_rss:13340672
used_memory_rss_human:12.72M
used_memory_peak:853896
used_memory_peak_human:833.88K
used_memory_peak_perc:100.00%
used_memory_overhead:840694
used_memory_startup:791000
.......

安装的相关程序介绍2

复制代码
[root@localhost ~]# ll /usr/bin/redis-*
-rwxr-xr-x 1 root root  656280 Oct 20  2021 redis-benchmark            #性能测试程
序
lrwxrwxrwx 1 root root      12 Oct 20  2021 redis-check-aof -> redis-server       
#AOF文件检查程序
lrwxrwxrwx 1 root root      12 Oct 20  2021 redis-check-rdb -> redis-server       
#RDB文件检查程序
-rwxr-xr-x 1 root root  827608 Oct 20  2021 redis-cli                             
#客户端程序
lrwxrwxrwx 1 root root      12 Oct 20  2021 redis-sentinel -> redis-server       
 #哨兵程序,软连接到服务器端主程序
-rwxr-xr-x 1 root root 1800864 Oct 20  2021 redis-server                         
 #服务端主程序   

客户端程序redis-cli

复制代码
# 默认本机无密码连接
redis-cli
# 远程客户端连接,注意:Redis没有用户的概念
redis-cli -h <Redis服务器IP> -p <PORT> -a <PASSWORD> --no-auth-warning

Redis常用命令

info

显示当前节点redis运行状态信息

复制代码
127.0.0.1:6379> INFO
# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:9529b692c0384fb7
redis_mode:standalone
os:Linux 4.18.0-553.6.1.el8.x86_64 x86_64
arch_bits:64
......
# 只显示指定部分内容,INFO命是分段的
# 只显示INFO内容# Server这一段
127.0.0.1:6379> INFO Server                  
# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:9529b692c0384fb7
redis_mode:standalone
os:Linux 4.18.0-553.6.1.el8.x86_64 x86_64
......
[root@localhost ~]# redis-cli info Cluster
# Cluster
select

切换数据库,相当于MySQL的USE DBNAME指令

复制代码
[root@localhost ~]# redis-cli 
127.0.0.1:6379> info cluster
# Cluster
cluster_enabled:0
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> select 15
OK
127.0.0.1:6379[15]> select 16
(error) ERR DB index is out of range
127.0.0.1:6379[15]> 
~KEYS

查看当前库下所有key,此命令慎用

复制代码
[root@localhost ~]# redis-cli bgsave 

DBSIZE 
返回当前库下所有key数量
127.0.0.1:6379> SELECT 0
OK
127.0.0.1:6379> KEYS *
  1) "key:91"
  2) "key:87"
......
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]> MSET one 1 two 2 three 3 four 4      # 一次设置4个key
OK
127.0.0.1:6379[1]> KEYS *o*
1) "four"
2) "two"
3) "one"
127.0.0.1:6379[1]> KEYS t??
1) "two"
127.0.0.1:6379[1]> KEYS t[w]*
1) "two"
127.0.0.1:6379[1]> KEYS *
1) "three"
2) "four"
3) "two"
4) "one"
bgsave

手动在后台执行RDB持久化操作

复制代码
# 交互式执行
127.0.0.1:6379> BGSAVE
Background saving started
# 非交互式执行
[root@localhost ~]# redis-cli BGSAVE
Background saving started
DBSIZE

返回当前库下所有key数量

复制代码
127.0.0.1:6379> DBSIZE
(integer) 5000000
127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]> DBSIZE
(integer) 0
FLUSHDB

强制清空当前库中所有key,此命令慎用

复制代码
127.0.0.1:6379> DBSIZE
(integer) 5000000
127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]> DBSIZE
(integer) 0
FLUSHALL

强制清空当前Redis服务器所有数据库中的所有key,即删除所有数据,此命令慎用

复制代码
127.0.0.1:6379> FLUSHALL
OK
# 生产建议修改配置使用rename-command禁用此命令
[root@localhost ~]# vim /etc/redis.conf 
 523 rename-command FLUSHALL ""            #flushdb和flushall 配置和AOF功能冲突,需要
设置appendonly no,不区分命令大小写
 699 appendonly no
# 测试
[root@localhost ~]# systemctl restart redis
[root@localhost ~]# redis-cli 
127.0.0.1:6379> FLUSHALL
(error) ERR unknown command `FLUSHALL`, with args beginning with: 
127.0.0.1:6379> FLUSHDB
OK
SHUTDOWN
复制代码
可用版本:>=1.0.0
时间复杂度:O(N),其中 N 为关机时需要保存的数据库键数量。
SHUTDOWN命令执行以下操作:
关闭Redis服务,停止所有客户端连接
如果有至少一个保存点在等待,执行SAVE命令
如果AOF选项被打开,更新AOF文件
关闭redis服务器(server)
如果持久化被打开的话,SHUTDOWN命令会保证服务器正常关闭而不丢失任何数据。
另一方面,假如只是单纯地执行SAVE命令,然后再执行QUIT命令,则没有这一保证一一因为在执行 SAVE之
后、执行QUIT之前的这段时间中间,其他客户端可能正在和服务器进行通讯,这时如果执行QUIT就会造成数据
丢失

#建议禁用此指令
[root@localhost ~]# vim /etc/redis.conf
rename-command shutdown ""
相关推荐
码农阿豪2 小时前
时序数据库选型指南:我们是怎么评估和选型的
数据库·时序数据库
2301_796588502 小时前
Go语言如何压缩文件_Go语言gzip压缩教程【基础】
jvm·数据库·python
m0_617881422 小时前
c++如何通过重定向rdbuf来捕获第三方库的日志输出到文件【详解】
jvm·数据库·python
IntMainJhy2 小时前
【flutter for open harmony】第三方库Flutter 国际化多语言的鸿蒙化适配与实战指南
数据库·flutter·华为·sqlite·harmonyos
Greyson12 小时前
mysql查询执行过程中如何追踪耗时_使用PROFILE分析指令周期
jvm·数据库·python
解救女汉子2 小时前
CSS如何实现水平垂直居中效果_利用flex布局的justify-content与align-items
jvm·数据库·python
2301_773553622 小时前
CSS如何解决栅格重叠问题_使用Grid-area明确划分元素占位
jvm·数据库·python
看海的四叔2 小时前
【SQL】SQL的日期与时间函数
数据库·hive·sql·数据分析·时间函数·日期函数
步辞2 小时前
JavaScript中Symbol-keyFor检索全局符号键名逻辑
jvm·数据库·python