Are you sure you don't want to take a look? One-click installation and configuration of related services on CentOS7 system (install MySQL8, JDK11, Minio, Redis6.2, gcc environment in rpm mode in offline mode), save you worry and effort!

I. Introduction

Deploying and configuring various services is a common and tedious task in an enterprise or personal server environment . Especially in an offline environment , without an internet connection or network limitations, installing and configuring each service from scratch can be time-consuming and complicated .

In order to solve this problem, I provide you with a convenient solution, that is, a script for CentOS 7 one-click installation and configuration of related services. This script aims to provide users with an easy way to install and configure the following services in CentOS 7 systems in an automated and efficient manner: MySQL 8, MinIO, Redis 6.2 and gcc environments.

By using this one-click install script, users don't need to manually download packages, resolve dependencies, or perform complex configuration steps. Scripts will automatically handle these tasks and ensure that each service is properly installed and configured in the system.

Additionally, the script works in offline mode, which means users can complete the installation and configuration of the service without an internet connection. It has the required packages and dependencies built in and can run standalone without internet access.

Using this one-click installation script, users can save time and effort and quickly build a fully functional server environment. Whether deploying the service in an enterprise or using it in a personal project, this one-click installation script allows you to easily set up the services you need and reduce the tedious manual process.

Note that the script provides default installation and configuration options, which you can customize to suit your needs. Before running the script, make sure you have the appropriate permissions and read the instructions and prompts in the script carefully.

Start using CentOS 7 one-click installation and scripts to configure related services, making your deployment process more worry-free and effortless!

Two, MySQL8 installation configuration script

Create a corresponding folder in the server, and upload the rpm package of the relevant application to the corresponding folder in the server.
insert image description here
insert image description here
Upload the script to the corresponding folder. Before executing the script, you need to switch directories

# 切换目录
cd /usr/local/mysql
# 分配执行权限
chmod +x mysql_install\(8.0\).sh

insert image description here
Execute the following command:

./mysql_install\(8.0\).sh

Wait for a while for the program to be installed and configured. When the script is executed, it will automatically restart. Just reconnect to the server. After reconnecting to the server command console, execute:

systemctl status mysqld.service

Check the running status of mysql, as follows, the installation is successful.
insert image description here

4. To view the initialization password, execute:

grep "A temporary password is generated for root@localhost: " /var/log/mysql/error.log

implement:

mysql -u root -p

Enter the MySQL command line, enter the above temporary password to enter
insert image description here
5, modify the password, execute:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'admin123';

insert image description here

The specific MySQL8 script content is as follows:

#! /bin/bash
###MySQL8.0.33 数据库自动安装脚本
# mysql-8.0.33-1.el7.x86_64.rpm-bundle.tar
tarPath=/usr/local/mysql/
rpmPath=/usr/local/mysql/
tarVersion=-8.0.33-1.el7.x86_64
tarFile=mysql${tarVersion}.rpm-bundle

#mysql 安装路径
installPath=/usr/local/mysql/

#my.cnf 配置文件
mysqlcnf=/etc/my.cnf

#mysql serverid 需要设置唯一的id,比如ip+3位数字(配备主从服务器时使用)
#mysqlServerid=1010101

#mysql 密码
mysqlPass="bjyz@123"


#mysql数据目录
dase_default=${installPath}
data_dir=${dase_default}/data

#判断服务是否存在
CheckServiceRunning() 
{
    
    
    if [[ -n `netstat -tlunp|grep -w {
     
     mysqlPort}` || -n `ps -ef|grep mysqld|grep -v grep` || -n `systemctl status mysqld 2>/dev/null` ]]; then
        echo -e "\e[31m mysql已存在! \e[0m"
        exit 1
    fi
}

#校验是否为root用户
CheckRoot()
{
    
    
    if [ $(id -u) != "0" ]; then
        echo -e "\e[31m 当前不是root用户,禁止操作 \e[0m"
        exit 1
    fi
}

