企业级NoSql数据库 --- Redis集群

2024-08-27 06:36

本文主要是介绍企业级NoSql数据库 --- Redis集群,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

什么是 redis ?

Redis是一个开源的、遵循BSD协议的、基于内存的而且目前比较流行的键值数据库(key-value database),是一个非关系型数据库,redis 提供将内存通过网络远程共享的一种服务,提供类似功能的还有memcached,但相比memcached,redis还提供了易扩展、高性能、具备数据持久性等功能。

Redis 在高并发、低延迟环境要求比较高的环境使用量非常广泛。

环境准备:

母盘:RHEL9 ---> 1G,1内核

克隆:redis-node1(172.25.254.10)、redis-node2(172.25.254.20)、redis-node3(172.25.254.30)

vmset.sh eth0 172.25.254.10 redis-node1.test.org

vmset.sh eth0 172.25.254.20 redis-node1.test.org

vmset.sh eth0 172.25.254.30 redis-node1.test.org

redis安装

[root@redis-node1 ~]# tar zxf redis-7.4.0.tar.gz[root@redis-node1 ~]# cd redis-7.4.0/[root@redis-node1 redis-7.4.0]# dnf install make -y
[root@redis-node1 redis-7.4.0]# dnf install initscripts -y[root@redis-node1 redis-7.4.0]# ll /etc/init.d/
[root@redis-node1 redis-7.4.0]# dnf install gcc -y
[root@redis-node1 redis-7.4.0]# make && make install[root@redis-node1 redis-7.4.0]# cd utils/
[root@redis-node1 utils]# vim install_server.sh 
[root@redis-node1 utils]# ./install_server.sh 
#一路回车
[root@redis-node1 utils]# vim /etc/redis/6379.conf
[root@redis-node1 utils]# /etc/init.d/redis_6379 restart

