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
相关推荐
float_六七4 小时前
Redis:极速缓存与数据结构存储揭秘
数据结构·redis·缓存
blammmp4 小时前
Redis : set集合
数据库·redis·缓存
昂子的博客5 小时前
Springboot仿抖音app开发之消息业务模块后端复盘及相关业务知识总结
java·spring boot·redis·后端·mysql·mongodb·spring
冷崖6 小时前
Redis事务与驱动的学习(一)
数据库·redis·学习
你好龙卷风!!!10 小时前
mac redis以守护进程重新启动
redis
Eric.Lee202113 小时前
ubuntu 系统 多条命令通过 bash 脚本执行
linux·ubuntu·bash
bingbingyihao14 小时前
服务自动添加实例工具
linux·运维·bash
等风来不如迎风去14 小时前
【python】bash: !‘: event not found
chrome·python·bash
天然首长1 天前
Redis相关
redis
小葛呀1 天前
互联网大数据求职面试:从Zookeeper到数据挖掘的技术探讨
大数据·redis·zookeeper·面试·互联网·数据采集·技术栈