当前位置: 首页 > news >正文

Redis数据库基础与持久化部署

Redis数据库

一、基础

1.yum 安装

[root@target ~]# yum install -y redis
[root@target ~]# systemctl start redis
#登录
[root@target ~]# redis-cli
#和mysql意义对大小写不敏感 支持补齐

2.Redis 命令工具

redis-server 等等
redis-server:     		 #服务器启动命令
#可以开启新线程
#备份一个新的配置文件
[root@target ~]# cp /etc/redis.conf /etc/redis_6380.conf
#修改日志文件名字 并把对应端口号修改
[root@target ~]# vim /etc/redis_6380.conf 
logfile /var/log/redis/redis_6380.log
#启动新线程 此时只能在前台运行
[root@target ~]# redis-server /etc/redis_6380.conf
#修改配置文件 让其在后台运行
[root@target ~]vim /etc/redis_6380.conf nf
daemonize yes
#重启检测
[root@target ~]# redis-server /etc/redis_6380.conf
[root@target ~]# ps aux | grep redis-server
redis     275736  0.0  1.5  76532 22388 ?        Ssl  09:06   0:02 /usr/bin/redis-server 127.0.0.1:6379
root      288551  0.0  1.1 152312 17396 ?        Ssl  09:49   0:00 redis-server 127.0.0.1:6380
root      292056  0.0  0.5  21988  7984 pts/0    S+   10:00   0:00 grep --color=auto redis-server
[root@target ~]# killall redis-server
[root@target ~]# ps aux | grep redis-server
root      292615  0.0  0.5  21988  8008 pts/0    S+   10:02   0:00 grep --color=auto redis-server
redis-benchmark
[root@target ~]# redis-benchmark 
====== PING_INLINE ======                                                    100000 requests completed in 1.41 seconds50 parallel clients3 bytes payloadkeep alive: 1host configuration "save": 3600 1 300 100 60 10000host configuration "appendonly": nomulti-thread: noLatency by percentile distribution:
0.000% <= 0.055 milliseconds (cumulative count 4)
50.000% <= 0.263 milliseconds (cumulative count 50857)
75.000% <= 0.463 milliseconds (cumulative count 75646)
.....
redis-cli 远程连接
[root@target ~]# systemctl stop redis
#修改IP地址
[root@target ~]# vim /etc/redis.conf 
#bind 127.0.0.1 -::1
bind 192.168.157.168
[root@target ~]# systemctl start redis
[root@target ~]# redis-cli 
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> 
#使用指定IP登录
[root@target ~]# redis-cli -h 192.168.157.168
192.168.157.168:6379> 
#需要关闭 保护模式
[root@target ~]# vim /etc/redis.conf 
protected-mode no
[root@target ~]# systemctl restart redis

3.Redis 数据库常用命令

[root@target ~]# redis-cli -h 192.168.157.168
#存放数据 
192.168.157.168:6379> set name zy
OK
#再次设置 覆盖原有值
192.168.157.168:6379> set name hzk
OK
192.168.157.168:6379> get name
"hzk"#获取数据
192.168.157.168:6379> get name
"zy"
#如果没定义 就显示nil 表示空
192.168.157.168:6379> get age
(nil)
#多数据库间切换
#select 序号
使用 redis-cli 连接 Redis 数据库后,默认使用的是序号为 0 的数据库。
192.168.157.168:6379> select 1
OK#move 移动
192.168.157.168:6379> keys *
1) "c"
2) "name"
192.168.157.168:6379> move c 1
(integer) 1
192.168.157.168:6379> get c
(nil)
192.168.157.168:6379> SELECT 1
OK
192.168.157.168:6379[1]> get c
"5"#ttl 命令
192.168.157.168:6379> ttl d
(integer) -1
#expire 
192.168.157.168:6379> EXPIRE d 10
(integer) 1
192.168.157.168:6379> ttl d
(integer) 6
192.168.157.168:6379> ttl d
(integer) 4
192.168.157.168:6379> ttl d
(integer) 1
192.168.157.168:6379> ttl d
(integer) -2
192.168.157.168:6379> get d
(nil)
key
#查看当前数据库中所有键
192.168.157.168:6379[1]> keys *
(empty array)
192.168.157.168:6379[1]> SELECT 0
OK
192.168.157.168:6379> keys *
1) "key:__rand_int__"
2) "name"#查看当前数据库中所有键
192.168.157.168:6379> KEYS *	
#查看当前数据库中以 v 开头的数据
192.168.157.168:6379> KEYS v*
#查看当前数据库中以 v 开头后面包含任意一位的数据
192.168.157.168:6379> KEYS v?				
#查看当前数据库中以 v 开头 v 开头后面包含任意两位的数据
192.168.157.168:6379> KEYS v??				
###
批量设置键和查看键
#批量设置键和查看键
192.168.157.168:6379> mset a 1 b 2
OK
192.168.157.168:6379> get a
"1"
192.168.157.168:6379> get b
"2"
192.168.157.168:6379> keys *
1) "c"
2) "name"
192.168.157.168:6379> mget c name
1) "5"
2) "hzk"
del 命令
192.168.157.168:6379> keys *
1) "a"
2) "b"
3) "key:__rand_int__"
4) "name"
192.168.157.168:6379> del "key:__rand_int__"
(integer) 1
192.168.157.168:6379> del a
(integer) 1
192.168.157.168:6379> get a
(nil)
rename与renamenx
#rename
#对已有 key 进行重命名。(覆盖)
192.168.157.168:6379> set c 22
OK
192.168.157.168:6379> RENAME c d
OK
192.168.157.168:6379> get c
(nil)
192.168.157.168:6379> get d
"22"
192.168.157.168:6379> set c 4
OK
192.168.157.168:6379> RENAME c d
OK
192.168.157.168:6379> get c
(nil)
192.168.157.168:6379> get d
"4"
#renamenx
#对已有 key 进行重命名,并检测新名是否存在,如果目标 key 存在则不进行重命名。(不覆盖)
192.168.157.168:6379> set c 5
OK
192.168.157.168:6379> RENAMENX c d
(integer) 0
192.168.157.168:6379> get c
"5"
192.168.157.168:6379> get d
"4"
append
#存在追加 没有赋值
192.168.157.168:6379> get name
"hzk"
192.168.157.168:6379> APPEND name bbbb
(integer) 7
192.168.157.168:6379> get name
"hzkbbbb"
192.168.157.168:6379> get p
(nil)
192.168.157.168:6379> APPEND p 1
(integer) 1
192.168.157.168:6379> get p
"1"

