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

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

一、编译安装

1.安装依赖

apt-get install build-essential autoconf automake

2.下载

官方下载页面

cd /usr/local

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

wget https://github.com/redis/redis/archive/7.0.12.tar.gz

3.解压

tar -zxvf /usr/local/redis-7.0.12.tar.gz

4.编译安装

cd redis-7.0.12

安装到/usr/local/redis

make && make install

5.配置文件

配置文件在源码包里面

mkdir /etc/redis
cp /usr/local/redis-7.0.12/redis.conf /etc/redis/redis.conf

测试运行,ctrl+c取消

/usr/local/bin/redis-server /etc/redis/redis.conf

6.修改配置文件

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脚本管理

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

依次执行下面的命令

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

8.远程连接

使用redisinsight连接,不写账号,只写上面设定的密码即可
如果后期优化,可以设定账号密码,这里不再涉及

二、编译安装的一键脚本

1.创建脚本文件

vi /usr/local/redis7.sh

2.编辑

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

#!/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.运行

sh /usr/local/redis7.sh

猜你喜欢

转载自blog.csdn.net/ziqibit/article/details/131771294
今日推荐