#拷贝tar包
DecompressionTar()
{
    
    
    if [ ! -e ${tarPath}${tarFile}.tar ]; then
        echo -e "\e[31m ${tarPath}${tarFile}.tar 不存在!请检查后重新执行脚本 \e[0m"
        exit 1
    fi

    #解压并复制到安装目录
    if [ ! -d ${installPath}${tarFile} ]; then
        tar -xvf ${tarPath}${tarFile}.tar -C ${tarPath}
    fi

    echo -e "\e[32m 软件已解压 \e[0m"
}

#检查冲突包,安装包
CheckPackage()
{
    
    
    rpm -qa | grep postfix 1>/dev/null
    if [ $? -eq 0 ]; then
        rpm -ev postfix-2.10.1-9.el7.x86_64
            if [ $? -ne 0 ]; then
            echo -e "\e[31m postfix卸载失败 \e[0m"
            exit 1
            fi
    fi

    rpm -qa | grep mariadb 1>/dev/null
    if [ $? -eq 0 ]; then
        rpm -ev mariadb-libs-5.5.68-1.el7.x86_64
        if [ $? -ne 0 ]; then
            echo -e "\e[31m mariadb卸载失败 \e[0m"
            exit 1
            fi
    fi

    # 一键安装:rpm -ivh *.rpm --nodeps --force
    rpm -ivh ${rpmPath}mysql-community-client-plugins${tarVersion}.rpm
    if [ $? -ne 0 ]; then
        echo -e "\e[31m community-client-plugins安装失败 \e[0m"
    fi

    rpm -ivh ${rpmPath}mysql-community-common${tarVersion}.rpm
    if [ $? -ne 0 ]; then
        echo -e "\e[31m community-common安装失败 \e[0m"
    fi

    rpm -ivh ${rpmPath}mysql-community-libs${tarVersion}.rpm
    if [ $? -ne 0 ]; then
        echo -e "\e[31m community-libs安装失败 \e[0m"
        exit 1
    fi

    rpm -ivh ${rpmPath}mysql-community-client${tarVersion}.rpm
    if [ $? -ne 0 ]; then
        echo -e "\e[31m community-client安装失败 \e[0m"
        exit 1
    fi

    rpm -ivh ${rpmPath}mysql-community-icu-data-files${tarVersion}.rpm
    if [ $? -ne 0 ]; then
        echo -e "\e[31m community-icu-data-files安装失败 \e[0m"
        exit 1
    fi

    rpm -ivh ${rpmPath}mysql-community-server${tarVersion}.rpm
    if [ $? -ne 0 ]; then
        echo -e "\e[31m community-server安装失败 \e[0m"
        exit 1
    fi
}


# 添加读写权限、创建用户组
AssignMySQL() {
    
    
    mkdir -p ${data_dir}                  # 创建数据卷
    mkdir -p /var/log/mysql/              # 创建日志文件夹
    touch /var/log/mysql/error.log        # 创建日志文件
    groupadd mysql                        # 创建 mysql 用户组
    useradd -r -g mysql mysql             # 创建 mysql用户并加入 mysql 用户组
    cat /etc/group                        # 显示/etc/group文件内容,可验证用户组是否创建成功
    cat /etc/passwd                       # 显示/etc/passwd文件内容,可验证用户是否创建成功
    chown -R mysql:mysql /var/lib/mysql/  # 设置目录权限
    chown -R mysql:mysql /usr/local/mysql/data     # 更改属主目录权限
    chown -R mysql:mysql /usr/local/mysql/
    chmod 770 /usr/local/mysql/data
    chmod 770 /var/lib/mysql              # 更改日志目录的权限
    chmod 777 /var/run/mysqld/            # 更改主进程的权限
    chmod 777 /var/log/mysql/
    chmod 777 /var/log/mysql/error.log
    

}

ConfigureMySQL() {
    
    
    # 修改profile文件
    # vim /etc/profile
    # 在文件尾部追加下面的内容
    echo "export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib" >> /etc/profile
    # 保存/etc/profile,并应用此文件
    source /etc/profile
}

