一、Nosql
1.1、概念
首先数据库分为两种,即关系型数据库和非关系型数据库(也就是Nosql)。
NoSQL数据库,即非关系型数据库,提供了一种不同于传统关系型数据库的数据存储与管理方式。其特点包括灵活的数据模型、高可扩展性、高性能以及适用于大数据处理等。这些特性使NoSQL数据库在现代应用程序中越来越受欢迎,特别是在需要处理大规模、半结构化或非结构化数据的场景中。
1.2、分类
主要分为临时性键 值存储(memcached、Redis)、永久性键值存储(ROMA、Redis)、面向文档的数据库 (MongoDB、CouchDB)、面向列的数据库(Cassandra、HBase)。
二、redis
2.1、概念
Redis(Remote Dictionary Server)是一个开源的内存数据库,使用ANSI C编写,遵守BSD协议。它主要用作键值存储系统、缓存、消息队列和会话存储等。
2.2、特性
- 速度快: 10W QPS,基于内存,C语言实现
- 单线程
- 持久化
- 支持多种数据结构
- 支持多种编程语言
- 功能丰富: 支持Lua脚本,发布订阅,事务,pipeline等功能
- 简单: 代码短小精悍(单机核心代码只有23000行左右),单线程开发容易,不依赖外部库,使用简单
- 主从复制
- 支持高可用和分布式
2.2.1、单线程优势
- 纯内存
- 非阻塞
- 避免线程切换和竞态消耗
2.3、搭建
官方下载地址:Index of /releases/
2.3.1、编译搭建
首先部署三台主机
在node1中解压并安装编译包

dnf install make gcc initscripts-10.11.6 1.el9.x86_64 -y
执行编译
接着make && make install
启动redis

然后会报错,接着前往 vim install_server.sh
对应位置注释掉

可以/system慢慢找,接着再次执行,一路enter
接着启动

2.3.2、配置redis
vim /etc/redis/6379.conf
防止找不到路径

/etc/init.d/redis_6379 restart 重启服务
接着端口查看

2.4、主从复制
2.4.1、环境
ip和nat网卡
在三台主机中vim /etc/redis/6379.conf

在node2,3中

2.4.2、测试
在主节点中node1中

在slave中node2,3

2.5、哨兵(redis高可用)
2.5.1、概念
哨兵通过监控主从节点的运行状态,并在主节点故障时自动进行故障转移,确保服务的连续性和稳定性的一种机制。这种机制是一种分布式系统,用于对主从结构中的每台服务器进行监控。当主节点出现故障时,哨兵会自动选择一个从节点提升为主节点,并通知其他从节点和客户端更新配置。在上述的主从复制的步骤上大大优化,不需要人工干预。
2.5.2、实验过程
2.5.2.1、master节点
首先在master节点也就是node1上

更改以下设置(相隔太远就不拍了)
pidfile /var/run/redis-sentinel.pid
sentinel monitor mymaster 172.25.254.10 6379 2
sentinel down-after-milliseconds mymaster 10000
将文件复制到另外两台主机上

redis-sentinel /etc/redis/sentinel.conf 启动

2.5.2.2、测试
再开一台master节点的主机


2.6、Redis cluster
2.6.1、概念
在哨兵sentinel机制中,可以解决redis高可用问题,即当master故障后可以自动将slave提升为master, 从而可以保证redis服务的正常使用,但是无法解决redis单机写入的瓶颈问题,即单机redis写入性能受 限于单机的内存大小、并发数量、网卡速率等因素。redis 3.0版本之后推出了无中心架构的redis cluster机制,在无中心的redis集群当中,其每个节点保存 当前节点数据和整个集群状态,每个节点都和其他所有节点连接。
2.6.2、实验过程
2.6.2.1、安装
为了方便就不源码编译了,直接yum
在原本三台主机的基础上再次部署三台主机(这里用的如果是mobashell可开启多执行模式,xshell还没搞清)
IP分别设置为172.25.254.110/120/130
为了配置统一,先将前三台node1,2,3的redis代码配置删掉

之后全部进行
Yum install redis -y
yum安装编译后修改配置文件
2.6.2.2、更改参数
在node1中
vim /etc/redis/redis.conf
检测发现6379已经打开了


将配置文件发往其他主机


成功后,在其他主机执行systemctl enable --now redis

2.6.3、创建redis cluster
接着在node1中创建集群

接着询问你是否进行配置选择yes

进行检测

这里提示5798超过了node1的范围,上图显示是0-5461

所以得去node2中写
Node1,2,3是一个哈希层,数据分别存储到它们三台master主机中,而slave的作用是master或者说哈希层出现问题后,才会把数据存储到slave节点中。
2.6.4、集群扩容
再开两台主机
IP分别设置为172.25.254.50/150
首先下载redis包
Yum install redis -y
同时再把前面的几台主机的配置文件复制过去

然后再在新建两台主机打开redis服务
Systemctl start redis
通过以下命令进行添加master
redis-cli -a 123456 --cluster add-node 172.25.254.50:6379 172.25.254.20:6379

这里可以看到添加成功

接着重新分享槽位

这里16384分享给四个槽位所以/4

因为50没有,所以是50的id,id在check检测那一步可以看,然后选择所有人给他分配
每人都匀一点给他

接着会问你是否分配,yes就可以了
redis-cli -a 123456 --cluster check 172.25.254.10:6379
这里检测可以看到已经有槽位了

接着添加slava150
redis-cli -a 123456 --cluster add-node 172.25.254.150:6379 172.25.254.20:6379 --cluster-slave --cluster-master-id ef434540246ffaef763e4275756c062b7a387993
接着检测redis-cli -a 123456 --cluster check 172.25.254.20:6379
可以看到已经添加进去了,同时150变成了50的slave

2.6.5、如何删除节点
2.6.5.1、首先删除slave
redis-cli -a 123456 --cluster del-node 172.25.254.150:6379 54a1810c38c8ddf0bf677d37b682ac645a9c922b
后面跟得是id

检测:可以看到已经没有150了

2.6.5.2、删除master
因为分配过槽位,有数据,不能直接删除,得先清除数据。
重新分配槽位
redis-cli -a 123456 --cluster reshard 172.25.254.20:6379
将50的分配给130

这里可以看到50上没有数据,所以可以进行删除

