1. #---------------------------------------------------
    2. #redis5.0.3自动安装脚本,可自行修改redis版本
    3. #---------------------------------------------------
    4. #---------------------------------------------------
    5. #1、安装编译依赖gcc环境
    6. echo '(1/8)、开始检测源码编译依赖gcc环境'
    7. gccVersion=`gcc --version`
    8. if [ "$gccVersion" = "" ]
    9. then
    10. yum install -y gcc-c++
    11. else
    12. echo '检测到gcc已经安装过,将跳过安装'
    13. fi
    14. #---------------------------------------------------
    15. #2、下载redis安装包
    16. echo '(2/8)、开始下载redis安装包'
    17. wget http://download.redis.io/releases/redis-5.0.3.tar.gz
    18. #---------------------------------------------------
    19. #3、解压安装包
    20. echo '(3/8)、开始解压安装包'
    21. tar -zxvf redis-5.0.3.tar.gz
    22. #---------------------------------------------------
    23. #4、编译安装包
    24. echo '(4/8)、开始编译安装包'
    25. cd redis-5.0.3
    26. make
    27. #---------------------------------------------------
    28. #5、安装redis到指定目录/usr/local/redis
    29. echo '(5/8)、开始安装redis到指定目录/usr/local/redis'
    30. make install PREFIX=/usr/local/redis
    31. #---------------------------------------------------
    32. #6、复制并修改redis配置
    33. echo '(6/8)、开始复制并修改redis配置'
    34. cp redis.conf /usr/local/redis/bin/redis.conf
    35. sed -i 's/daemonize no/daemonize yes/g' /usr/local/redis/bin/redis.conf
    36. sed -i 's/bind 127.0.0.1/# bind 127.0.0.1/g' /usr/local/redis/bin/redis.conf
    37. sed -i 's/protected-mode yes/protected-mode no/g' /usr/local/redis/bin/redis.conf
    38. sed -i 's/# requirepass foobared/requirepass Dlz202278/g' /usr/local/redis/bin/redis.conf
    39. sed -i 's/supervised no/supervised systemd/g' /usr/local/redis/bin/redis.conf
    40. #---------------------------------------------------
    41. #7、后台启动redis
    42. echo '(7/8)、开始后台启动redis'
    43. cd /usr/local/redis/bin/
    44. ./redis-server redis.conf
    45. #---------------------------------------------------
    46. #8、设置redis开机启动
    47. echo '(8/8)、开始设置redis开机启动'
    48. autoStartInfo="[Unit]\n:
    49. Description=redis-server\n
    50. After=network.target\n
    51. [Service]\n
    52. Type=forking\n
    53. ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf\n
    54. PrivateTmp=true\n
    55. [Install]\n
    56. WantedBy=multi-user.target"
    57. echo -e $autoStartInfo >> /etc/systemd/system/redis.service
    58. ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis
    59. systemctl daemon-reload
    60. systemctl start redis.service
    61. systemctl enable redis.service
    62. echo 'redis自动安装完成'
    63. 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 #停止开机自启动