#更新cnf文件
# MySQL8.0在初始化时不支持修改lower_case_table_names参数
# 相关链接:https://blog.csdn.net/qq_26007225/article/details/121172462
UpdateCnf() {
    
    
    cat << EOF > /etc/my.cnf
[mysql]
# 默认字符集
default-character-set=utf8mb4

[client]
port=3306
socket=/var/lib/mysql/mysql.sock

[mysqld]
port = 3306
user = mysql
# 允许外部访问
bind-address=0.0.0.0

# 指定 MySQL 数据库的默认字符集
character-set-server=utf8mb4

# 设置 MySQL 执行中断的超时时间
interactive_timeout = 120
wait_timeout = 120

# 设置 MySQL 数据库的缓存大小
key_buffer_size = 16M
# query_cache_size = 128M

socket = /var/lib/mysql/mysql.sock
datadir = /usr/local/mysql/data
# basedir = /usr/local/mysql/


# 关闭查询缓存
# query_cache_type = 0

# 设置 MySQL 允许访问的最大连接数
max_connections = 1000

# 设置 MySQL 日志级别
log-error=/var/log/mysql/error.log
log_queries_not_using_indexes = 1
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1


#事务隔离级别,默认为可重复读,mysql默认可重复读级别(此级别下可能参数很多间隙锁,影响性能)
transaction_isolation = READ-COMMITTED

#TIMESTAMP如果没有显示声明NOT NULL,允许NULL值
explicit_defaults_for_timestamp = true


#开启bin log 功能
log-bin=mysql-bin

#binlog 记录内容的方式,记录被操作的每一行
binlog_format = ROW

#设置client连接mysql时的字符集,防止乱码
init_connect='SET NAMES utf8mb4'

#数据库字符集对应一些排序等规则,注意要和character-set-server对应
collation-server = utf8mb4_general_ci

#是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names = 1


[mysqld_safe]
log-error = /var/log/mysql/error.log
pid-file = /var/lib/mysql/mysqld.pid

# lc_messages_dir=/usr/local/mysql/share
# lc_messages=en_US

# include all files from the config directory
EOF

}

