Linux 下shell命令一键安装redis并配置开机自启
installRedis.sh
#安装C++解码器
yum install -y gcc g++ gcc-c++ make
cd /usr/local
#下载redis压缩包
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
tar -zxvf redis-4.0.8.tar.gz
cd redis-4.0.8
#编译redis文件
make MALLOC=libc
cd src
make install PREFIX=/usr/local/redis
mkdir /usr/local/redis/etc
cd ../
mkdir -p /etc/redis
#通过sed-i命令修改redis配置文件文本
#(设置为后台启动)
sed -i 's/daemonize no/daemonize yes/g' redis.conf
#(关闭保护模式)
sed -i 's/protected-mode yes/protected-mode no/g' redis.conf
#(设置可远程访问)
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g' redis.conf
cp redis.conf /etc/redis
cp redis.conf /usr/local/redis
#修改设置开机启动
cat << EOF >> /etc/rc.local
/usr/local/redis/bin/redis-server /etc/redis/redis.conf
EOF
#修改配置文件防止虚拟机重启导致redis信息丢失
cat << EOF >> /etc/sysctl.conf
vm.overcommit_memory = 1
EOF
#修改配置文件实现开机服务启动
sed -i 's/\/etc\/redis\/${REDISPORT}.conf/\/etc\/redis\/redis.conf/g' /usr/local/redis-4.0.8/utils/redis_init_script
sed -i 's/CLIEXEC=\/usr\/local\/bin\/redis-cli/CLIEXEC=\/usr\/local\/redis\/bin\/redis-cli/g' /usr/local/redis-4.0.8/utils/redis_init_script
sed -i '/REDISPORT=6379/i #chkconfig: 2345 90 10' /usr/local/redis-4.0.8/utils/redis_init_script
sed -i 's/EXEC=\/usr\/local\/bin\/redis-server/EXEC=\/usr\/local\/redis\/bin\/redis-server/g' /usr/local/redis-4.0.8/utils/redis_init_script
cp -rf /usr/local/redis-4.0.8/utils/redis_init_script /etc/init.d/redis
#添加执行权限
chmod +x /etc/init.d/redis
#开启开机自启服务
chkconfig on redis
#启动redis服务
service redis start
执行完成之后
ps -ef|grep redis
可以查看到redis已经启动
继续输入:reboot 让虚拟机重启
重启后
ps -ef|grep redis
依旧显示redis正常运行