0

0

Redis自动化安装以及集群实现

蓮花仙者

蓮花仙者

发布时间:2025-07-09 11:52:24

|

765人浏览过

|

来源于php中文网

原创

redis实例安装与自动化集群实现

安装说明:自动解压安装包,并在指定路径编译安装。将配置文件模板复制到Redis实例的目录下,并根据端口号修改配置文件。

所需文件:配置文件、当前shell脚本、安装包。

参数说明:

  • 参数1(basedir):Redis安装包路径
  • 参数2(installdir):安装实例路径
  • 参数3(installfilename):安装包名称
  • 参数4(port):安装实例的端口号
#!/bin/bash
set -e
if [ $# -lt 4 ]; then
  echo "$(basename $0): Missing script argument"
  echo "$(basename $0) [installfilename] [port]"
  exit 9
fi
PortInUse=`netstat -anp | awk '{print $4}' | grep $4 | wc -l`
if [ $PortInUse -gt 0 ]; then
  echo "ERROR" $4 "Port is used by another process!"
  exit 9
fi
basedir=$1
installdir=$2
installfilename=$3
port=$4
cd $basedir
tar -zxvf $installfilename.tar.gz &>/dev/null 2>&1 &
cd $installfilename
mkdir -p $installdir
make PREFIX=$installdir install
sleep 1
scp $basedir/redis.conf $installdir
sed -i "s/instance_port/$port/g" $installdir/redis.conf
sleep 1
cd $installdir
./bin/redis-server redis.conf &>/dev/null 2>&1 &

配置文件模板:

<code>################################## INCLUDES ####################################
include /path/to/local.conf
# include /path/to/other.conf
<p>################################## MODULES ######################################
loadmodule /path/to/my_module.so</p><h1>loadmodule /path/to/other_module.so</h1><p>################################## NETWORK #####################################
bind 127.0.0.1 & your ip
port instance_port
tcp-backlog 511
timeout 0
tcp-keepalive 300</p><p>################################# GENERAL #####################################
daemonize yes
supervised no
pidfile ./redis_instance_port.pid
loglevel notice
logfile ./redis_log.log
databases 16
always-show-logo yes</p><p>################################ SNAPSHOTTING ################################
save 900 1
save 300 10
save 60 10000</p><p>stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./</p><p>################################# REPLICATION ##################################
masterauth <master-password>
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100</p><p>################################## SECURITY ###################################
requirepass your_password</p><p>################################### CLIENTS #####################################
maxclients 10000</p><p>############################## MEMORY MANAGEMENT #################################
maxmemory <bytes></p><h1>maxmemory-policy noeviction</h1><h1>maxmemory-samples 5</h1><h1>replica-ignore-maxmemory yes</h1><p>############################# LAZY FREEING ####################################
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no</p><p>############################## APPEND ONLY MODE ###############################
appendonly no</p><p>appendfilename "appendonly.aof"</p><h1>appendfsync always</h1><p>appendfsync everysec</p><h1>appendfsync no</h1><p>no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes</p><p>################################ LUA SCRIPTING ###############################
lua-time-limit 5000</p><p>################################ REDIS CLUSTER ###############################
cluster-enabled yes</p><h1>cluster-replica-validity-factor 10</h1><h1>cluster-require-full-coverage yes</h1><h1>cluster-replica-no-failover no</h1><p>########################## CLUSTER DOCKER/NAT support ########################</p><p>################################## SLOW LOG ###################################
slowlog-log-slower-than 10000
slowlog-max-len 128</p><p>################################ LATENCY MONITOR ##############################
latency-monitor-threshold 0</p><p>############################# EVENT NOTIFICATION ##############################
notify-keyspace-events ""</p><p>############################### ADVANCED CONFIG ###############################
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60</p><h1>client-query-buffer-limit 1gb</h1><h1>proto-max-bulk-len 512mb</h1><p>hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes</p><p>########################### ACTIVE DEFRAGMENTATION #######################</p><h1>Enabled active defragmentation</h1><h1>activedefrag yes</h1><h1>Minimum amount of fragmentation waste to start active defrag</h1><h1>active-defrag-ignore-bytes 100mb</h1><h1>Minimum percentage of fragmentation to start active defrag</h1><h1>active-defrag-threshold-lower 10</h1><h1>Maximum percentage of fragmentation at which we use maximum effort</h1><h1>active-defrag-threshold-upper 100</h1><h1>Minimal effort for defrag in CPU percentage</h1><h1>active-defrag-cycle-min 5</h1><h1>Maximal effort for defrag in CPU percentage</h1><h1>active-defrag-cycle-max 75</h1><h1>Maximum number of set/hash/zset/list fields that will be processed from</h1><h1>the main dictionary scan</h1><h1>active-defrag-max-scan-fields 1000</code>

安装示例:

AI小聚
AI小聚

一站式多功能AIGC创作平台,支持AI绘画、AI视频、AI聊天、AI音乐

下载
sh redis_install.sh /usr/local/redis/ /usr/local/redis5/redis9008/ redis-5.0.4 9008

Redis自动化安装以及集群实现

Redis实例的目录结构:

Redis自动化安装以及集群实现

基于Python的Redis自动化集群实现:

基于Python的自动化集群实现,初始化节点为node_1~node_6,节点实例需要为集群模式,三主三从,自动化集群,分配slots,加入从节点,约3秒钟完成。

import redis</h1><h1>master</h1><p>node_1 = {'host': '127.0.0.1', 'port': 9001, 'password': '<strong><em>'}
node_2 = {'host': '127.0.0.1', 'port': 9002, 'password': '</em></strong>'}
node_3 = {'host': '127.0.0.1', 'port': 9003, 'password': '***'}</p><h1>slave</h1><p>node_4 = {'host': '127.0.0.1', 'port': 9004, 'password': '<strong><em>'}
node_5 = {'host': '127.0.0.1', 'port': 9005, 'password': '</em></strong>'}
node_6 = {'host': '127.0.0.1', 'port': 9006, 'password': '***'}</p><p>redis_conn_1 = redis.StrictRedis(host=node_1["host"], port=node_1["port"], password=node_1["password"])
redis_conn_2 = redis.StrictRedis(host=node_2["host"], port=node_2["port"], password=node_2["password"])
redis_conn_3 = redis.StrictRedis(host=node_3["host"], port=node_3["port"], password=node_3["password"])</p><h1>cluster meet</h1><p>redis_conn_1.execute_command("cluster meet {0} {1}".format(node_2["host"],node_2["port"]))
redis_conn_1.execute_command("cluster meet {0} {1}".format(node_3["host"],node_3["port"]))
print('#################flush slots #################')
redis_conn_1.execute_command('cluster flushslots')
redis_conn_2.execute_command('cluster flushslots')
redis_conn_3.execute_command('cluster flushslots')
print('#################add slots#################')
for i in range(0,16383+1):
if i < 5461:
redis_conn_1.execute_command('cluster addslots {0}'.format(i))
elif i < 10922:
redis_conn_2.execute_command('cluster addslots {0}'.format(i))
else:
redis_conn_3.execute_command('cluster addslots {0}'.format(i))</p><h1>slave cluster meet</h1><p>redis_conn_1.execute_command("cluster meet {0} {1}".format(node_4["host"],node_4["port"]))
redis_conn_2.execute_command("cluster meet {0} {1}".format(node_5["host"],node_5["port"]))
redis_conn_3.execute_command("cluster meet {0} {1}".format(node_6["host"],node_6["port"]))</p><h1>cluster nodes</h1><p>print(str(redis_conn_1.execute_command('cluster nodes'), encoding = "utf-8"))

示例:

Redis自动化安装以及集群实现

这样一个Redis的集群,从实例的安装到集群的安装,如果环境依赖没有问题的话,基本上在一分钟内可以完成整个搭建过程。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
常用的数据库软件
常用的数据库软件

常用的数据库软件有MySQL、Oracle、SQL Server、PostgreSQL、MongoDB、Redis、Cassandra、Hadoop、Spark和Amazon DynamoDB。更多关于数据库软件的内容详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1006

2023.11.02

内存数据库有哪些
内存数据库有哪些

内存数据库有Redis、Memcached、Apache Ignite、VoltDB、TimesTen、H2 Database、Aerospike、Oracle TimesTen In-Memory Database、SAP HANA和ache Cassandra。更多关于内存数据库相关问题,详情请看本专题下面的文章。php中文网欢迎大家前来学习。

671

2023.11.14

mongodb和redis哪个读取速度快
mongodb和redis哪个读取速度快

redis 的读取速度比 mongodb 更快。原因包括:1. redis 使用简单的键值存储,而 mongodb 存储 json 格式的数据,需要解析和反序列化。2. redis 使用哈希表快速查找数据,而 mongodb 使用 b-tree 索引。因此,redis 在需要高性能读取操作的应用程序中是一个更好的选择。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

501

2024.04.02

redis怎么做缓存服务器
redis怎么做缓存服务器

redis 作为缓存服务器的答案:redis 是一款开源、高性能、分布式的键值存储,可作为缓存服务器使用。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

413

2024.04.07

redis怎么解决数据一致性
redis怎么解决数据一致性

redis 提供了两种一致性模型,以维护副本数据一致性:强一致性 (sync) 确保写操作仅在复制到所有从节点后才完成;最终一致性 (async) 则在主节点上写操作后认为已完成,牺牲一致性换取性能。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

408

2024.04.07

mysql和redis怎么保证双写一致性
mysql和redis怎么保证双写一致性

确保 mysql 和 redis 双写一致性的技术包括:1、事务性更新:同时更新 mysql 和 redis,保证一致性;2、主从复制:mysql 主服务器更改同步到 redis 从服务器;3、基于事件的更新:mysql 记录更改并发送到 redis等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

484

2024.04.07

redis缓存一般存些什么数据
redis缓存一般存些什么数据

redis缓存中存储的数据类型包括:字符串、哈希、列表、集合、有序集合、位图、地理空间数据和hyperloglog。这些数据类型适用于存储各种数据,从简单信息到复杂对象和地理位置。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

425

2024.04.07

redis的8种数据类型有哪些
redis的8种数据类型有哪些

redis 提供 8 种数据类型:字符串(文本、数字、二进制)、哈希(键值对)、列表(有序集合)、集合(无序唯一元素)、有序集合(按分数排序)、地理空间(地理位置)、hyperloglog(估计大数据基数)和位图(位序列存储)。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

450

2024.04.07

C# ASP.NET Core微服务架构与API网关实践
C# ASP.NET Core微服务架构与API网关实践

本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。

76

2026.03.11

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 22.5万人学习

Django 教程
Django 教程

共28课时 | 5万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号