#重置密码
ResetPwd()
{
    
    

    # 启动之前初始化
    mysqld --initialize

    # 解决mysqld: File '/usr/local/mysql/data/mysql-bin.index' not found (OS errno 13 - Permission denied)
    sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

    sleep 2

    ln -s /var/lib/mysql/mysql.sock /tmp  #添加软连接

    
    systemctl start mysqld.service
    systemctl status mysqld.service

    # 从日志中获取mysql初始密码
    pwd=$(grep "A temporary password is generated for root@localhost: " /var/log/mysql/error.log | awk '{print $NF}')
    echo -e "\e[31m 初始密码是: \e[0m${pwd}"
    pwd=${pwd// /}
    echo -e "\e[31m 初始密码是: \e[0m${pwd}"
    
    # 修改密码规则
    mysql -u root --password="${pwd}" -e "SET GLOBAL validate_password.policy=0;" &&
    mysql -u root --password="${pwd}" -e "SET GLOBAL validate_password.length=1;" &&
    
    # 修改密码
    mysql -u root --password="${pwd}" -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '${mysqlPass}';" &&
    
    # 重启服务
    systemctl restart mysqld.service
    
    # 修改密码策略
    # mysql -uroot --password="${mysqlPass}" -e "SET GLOBAL validate_password.policy=STRONG;"
    
    # 重启 MySQL 服务
    # systemctl restart mysqld.service
    
    # 设置密码复杂度
    mysql -u root --password="${mysqlPass}" -e "USE mysql; UPDATE user SET Host='%' WHERE user='root';" &&
    
    # 刷新退出
    mysql -u root --password="${mysqlPass}" -e "flush privileges;"
    
    # 设置开启自启
    systemctl enable mysqld.service
    systemctl daemon-reload

    
    # 若需要,关闭防火墙并永久停用
    systemctl stop firewalld.service
    systemctl disable firewalld.service
}

#开启远程访问
# RemoteConnection()
# {
    
    
#     mysql -uroot -p${mysqlPass} -e "use mysql;"
#     mysql -uroot -p${mysqlPass} -e "update user set host='%' where user='root';"
#     mysql -uroot -p${mysqlPass} -e "flush privileges;"
#     mysql -uroot -p${mysqlPass} -e "grant all privileges on *.* to 'root'@'%' IDENTIFIED BY '${mysqlPass}' WITH GRANT OPTION;"
#     mysql -uroot -p${mysqlPass} -e "flush privileges;"
# }

main()
{
    
    
    ##1.判断是否已安装mysql
    CheckServiceRunning

    ##2.判断是否为ROOT用户
    CheckRoot

    ##3.拷贝tar包
    DecompressionTar

    ##4.检查冲突包,安装包
    CheckPackage

    ##5.添加读写权限、创建用户组
    AssignMySQL

    ##5.配置mysql
    ConfigureMySQL

    ##6.更新cnf文件
    UpdateCnf

    ##7.重置密码
    ResetPwd

    ##8.开启远程访问
    # RemoteConnection
    
    echo -e "\e[32m mysql安装完成 \e[0m"

    sleep 3

    echo -e "\e[32m CentOS7系统即将重启,加载MySQL配置...... \e[0m"

    systemctl enable mysqld.service
    systemctl start mysqld.service
    systemctl status mysqld.service


    reboot
}

main

3. JDK11 configuration and installation

cd to the corresponding directory, execute:

cd /usr/local/jdk11/

2. Give the script execution permission and execute:

# 赋予执行权限
chmod +x jdk11_install.sh
# 查看权限组
ll

insert image description here

3. Execute:

# 执行脚本
./jdk11_install.sh
# 查看java版本
java -version

Operate according to the prompts in the program. After the installation is complete,
insert image description here
the specific script content of JDK11 installation is as follows:

#!/bin/bash

# JDK 安装包路径
jdkPath=/usr/local/jdk11/

# JDK 安装路径
installPath=/usr/local/jdk11/jdk-11.0.19

# JDK 安装包名称
jdkFile=jdk-11.0.19_linux-x64_bin.tar.gz


# 检查是否有root权限运行此脚本
CheckRoot() {
    
     
    if [ $(id -u) != "0" ]; then 
        echo -e "\e[31m 当前不是 root 用户,禁止操作 \e[0m" 
        exit 1 
    fi 
} 

# 检查服务是否正在运行
checkServiceRunning() {
    
    
    if [[ -n $(java -version 2>&1 | grep "openjdk version") ]]; then
        echo -e "\e[31m JDK 服务已存在!\e[0m"

        read -p "是否卸载现有的 JDK 1.8 服务? (y/n): " choice
        case "$choice" in
            y|Y )
                # 执行卸载 JDK 1.8 的操作
                echo "正在卸载 JDK 1.8 服务..."
                rpm -qa | grep java | xargs rpm -e --nodeps
                echo "JDK 1.8 服务已卸载"
                ;;
            n|N )
                echo "取消卸载 JDK 1.8 服务"
                exit 1
                ;;
            * )
                echo "无效的输入,取消卸载 JDK 1.8 服务"
                exit 1
                ;;
        esac
    fi
}

# 检查JDK压缩包是否被解压
installJDK() {
    
    
    if [ ! -e $jdkFile ]; then
        echo "JDK tar包不存在!!!"
        exit 1
    fi
    cd $installPath
    mkdir jdk-11.0.19
    tar -xzvf $jdkFile -C $installPath --strip-components 1
}


# 配置Java环境变量
configureJDK() {
    
    
    echo "export JAVA_HOME=$installPath" >> /etc/profile
    echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> /etc/profile
    echo "export CLASSPATH=.:\$JAVA_HOME/jre/lib/rt.jar:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar" >> /etc/profile
    echo "export CLASSPATH=\$JAVA_HOME/lib/" >> /etc/profile
    source /etc/profile
}


# 主函数
main() {
    
    
    # 检查是否有root权限运行此脚本
    CheckRoot

    # 检查服务是否正在运行
    checkServiceRunning

    # 安装 JDK
    installJDK

    # 配置环境变量
    configureJDK

    # 检查JDK版本
    java -version

    echo -e "\e[32m JDK 11 安装完成! \e[0m"
}

# 执行主函数
main

Fourth, the configuration and installation of Minio

1. cd to the corresponding directory, execute:

cd /usr/local/minio

2. Give the permission to execute, execute:

