#---------------------------------------------------
#redis5.0.3自动安装脚本,可自行修改redis版本
#---------------------------------------------------
#---------------------------------------------------
#1、安装编译依赖gcc环境
echo '(1/8)、开始检测源码编译依赖gcc环境'
gccVersion=`gcc --version`
if [ "$gccVersion" = "" ]
then
yum install -y gcc-c++
else
echo '检测到gcc已经安装过,将跳过安装'
fi
#---------------------------------------------------
#2、下载redis安装包
echo '(2/8)、开始下载redis安装包'
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
#---------------------------------------------------
#3、解压安装包
echo '(3/8)、开始解压安装包'
tar -zxvf redis-5.0.3.tar.gz
#---------------------------------------------------
#4、编译安装包
echo '(4/8)、开始编译安装包'
cd redis-5.0.3
make
#---------------------------------------------------
#5、安装redis到指定目录/usr/local/redis
echo '(5/8)、开始安装redis到指定目录/usr/local/redis'
make install PREFIX=/usr/local/redis
#---------------------------------------------------
#6、复制并修改redis配置
echo '(6/8)、开始复制并修改redis配置'
cp redis.conf /usr/local/redis/bin/redis.conf
sed -i 's/daemonize no/daemonize yes/g' /usr/local/redis/bin/redis.conf
sed -i 's/bind 127.0.0.1/# bind 127.0.0.1/g' /usr/local/redis/bin/redis.conf
sed -i 's/protected-mode yes/protected-mode no/g' /usr/local/redis/bin/redis.conf
sed -i 's/# requirepass foobared/requirepass Dlz202278/g' /usr/local/redis/bin/redis.conf
sed -i 's/supervised no/supervised systemd/g' /usr/local/redis/bin/redis.conf
#---------------------------------------------------
#7、后台启动redis
echo '(7/8)、开始后台启动redis'
cd /usr/local/redis/bin/
./redis-server redis.conf
#---------------------------------------------------
#8、设置redis开机启动
echo '(8/8)、开始设置redis开机启动'
autoStartInfo="[Unit]\n:
Description=redis-server\n
After=network.target\n
[Service]\n
Type=forking\n
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf\n
PrivateTmp=true\n
[Install]\n
WantedBy=multi-user.target"
echo -e $autoStartInfo >> /etc/systemd/system/redis.service
ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis
systemctl daemon-reload
systemctl start redis.service
systemctl enable redis.service
echo 'redis自动安装完成'
ps -ef | grep redis
服务操作命令
systemctl start redis.service #启动redis服务
systemctl stop redis.service #停止redis服务
systemctl restart redis.service #重新启动服务
systemctl status redis.service #查看服务当前状态
systemctl enable redis.service #设置开机自启动
systemctl disable redis.service #停止开机自启动