企业级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数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

Redis中Hash从使用过程到原理说明

《Redis中Hash从使用过程到原理说明》RedisHash结构用于存储字段-值对,适合对象数据,支持HSET、HGET等命令,采用ziplist或hashtable编码,通过渐进式rehash优化... 目录一、开篇:Hash就像超市的货架二、Hash的基本使用1. 常用命令示例2. Java操作示例三

Redis中Set结构使用过程与原理说明

《Redis中Set结构使用过程与原理说明》本文解析了RedisSet数据结构,涵盖其基本操作(如添加、查找)、集合运算(交并差)、底层实现(intset与hashtable自动切换机制)、典型应用场... 目录开篇:从购物车到Redis Set一、Redis Set的基本操作1.1 编程常用命令1.2 集

Redis中的有序集合zset从使用到原理分析

《Redis中的有序集合zset从使用到原理分析》Redis有序集合(zset)是字符串与分值的有序映射,通过跳跃表和哈希表结合实现高效有序性管理,适用于排行榜、延迟队列等场景,其时间复杂度低,内存占... 目录开篇:排行榜背后的秘密一、zset的基本使用1.1 常用命令1.2 Java客户端示例二、zse

Redis中的AOF原理及分析

《Redis中的AOF原理及分析》Redis的AOF通过记录所有写操作命令实现持久化,支持always/everysec/no三种同步策略,重写机制优化文件体积,与RDB结合可平衡数据安全与恢复效率... 目录开篇:从日记本到AOF一、AOF的基本执行流程1. 命令执行与记录2. AOF重写机制二、AOF的

使用Node.js和PostgreSQL构建数据库应用

《使用Node.js和PostgreSQL构建数据库应用》PostgreSQL是一个功能强大的开源关系型数据库,而Node.js是构建高效网络应用的理想平台,结合这两个技术,我们可以创建出色的数据驱动... 目录初始化项目与安装依赖建立数据库连接执行CRUD操作查询数据插入数据更新数据删除数据完整示例与最佳

Oracle数据库在windows系统上重启步骤

《Oracle数据库在windows系统上重启步骤》有时候在服务中重启了oracle之后,数据库并不能正常访问,下面:本文主要介绍Oracle数据库在windows系统上重启的相关资料,文中通过代... oracle数据库在Windows上重启的方法我这里是使用oracle自带的sqlplus工具实现的方

MySQL批量替换数据库字符集的实用方法(附详细代码)

《MySQL批量替换数据库字符集的实用方法(附详细代码)》当需要修改数据库编码和字符集时,通常需要对其下属的所有表及表中所有字段进行修改,下面:本文主要介绍MySQL批量替换数据库字符集的实用方法... 目录前言为什么要批量修改字符集?整体脚本脚本逻辑解析1. 设置目标参数2. 生成修改表默认字符集的语句3