# 赋予脚本执行的权限
chmod +x minio_install.sh
# 查看权限组
ll

insert image description here
3. Execute:

./minio_install\(8.0\).sh

Wait for a while for the program to be installed and configured. If the following picture appears, the installation is successful:
insert image description here
4. Check the status and execute:

systemctl status minio.service

As shown in the figure below:
insert image description here
the content in the red circle in the figure is the background access address of Minio, 9000 in the first line is a fixed port, and the second line is a dynamic port. If you need to modify the port before initialization, use "vim minio_install.sh" (press i to enter the edit state, modify the ConfigureMinio() method in the script); if you modify the port after running, use "vim /etc/default/minio/minioEnv" to modify the port, and then use "systemctl restart minio.service" to check the status after restarting" systemctl status minio.service"

# 编辑脚本内容
vim minio_install.sh
# 编辑minio的运行环境
vim /etc/default/minio/minioEnv
# 重启minio服务
systemctl restart minio.service

In addition, before executing the script, if you need to change the account password, use the Shell connection tool to edit the minio_install.sh script, find the accessKey and secretKey, and modify it.
The specific script content of Minio:

#!/bin/bash 
### Minio 安装启动脚本 
 
# Minio 安装包路径
tarPath=/usr/local/
rpmPath=/usr/local/minio/
tarVerion=-20230518000536.0.0.x86_64
tarFile=minio${tarVerion}.rpm

# Minio 安装路径 
installPath=/usr/local/minio/ 
 
# Minio 配置文件路径 
configPath=/etc/default/minio/
 
# Minio Access Key 
accessKey=minioAdmin
 
# Minio Secret Key 
secretKey=minioAdmin
 
# Minio 端口号 
minioPort=9000
 
# Minio 日志路径 
logPath=/var/log/minio.log 

# Minio 数据存储路径 
dataPath=${installPath}/data

# Minio 服务名称 
serviceName=minio.service

# 设置 MINIO_VOLUMES 变量的值
MINIO_VOLUMES="/usr/local/minio/data"

# 判断服务是否存在
checkServiceRunning() {
    
    
    if [[ -n $(netstat -tlunp | grep -w $minioPort) || -n $(ps -ef | grep minio | grep -v grep) || -n $(systemctl status minio 2>/dev/null) ]]; then
        echo -e "\e[31m Minio服务已经存在! \e[0m"
        exit 1
    fi
}


# 添加执行权限、创建用户组
AssignMinio() {
    
    
    mkdir /mnt/data                     # 创建数据磁盘目录
    mkdir /usr/local/minio/data         # 创建数据卷
    chmod 770 /usr/local/minio/data     # 添加执行权限
    
    groupadd minio-user                 # 创建 minio-user 用户组
    useradd -g minio-user minio-user    # 创建 minio-user 用户并加入 minio-user 用户组
    cat /etc/group                      # 显示/etc/group文件内容,可验证用户组是否创建成功
    cat /etc/passwd                     # 显示/etc/passwd文件内容,可验证用户是否创建成功
    chown -R minio-user:minio-user ${MINIO_VOLUMES}   # 设置 /usr/local/minio/data 目录的所有者为 minio-user 用户及所属组
}
 
# 检查是否为 root 用户 
CheckRoot() {
    
     
    if [ $(id -u) != "0" ]; then 
        echo -e "\e[31m 当前不是 root 用户,禁止操作 \e[0m" 
        exit 1 
    fi 
} 
 
# 创建 Minio 配置文件目录 
CreateConfigDir() {
    
     
    if [ ! -d $configPath ]; then 
        mkdir -p $configPath 
    fi 
} 
 
# 创建 Minio 数据存储目录 
CreateDataDir() {
    
     
    if [ ! -d $dataPath ]; then 
        mkdir -p $dataPath 
    fi 
} 
 
# 安装 Minio
InstallMinio() {
    
    
    if [ ! -e $tarFile ]; then
        echo "Minio rpm package不存在!!!"
        exit 1
    fi
    rpm -ivh $tarFile
    if [ $? -ne 0 ]; then
        echo "Minio 安装失败!!!"
        exit 1
    fi
}
 
