CentOS Stream 9 Redis 7.2.7 源码编译一键安装脚本
- 自动编译、自动配置、自动 systemd 托管
- 开启 AOF 持久化 + 密码 + 远程访问
- 安装完直接可用
#!/bin/bashset-euopipefail# 版本与路径REDIS_VERSION="7.2.7"INSTALL_DIR="/usr/local/redis"DATA_DIR="/data/redis"LOG_DIR="/var/log/redis"CONF_DIR="${INSTALL_DIR}/conf"REDIS_PASS="123456"echo-e"\033[32m===== 安装依赖 =====\033[0m"dnfinstall-ygcc gcc-c++makeopenssl-develecho-e"\033[32m===== 下载 Redis${REDIS_VERSION}=====\033[0m"cd/usr/local/srcwget-Nhttps://download.redis.io/releases/redis-${REDIS_VERSION}.tar.gzecho-e"\033[32m===== 解压 & 编译 =====\033[0m"tarzxf redis-${REDIS_VERSION}.tar.gzcdredis-${REDIS_VERSION}make-j$(nproc)echo-e"\033[32m===== 安装 =====\033[0m"makeinstallPREFIX=${INSTALL_DIR}echo-e"\033[32m===== 创建目录 =====\033[0m"mkdir-p${CONF_DIR}${DATA_DIR}${LOG_DIR}echo-e"\033[32m===== 复制配置文件 =====\033[0m"cpredis.conf${CONF_DIR}/redis.confecho-e"\033[32m===== 自动配置 redis.conf =====\033[0m"sed-i"s|^bind .*|bind 0.0.0.0|"${CONF_DIR}/redis.confsed-i"s|^protected-mode .*|protected-mode no|"${CONF_DIR}/redis.confsed-i"s|^port .*|port 6379|"${CONF_DIR}/redis.confsed-i"s|^daemonize .*|daemonize no|"${CONF_DIR}/redis.confsed-i"s|^logfile .*|logfile${LOG_DIR}/redis.log|"${CONF_DIR}/redis.confsed-i"s|^dir .*|dir${DATA_DIR}|"${CONF_DIR}/redis.confsed-i"s|^# requirepass .*|requirepass${REDIS_PASS}|"${CONF_DIR}/redis.confsed-i"s|^appendonly .*|appendonly yes|"${CONF_DIR}/redis.confecho-e"\033[32m===== 创建 systemd 服务 =====\033[0m"cat>/usr/lib/systemd/system/redis.service<<EOF [Unit] Description=Redis Server After=network.target [Service] Type=simple ExecStart=${INSTALL_DIR}/bin/redis-server${CONF_DIR}/redis.conf ExecReload=/bin/kill -USR2 \$MAINPIDRestart=on-failure LimitNOFILE=65535 [Install] WantedBy=multi-user.target EOFecho-e"\033[32m===== 启动并开机自启 =====\033[0m"systemctl daemon-reload systemctlenable--nowredissleep2echo-e"\033[32m===== 加入全局命令 =====\033[0m"echo"export PATH=\$PATH:${INSTALL_DIR}/bin">/etc/profile.d/redis.shsource/etc/profile.d/redis.shecho-e"\033[32m===== 安装完成!=====\033[0m"redis-server--versionredis-cli--versionechoecho"安装目录:${INSTALL_DIR}"echo"配置文件:${CONF_DIR}/redis.conf"echo"数据目录:${DATA_DIR}"echo"密码:${REDIS_PASS}"echo"测试:redis-cli -a${REDIS_PASS}"使用方法
# 1. 创建脚本viminstall_redis727.sh# 2. 粘贴上面全部内容,保存# 3. 加执行权限chmod+x install_redis727.sh# 4. 运行./install_redis727.sh安装后信息
- 端口:6379
- 密码:123456
- 开启 AOF 持久化
- 允许远程访问
- 开机自启
- 命令全局可用
测试
redis-cli-a123456127.0.0.1:6379>settestok127.0.0.1:6379>gettest