二、redis持久化

RDB

[root@target ~]# vim /etc/redis.conf
save 3600 1 300 100 60 10000
[root@target ~]# cd /var/lib/r
redis/     rpcbind/   rpm/       rpm-state/ rsyslog/   
[root@target ~]# cd /var/lib/redis/
#保存在这个目录下
[root@target redis]# ls
dump.rdb
[root@target redis]# systemctl restart redis

AOF


[root@nfs-server redis]# vim /etc/redis.conf 
appendonly yes
[root@nfs-server redis]# cd appendonlydir/
[root@nfs-server appendonlydir]# ls
appendonly.aof.1.base.rdb  appendonly.aof.1.incr.aof  appendonly.aof.manifest
[root@nfs-server appendonlydir]# vim appendonly.aof.1.incr.aof 
$1
0
*3
$3
set
$3
bbb
$3
222
*3
$3
set
....
#aof关闭之后 重新开启 数据会丢失 因为原文件会被覆盖

三、密码

#修改配置文件 设置密码
[root@nfs-server appendonlydir]# vim /etc/redis.conf requirepass 123.com
#重启
[root@nfs-server appendonlydir]# systemctl restart redis
#尝试登录[root@target redis]# redis-cli -h 192.168.157.168
192.168.157.168:6379> keys *
#被拒绝
(error) NOAUTH Authentication required.
#auth命令 进行密码认证
192.168.157.168:6379> auth 123.com
OK
192.168.157.168:6379> keys *
1) "bbb"
2) "v"
3) "a"
4) "m"
5) "z"
6) "kk"
#或者 -a指密码
[root@target redis]# redis-cli -h 192.168.157.168 -a 123.com
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.157.168:6379> keys *
1) "bbb"
2) "v"
3) "a"
4) "m"
5) "z"
http://www.lryc.cn/news/593452.html

相关文章:

  • 使用CCS6.2为C2000(DSP28335)生成.bin文件和.hex文件
  • 【LeetCode 热题 100】437. 路径总和 III——(解法一)递归递归!
  • CCF编程能力等级认证GESP—C++7级—20250628
  • STM32_Hal库学习ADC
  • IntelliJ IDEA中Mybatis的xml文件报错解决
  • SSM框架——注入类型
  • aws(学习笔记第四十九课) ECS集中练习(1)
  • Streamlit 官翻 5 - 部署、社区云 Deploy
  • Python绘制数据(三)
  • Matplotlib 30分钟精通
  • 人该怎样活着呢?55
  • Windows11下编译好的opencv4.8-mingw,可下载后直接用
  • Apache Kafka 学习笔记
  • 详细阐述 TCP、UDP、ICMPv4 和 ICMPv6 协议-以及防火墙端口原理优雅草卓伊凡
  • Python高级数据类型:字典(Dictionary)
  • Datawhale 7月学习
  • RK3568 Linux驱动学习——SDK安装编译
  • Oracle为什么需要临时表?——让数据处理更灵活
  • DAY 18 推断聚类后簇的类型
  • 【Project】kafka+flume+davinci广告点击实时分析系统
  • MySQL(145)如何升级MySQL版本?
  • 在服务器(ECS)部署 MySQL 操作流程
  • 基于单片机宠物喂食器/智能宠物窝/智能饲养
  • 手撕Spring底层系列之:注解驱动的魔力与实现内幕
  • Spring AI 1.0版本 + 千问大模型之 文本记忆对话
  • 基于单片机病床呼叫系统/床位呼叫系统
  • C#操作WPS表格
  • 大模型军备竞赛升级!Grok 4 携 “多智能体内生化” 破局,重构 AI 算力与 Agent 2.0 时代
  • 张 关于大语言模型(LLM)置信度研究的经典与前沿论文 :温度缩放;语义熵;自一致性;事实与反思;检索增强;黑盒引导;
  • [MySQL基础3] 数据控制语言DCL和MySQL中的常用函数