# 配置 Minio 
ConfigureMinio() {
    
     
    cat << EOF > ${configPath}/minioEnv
#######
MINIO_ROOT_USER="${accessKey}"
MINIO_ROOT_PASSWORD="${secretKey}"
MINIO_VOLUMES="/usr/local/minio/data"
MINIO_OPTS="--console-address :9999 --address 0.0.0.0:9000"
######
EOF
}

# 创建 Minio 服务 
CreateMinioService() {
    
     
    cat << EOF > /etc/systemd/system/$serviceName
######
[Unit] 
Description=Minio 
Documentation=https://docs.min.io
Wants=network-online.target 
After=network-online.target 
AssertFileIsExecutable=/usr/local/bin/minio
 
[Service]
WorkingDirectory=/usr/local/minio

User=minio-user
Group=minio-user
PermissionsStartOnly=true

EnvironmentFile=/etc/default/minio/minioEnv

ExecStartPre=/bin/bash -c "if [ -n \"${MINIO_VOLUMES}\" ]; then echo \"Variable MINIO_VOLUMES not set in /etc/default/minio/minioENV"; exit 1; fi"
ExecStart=/usr/local/bin/minio server /usr/local/minio/data

StandardOutput=journal
StandardError=inherit

# Specifies the maximum file descriptor number that can be opened by this process*
LimitNOFILE=65536
# Disable timeout logic and wait until process is stopped*
TimeoutStopSec=0
# SIGTERM signal is used to stop Minio*
KillSignal=SIGTERM
SendSIGKILL=no
SuccessExitStatus=0
 
[Install] 
WantedBy=multi-user.target

######
EOF
} 
 
# 启动 Minio 服务 
StartMinioService() {
    
     
    systemctl daemon-reload
    systemctl enable $serviceName
    systemctl start $serviceName
    systemctl restart $serviceName
    systemctl status $serviceName
} 
 
# 主函数 
main() {
    
     
    # 检查是否为 root 用户 
    CheckRoot

    # 添加执行权限
    AssignMinio
 
    # 创建 Minio 配置文件目录 
    CreateConfigDir 
 
    # 创建 Minio 数据存储目录 
    CreateDataDir 
 
    # 安装 Minio 
    InstallMinio 
 
    # 配置 Minio 
    ConfigureMinio 
 
    # 创建 Minio 服务 
    CreateMinioService 
 
    # 启动 Minio 服务 
    StartMinioService 
 
    echo -e "\e[32m Minio 安装完成 \e[0m" 
} 
 
# 执行主函数
main

Five, install the gcc environment

1. cd to the corresponding directory, execute:

cd /usr/local/gcc

2. Check whether there is a gcc environment, or not, as shown in the figure below:
insert image description here
3. Install the gcc environment, execute: rpm -ivh *.rpm --nodeps --force, as shown in the figure below:
insert image description here
4. Check the gcc version and environment, execute :gcc -v
insert image description here

Six, Redis configuration installation script

1. cd to the corresponding directory, execute:

cd /usr/local/redis

2. Give the permission to execute, execute:

chmod +x redis_install.sh

insert image description here

3. Execute: ./redis_install(8.0).sh, wait for the program to install and configure, when the script is executed, it will automatically restart, just reconnect to the server, after reconnecting to the server command console, execute: systemctl status redis. service, check the running status of redis, as follows, the installation is successful.
insert image description here
The specific script content is as follows:

#!/bin/bash

# Redis 安装路径
installPath=/usr/local/redis/

# Redis 压缩包路径
tarPath=/usr/local/redis/redis-6.2.13.tar.gz

# 检查是否为 root 用户
checkRoot() {
    
    
    if [ "$(id -u)" != "0" ]; then
        echo -e "\e[31m 当前不是 root 用户,禁止操作 \e[0m" 
        exit 1
    fi
}

# 解压 Redis 压缩包
extractRedis() {
    
    
    echo "解压Redis压缩包中..."
    mkdir -p $installPath
    tar -xvzf $tarPath -C $installPath
    echo "Redis压缩包解压完成!!!"
}

