#!/bin/bash
# -*- codeing = utf-8 -*-
# @Time : 2021/11/7 22:11
# @Author : xiaoguaishou
# @File : redis_install.sh
# @Software : PyCharm
#1.设置redis安装目录
redis_install=/home
#2.设置redis端口
#redis_port=6379
#3.设置redis版本号
redis_version=redis-3.2.8
#解压安装包
tar -zxvf $redis_version.tar.gz -C $redis_install
sleep 3
mv $redis_install/$redis_version $redis_install/redis
cd $redis_install/redis
#创建redis数据存放,日志目录
mkdir {data,logs}
#安装离线gcc依赖
rpm -ivh gcc/*.rpm --nodeps
sleep 5
#安装redis
make -j 4
sleep 5
make install
sleep 5
#127.0.0.1允许本地连接,0.0.0.0允许所有连接
sed -i 's/^bind 127.0.0.1/bind 0.0.0.0/g' $redis_install/redis/redis.conf
#使用yes后台运行
sed -i 's/daemonize no/daemonize yes/g' $redis_install/redis/redis.conf
#将yes改为no,开启远程连接
sed -i 's/protected-mode yes/protected-mode no/g' $redis_install/redis/redis.conf
#修改密码
sed -i 's/# requirepass foobared/requirepass XLSxuR1UFgsDQfYa/g' $redis_install/redis/redis.conf
#设置log输出目录
sed -i '/logfile/c\logfile "/home/redis/logs/redis.log"' $redis_install/redis/redis.conf
#设置数据存放的目录
sed -i '/dir .\//c\dir /home/redis/data' $redis_install/redis/redis.conf
#修改连接端口
#sed -i 's/port 6379/port 20012/g' /home/redis/redis.conf
echo "
[Unit]
Description=Redis
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/home/redis/src/redis-server /home/redis/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
" > /usr/lib/systemd/system/redis-server.service
#启动redis
systemctl daemon-reload
systemctl start redis-server.service
systemctl enable redis-server.service
systemctl status redis-server.service
#检查redis是否启动成功
status=`netstat -tunlp |grep 6379 |wc -l`
if [ $status -eq 1 ];then
echo "redis 启动成功"
else
echo "redis 启动失败"
fi