本文共 3573 字,大约阅读时间需要 11 分钟。
在开始安装 Redis 之前,需要确保目标服务器已具备必要的工具和环境。我们将依次检测并安装以下必需工具:curl、wget 和 tar。
# 检测并安装 Curlif ! command -v curl &> /dev/null; then echo "正在安装 Curl" if [ Ubuntu ]; then sudo apt-get -y install curl else sudo yum -y install curl fifi# 检测并安装 Wgetif ! command -v wget &> /dev/null; then echo "正在安装 Wget" if [ Ubuntu ]; then sudo apt-get -y install wget else sudo yum -y install wget fifi# 检测并安装 Tarif ! command -v tar &> /dev/null; then echo "正在安装 Tar" if [ Ubuntu ]; then sudo apt-get -y install tar else sudo yum -y install tar fifi
接下来,我们将按照以下步骤安装 Redis:
# 下载 Redis 最新版本wget http://download.redis.io/releases/redis-6.2.0.tar.gz
# 解压tar -xzf redis-6.2.0.tar.gz -C /usr/local/cd /usr/local/redis-6.2.0make && make install
# 创建必要的目录mkdir -p /etc/redis/{conf,log,pid,redis6981,redis6982,redis6983,sentinel26981,sentinel26982,sentinel26983}# 编写配置文件# 例如:/etc/redis/conf/redis6981.confecho "bind 0.0.0.0protected-mode noport 6981tcp-backlog 511unixsocket /tmp/redis6981.sockunixsocketperm 700timeout 0tcp-keepalive 300daemonize yessupervised nopidfile /etc/redis/pid/redis_6981.pidloglevel noticelogfile /etc/redis/log/redis6981.logdatabases 16always-show-logo yes# 持久化配置save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename "dump6981.rdb"dir /etc/redis/redis6981# 主从配置masterauth {}replica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-backlog-size 10mbrepl-backlog-ttl 3600# 安全认证requirepass {}rename-command CONFIG "b840fc02d524045429941cc43f59e41cb7be6c52"# LUA 脚本支持lua-time-limit 5000# 慢日志slowlog-log-slower-than 10000slowlog-max-len 128# 高级设置activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 1024mb 256mb 300client-output-buffer-limit pubsub 32mb 8mb 60client-query-buffer-limit 1gb" > /etc/redis/conf/redis6981.conf# 编写 Sentinel 配置文件echo "port 26981daemonize yespidfile /etc/redis/pid/redis-sentinel26981.pidlogfile /etc/redis/log/sentinel26981.logdir /etc/redis/sentinel26981sentinel deny-scripts-reconfig yessentinel monitor mymaster {} 6981 2sentinel auth-pass mymaster {}" > /etc/redis/conf/sentinel26981.conf # 创建服务文件mkdir -p /opt/scriptswith open('redis.py', 'w', encoding='utf-8') do: rs.write('''#!/usr/bin/env python3import sysimport osif sys.argv[1] == 'start': for start_id in [1, 2, 3]: os.system(f'redis-server /etc/redis/conf/redis698{start_id}.conf') print(f'启动 Redis {start_id}') os.system(f'redis-sentinel /etc/redis/conf/sentinel2698{start_id}.conf') print(f'启动 Sentinel {start_id}')elif sys.argv[1] == 'stop': passwd='%s' for stop_id in [1, 2, 3]: os.system(f'redis-cli -p 698{stop_id} -a {passwd} shutdown > /dev/null 2>&1 &') print(f'停止 Redis {stop_id}') os.system(f'redis-cli -p 2698{stop_id} shutdown > /dev/null 2>&1 &') print(f'停止 Sentinel {stop_id}')else: print('请进入 start 或 stop 命令')''' % (passwd))# 生成服务文件chmod u+x redis.py # 启动 Redis 集群systemctl enable redis{1,2,3}.servicesystemctl start redis{1,2,3}.servicesystemctl enable sentinel{1,2,3}.servicesystemctl start sentinel{1,2,3}.service 如果部署过程中遇到问题,可以尝试以下解决方案:
通过以上步骤,您可以成功部署一个 Redis 集群。从工具检测到源码安装,再到配置和服务管理,每一步都需要细致完成。请确保在实际操作中按照文档进行,避免因疏忽导致配置错误。
转载地址:http://ameh.baihongyu.com/