redis7.0编译安装 + bash安装脚本

以下操作在debian11下演示,ubuntu通用,其它系列linux自行安装依赖,其余一样
以7.0.12版本为例进行安装
最后的bash脚本是对前面的操作的封装,一键即配置完成,即可使用

一、编译安装

1.安装依赖

bash 复制代码
apt-get install build-essential autoconf automake

2.下载

官方下载页面

bash 复制代码
cd /usr/local

如果下载不下来,建议用迅雷这类工具下载后上传到服务器

bash 复制代码
wget https://github.com/redis/redis/archive/7.0.12.tar.gz

3.解压

bash 复制代码
tar -zxvf /usr/local/redis-7.0.12.tar.gz

4.编译安装

bash 复制代码
cd redis-7.0.12

安装到/usr/local/redis

bash 复制代码
make && make install

5.配置文件

配置文件在源码包里面

bash 复制代码
mkdir /etc/redis
bash 复制代码
cp /usr/local/redis-7.0.12/redis.conf /etc/redis/redis.conf

测试运行,ctrl+c取消

bash 复制代码
/usr/local/bin/redis-server /etc/redis/redis.conf

6.修改配置文件

bash 复制代码
vi /etc/redis/redis.conf

守护模式开启

找到daemonize no,修改为daemonize yes

修改安全设置

requirepass xxx:设定密码访问

bing 127.0.0.1:绑定的ip,在保护模式开启的情况下,只有绑定的ip才可以访问redis服务,建议注释掉,否则外部的设备无法访问。

protected-mode yes:保护模式,如果为yes,绑定的ip或输入密码才能访问

port:redis服务端口号,默认是6379,如果需要修改redis的访问端口,就通过修改该值实现

7.systemctl脚本管理

bash 复制代码
vi /usr/lib/systemd/system/redis.service

内容如下

复制代码
[Unit]
Description=redis
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存:wq

依次执行下面的命令

bash 复制代码
systemctl daemon-reload
systemctl start redis
systemctl enable redis
systemctl status redis

8.远程连接

使用redisinsight连接,不写账号,只写上面设定的密码即可

如果后期优化,可以设定账号密码,这里不再涉及

二、编译安装的一键脚本

1.创建脚本文件

bash 复制代码
vi /usr/local/redis7.sh

2.编辑

可能会卡在下载redis源码包上,可以注释掉wget这一行,然后上传其它地方下载的上传到/usr/local

bash 复制代码
#!/bin/bash

# 常量设置
redis_version="7.0.12" # redis版本
install_path="/usr/local" # 安装、操作目录

# 输入redis密码,如果为空则没有
read -p -"请输入一个redis密码:" redis_password

# 安装依赖
echo "......正在安装依赖......"
apt-get install -y build-essential autoconf automake
echo "......依赖安装完成......"

# 下载redis源码包
echo "......正在下载源码包......"
wget -P ${install_path} https://github.com/redis/redis/archive/7.0.12.tar.gz 
echo "......源码包下载完成......"

# 解压缩
echo "......正在解压缩源码包......"
cd ${install_path}
tar -zxf ${install_path}/redis-${redis_version}.tar.gz
echo "......源码包解压缩完成......"

# 编译安装
echo "......正在编译安装......"
cd ${install_path}/redis-${redis_version} && make && make install
echo "......编译安装完成......"

# 配置文件
echo "......正在修改配置文件......"
## 创建配置文件
mkdir /etc/redis
cp ${install_path}/redis-${redis_version}/redis.conf /etc/redis/redis.conf
## 修改配置文件(/etc/redis/redis.conf)
sed -i 's/daemonize no/daemonize yes/g' /etc/redis/redis.conf
## 修改默认密码,如果为空则开启所有ip都可以访问,如果不为空则使用密码访问
if [ ${#redis_password} -eq 0 ];
then
    echo "未输入密码,开启所有IP都能访问"
    sed -i 's/bind 127.0.0.1 -::1/# bind 127.0.0.1 -::1/g' /etc/redis/redis.conf
    sed -i 's/protected-mode yes/protected-mode no/g' /etc/redis/redis.conf
else
    echo "已设定密码"
    sed -i 's/bind 127.0.0.1 -::1/# bind 127.0.0.1 -::1/g' /etc/redis/redis.conf
    sed -i 's/# requirepass foobared/requirepass '"${redis_password}"'/g' /etc/redis/redis.conf
fi

## 持久化设定

echo "......修改配置文件完成......"

# 配置systemctl脚本
echo "......正在配置systemctl脚本......"

echo "......正在配置systemctl脚本......"
cat>/usr/lib/systemd/system/redis.service<<EOF
[Unit]
Description=redis
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start redis
systemctl enable redis
systemctl status redis

echo "......systemctl脚本配置完成......"
echo "......!!!脚本运行完成!!!......"

3.运行

bash 复制代码
sh /usr/local/redis7.sh
相关推荐
難釋懷1 分钟前
优惠卷秒杀集群环境下的并发问题
redis·缓存
青衫码上行1 小时前
NoSql数据库简介 + Redis概述
数据库·redis·nosql
可涵不会debug2 小时前
Redis魔法学院——第四课:哈希(Hash)深度解析:Field-Value 层级结构、原子性操作与内部编码优化
数据库·redis·算法·缓存·哈希算法
fengxin_rou2 小时前
【黑马点评实战篇|第一篇:基于Redis实现登录】
java·开发语言·数据库·redis·缓存
我待_JAVA_如初恋2 小时前
Redis常用的数据类型之String
数据库·redis·缓存
这儿有一堆花2 小时前
任何东西都可以转成 Base64!?
bash
wasp5203 小时前
Banana Slides 深度解析:AI Core 架构设计与 Prompt 工程实践
人工智能·prompt·bash
学到头秃的suhian4 小时前
Redis跳表
redis
jiunian_cn5 小时前
【Redis】zset数据类型相关指令
数据库·redis·缓存
jiunian_cn5 小时前
【Redis】set数据类型相关指令
数据库·redis·缓存