# 编译和安装 Redis
compileAndInstallRedis() {
    
    
    echo "Compiling and installing Redis..."
    cd ${installPath}/redis-6.2.13
    make
    sleep 10s
    make install PREFIX=/usr/local/redis
    echo "Redis安装完成!!!"
}


# 配置 Redis 为后台运行
configureRedis() {
    
    
    echo "Configuring Redis..."
    # 配置Redis环境变量
    echo "export PATH=$PATH:/usr/local/redis/bin" >> /etc/profile
    # 设置环境变量生效
    source /etc/profile
    cp ${installPath}/redis-6.2.13/redis.conf /usr/local/redis/bin

    cd /usr/local/redis/bin
    # sed -i 's/^daemonize no$/daemonize yes/' redis.conf
    sed -i 's/bind 127.0.0.1/# bind 127.0.0.1/g' redis.conf
    sed -i 's/daemonize no/daemonize yes/g' redis.conf
    sed -i 's/protected-mode yes/protected-mode no/g' redis.conf


    # 创建并编辑redis.service文件
cat > /usr/lib/systemd/system/redis.service << EOF
[Unit]
Description=redis-server
After=network.target

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

[Install]
WantedBy=multi-user.target
EOF

    echo "Redis 配置完成!!!"
}

# 启动 Redis 服务
startRedis() {
    
    
    echo "启动Redis服务中...."
    cd /usr/local/redis/bin
    ./redis-server redis.conf
    echo "Redis服务启动完成"
}

# 设置 Redis 服务开机自启
enableRedisOnStartup() {
    
    
    echo "Redis服务配置开机自启中...."
    cp /usr/local/redis/redis-6.2.13/redis_init_script /etc/init.d/redis
    sed -i "s@^REDISPORT=.*@REDISPORT=6379@" /etc/init.d/redis
    sed -i "s@^REDISCONF=.*@REDISCONF=${installPath}bin/redis.conf@" /etc/init.d/redis
    chmod +x /etc/init.d/redis
    chkconfig --add redis
    chkconfig redis on
    echo "Redis服务开机自启服务配置完成!!!"
}

# 主函数
main() {
    
    
    # 检查是否为 root 用户
    checkRoot

    # 解压 Redis 压缩包
    extractRedis

    # 编译和安装 Redis
    compileAndInstallRedis

    # 配置 Redis 为后台运行
    configureRedis

    # 启动 Redis 服务
    startRedis

    # 设置 Redis 服务开机自启
    enableRedisOnStartup

    echo "Redis 安装且配置完成!!!"

    sleep 3

    echo -e "\e[32m CentOS7系统即将重启,加载Redis配置...... \e[0m"

    reboot
}

# 执行主函数
main

Redis configuration installation script 2, the content of the script is as follows:

#!/bin/bash


# 进入redis安装目录
cd /usr/local/redis

# 解压Redis源代码
tar -xvf redis-6.2.13.tar.gz -C /usr/local/redis

# 进入解压后的源代码目录
cd redis-6.2.13

# 编译Redis
make

sleep 40

# 指定安装目录并进行安装
make install PREFIX=/usr/local/redis

sleep 10

# 拷贝配置文件到bin目录
cp /usr/local/redis/redis-6.2.13/redis.conf /usr/local/redis/bin

# 进入bin目录
cd /usr/local/redis/bin/

# 修改配置文件
sed -i 's/bind 127.0.0.1/# bind 127.0.0.1/g' redis.conf
sed -i 's/daemonize no/daemonize yes/g' redis.conf
sed -i 's/protected-mode yes/protected-mode no/g' redis.conf

# 创建并编辑redis.service文件
cat > /usr/lib/systemd/system/redis.service << EOF
[Unit]
Description=redis-server
After=network.target

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

[Install]
WantedBy=multi-user.target
EOF

# 启动Redis服务
./redis-server redis.conf

# 等待Redis启动
sleep 5

# 查看进程
ps -ef | grep redis

# 开机自动启动
systemctl enable redis.service

# 启动redis服务
systemctl start redis.service

# 查看服务状态
systemctl status redis.service

reboot

Guess you like

Origin blog.csdn.net/qq_44723773/article/details/131728508