[root@redis-node1 utils]# cd
[root@redis-node1 ~]# scp -r redis-7.4.0 root@172.25.254.20:/root
[root@redis-node1 ~]# scp -r redis-7.4.0 root@172.25.254.30:/root[root@redis-node1 ~]# cd /usr/local/bin/#10~30:都需要安装rsync
[root@redis-node1 bin]# dnf install rsync -y[root@redis-node1 bin]# rsync -al * root@172.25.254.20:/usr/local/bin
[root@redis-node1 bin]# rsync -al * root@172.25.254.30:/usr/local/bin
[root@redis-node2 ~]# dnf install initscripts -y
[root@redis-node2 ~]# dnf install rsync -y[root@redis-node2 ~]# cd redis-7.4.0/
[root@redis-node2 redis-7.4.0]# cd utils/
[root@redis-node2 utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@redis-node2 utils]# 
[root@redis-node2 utils]# vim /etc/redis/6379.conf 
[root@redis-node2 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@redis-node3 ~]# dnf install initscripts -y
[root@redis-node3 ~]# dnf install rsync -y[root@redis-node3 ~]# cd redis-7.4.0/
[root@redis-node3 redis-7.4.0]# cd utils/
[root@redis-node3 utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@redis-node3 utils]# 
[root@redis-node3 utils]# vim /etc/redis/6379.conf
[root@redis-node3 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

redis的主从复制

[root@redis-node2 utils]# vim /etc/redis/6379.conf
[root@redis-node2 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@redis-node2 utils]# 
[root@redis-node2 utils]# redis-cli 
127.0.0.1:6379> get name
"lee"
[root@redis-node3 utils]# vim /etc/redis/6379.conf 
[root@redis-node3 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
[root@redis-node3 utils]# 
[root@redis-node3 utils]# redis-cli 
127.0.0.1:6379> get name
"lee"

redis哨兵

[root@redis-node1 ~]# ls
anaconda-ks.cfg  redis-7.4.0  redis-7.4.0.tar.gz
[root@redis-node1 ~]# cd redis-7.4.0/
[root@redis-node1 redis-7.4.0]# ls
00-RELEASENOTES     INSTALL      redis.conf              runtest-sentinel  TLS.md
BUGS                LICENSE.txt  REDISCONTRIBUTIONS.txt  SECURITY.md       utils
CODE_OF_CONDUCT.md  Makefile     runtest                 sentinel.conf
CONTRIBUTING.md     MANIFESTO    runtest-cluster         src
deps                README.md    runtest-moduleapi       tests
[root@redis-node1 redis-7.4.0]# cp sentinel.conf /etc/redis/
[root@redis-node1 redis-7.4.0]# vim /etc/redis/sentinel.conf
[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf root@172.25.254.20:/etc/redis/sentinel.conf
root@172.25.254.20's password: 
sentinel.conf                                    100%   14KB  10.6MB/s   00:00    
[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf root@172.25.254.30:/etc/redis/sentinel.conf
root@172.25.254.30's password: 
sentinel.conf                                    100%   14KB  10.5MB/s   00:00    
[root@redis-node1 redis-7.4.0]# 
[root@redis-node1 redis-7.4.0]# cd /etc/redis/
[root@redis-node1 redis]# ls
6379.conf  sentinel.conf
[root@redis-node1 redis]# cp sentinel.conf sentinel.conf.bak[root@redis-node1 redis]# redis-sentinel /etc/redis/sentinel.conf
[root@redis-node2 ~]# cd /etc/redis/
[root@redis-node2 redis]# ls
6379.conf  sentinel.conf
[root@redis-node2 redis]# cp sentinel.conf sentinel.conf.bak[root@redis-node2 redis]# redis-sentinel /etc/redis/sentinel.conf
[root@redis-node3 ~]# cd /etc/redis/
[root@redis-node3 redis]# ls
6379.conf  sentinel.conf
[root@redis-node3 redis]# cp sentinel.conf sentinel.conf.bak[root@redis-node3 redis]# redis-sentinel /etc/redis/sentinel.conf

测试:

redis cluster

克隆:redis-node110、redis-node120、redis-node130

vmset.sh eth0 172.25.254.110 redis-node110.test.org

vmset.sh eth0 172.25.254.120 redis-node120.test.org

vmset.sh eth0 172.25.254.130 redis-node130.test.org

[root@redis-node1 redis]# cd /root/redis-7.4.0/
[root@redis-node1 redis-7.4.0]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis-node1 redis-7.4.0]# ps aux | grep redis
root        7666  0.0  0.3   6408  2296 pts/1    S+   18:42   0:00 grep --color=auto redis
[root@redis-node1 redis-7.4.0]# make uninstall
cd src && make uninstall
make[1]: Entering directory '/root/redis-7.4.0/src'
rm -f /usr/local/bin/{redis-server,redis-benchmark,redis-cli,redis-check-rdb,redis-check-aof,redis-sentinel}
make[1]: Leaving directory '/root/redis-7.4.0/src'
[root@redis-node1 redis-7.4.0]# 
[root@redis-node1 redis-7.4.0]# cd
[root@redis-node1 ~]# dnf install redis -y[root@redis-node1 ~]# vim /etc/redis/redis.conf
[root@redis-node1 ~]# systemctl enable --now redis
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
[root@redis-node1 ~]# systemctl restart redis.service
[root@redis-node1 ~]# netstat -antlupe | grep redis
tcp        0      0 0.0.0.0:16379           0.0.0.0:*               LISTEN      991        40761      7919/redis-server * 
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      991        40759      7919/redis-server * 
tcp6       0      0 :::16379                :::*                    LISTEN      991        40762      7919/redis-server * 
tcp6       0      0 :::6379                 :::*                    LISTEN      991        40760      7919/redis-server * 
[root@redis-node1 ~]# 
[root@redis-node1 ~]# redis-cli 
-bash: /usr/local/bin/redis-cli: No such file or directory
[root@redis-node1 ~]# exit[root@redis-node1 ~]# redis-cli 
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> keys *
(empty array)[root@redis-node1 ~]# for i in 20 30 110 120 130
> do
> scp /etc/redis/redis.conf root@172.25.254.$i:/etc/redis/redis.conf
> done
[root@redis-node2 redis-7.4.0]# cd
[root@redis-node2 ~]# dnf install redis -y[root@redis-node2 ~]# vim /etc/hosts 
[root@redis-node2 ~]# systemctl enable --now redis[root@redis-node2 ~]# exit
[root@redis-node2 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set name lee
OK
[root@redis-node3 redis-7.4.0]# cd
[root@redis-node3 ~]# dnf install redis -y[root@redis-node3 ~]# vim /etc/hosts 
[root@redis-node3 ~]# systemctl enable --now redis[root@redis-node3 ~]# exit
[root@redis-node3 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> get name
(error) MOVED 5798 172.25.254.20:6379
[root@redis-node110 ~]# dnf install redis -y
[root@redis-node110 ~]# vim /etc/hosts 
[root@redis-node110 ~]# systemctl enable --now redis
[root@redis-node120 ~]# dnf install redis -y
[root@redis-node120 ~]# vim /etc/hosts 
[root@redis-node120 ~]# systemctl enable --now redis
[root@redis-node130 ~]# dnf install redis -y
[root@redis-node130 ~]# vim /etc/hosts 
[root@redis-node130 ~]# systemctl enable --now redis
[root@redis-node1 ~]# redis-cli --cluster create -a 123456 \
> 172.25.254.10:6379 172.25.254.20:6379 172.25.254.30:6379 \
> 172.25.254.110:6379 172.25.254.120:6379 172.25.254.130:6379 \
> --cluster-replicas 1
...
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 51942ff68767525da5b43516d5a1ac6d3a4236c0 172.25.254.10:6379slots:[0-5460] (5461 slots) master1 additional replica(s)
S: f775d5a125e9056a699f6ef324566e3024c8eba4 172.25.254.110:6379slots: (0 slots) slavereplicates 53eaf5b37b8961c67beed67abe67a4fdb568ba06
S: 2a8f1dfd906a1638fdcfc771dc24f00a21b5e8bf 172.25.254.120:6379slots: (0 slots) slavereplicates 51942ff68767525da5b43516d5a1ac6d3a4236c0
M: bd12ca08ea95bdc5abb1f52e35d6bb825611ba72 172.25.254.20:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)
M: 53eaf5b37b8961c67beed67abe67a4fdb568ba06 172.25.254.30:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)
S: aafeaea3cf23aa110960219c8b000946d94dd8a8 172.25.254.130:6379slots: (0 slots) slavereplicates bd12ca08ea95bdc5abb1f52e35d6bb825611ba72
...#20被推选,所以10不能创建
[root@redis-node1 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set name lee
(error) MOVED 5798 172.25.254.20:6379

集群扩容

克隆:redis50、redis150 #都是1G 1内核

vmset.sh eth0 172.25.254.50 redis50.silingchuan.org

vmset.sh eth0 172.25.254.150 redis150.silingchuan.org

50 150:

dnf install redis -y

[root@redis-node2 ~]# scp /etc/redis/redis.conf root@172.25.254.50:/etc/redis/redis.conf[root@redis-node2 ~]# scp /etc/redis/redis.conf root@172.25.254.150:/etc/redis/redis.conf
[root@redis-node50 ~]# dnf install redis -y
[root@redis-node50 ~]# systemctl start redis
[root@redis-node150 ~]# dnf install redis -y
[root@redis-node150 ~]# systemctl start redis

新增50master

[root@redis-node2 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.50:6379 172.25.254.20:6379[root@redis-node2 ~]# redis-cli -a 123456 --cluster check 172.25.254.10:6379

共享槽位

[root@redis-node2 ~]# redis-cli -a 123456 --cluster reshard 172.25.254.20:6379

(4096 50-ID all yes)

新增150slave

[root@redis-node2 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.150:6379 172.25.254.20:6379 --cluster-slave --cluster-master-id 2ce4bedb48252ca1c7030586d45d1b271490176a

移除150:

[root@redis-node2 ~]# redis-cli -a 123456 --cluster del-node  172.25.254.150:6379 6ab4d6ec8aab005c3934f6bfa272a6be77545cbb

清空槽位:

[root@redis-node2 ~]# redis-cli -a 123456 --cluster  reshard 172.25.254.20:6379

(4096 20-ID 50-ID done yes)

移除50:

[root@redis-node2 ~]# redis-cli -a 123456 --cluster del-node  172.25.254.50:6379 2ce4bedb48252ca1c7030586d45d1b271490176a

这篇关于企业级NoSql数据库 --- Redis集群的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110904

相关文章

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

postgresql数据库基本操作及命令详解

《postgresql数据库基本操作及命令详解》本文介绍了PostgreSQL数据库的基础操作,包括连接、创建、查看数据库,表的增删改查、索引管理、备份恢复及退出命令,适用于数据库管理和开发实践,感兴... 目录1. 连接 PostgreSQL 数据库2. 创建数据库3. 查看当前数据库4. 查看所有数据库

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

Redis的持久化之RDB和AOF机制详解

《Redis的持久化之RDB和AOF机制详解》:本文主要介绍Redis的持久化之RDB和AOF机制,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述RDB(Redis Database)核心原理触发方式手动触发自动触发AOF(Append-Only File)核