• Linux系统安装redis并配置为服务


    一、Linux环境

    1、下载

    官网提供的源码下载地址:

    https://github.com/redis/redis/archive/7.0.5.tar.gz

    2、将源码上传至服务器

    在这里插入图片描述

    3、解压缩

    # 将解压缩后的文件放置在同目录的source文件夹下
    tar -zxvf redis-7.0.5.tar.gz -C ./source
    
    • 1
    • 2

    4、编译安装

    对源码进行编译、安装

    # PREFIX参数表示安装在哪个目录下
    make PREFIX=/home/ubuntu/redis/redis-7.0.5 install
    
    • 1
    • 2

    5、复制配置文件

    # 将源码提供的配置文件复制到安装目录下
    cp /home/ubuntu/redis/source/redis-7.0.5/redis.conf /home/ubuntu/redis/redis-7.0.5/bin/
    
    • 1
    • 2

    6、启动redis

    # 启动redis时需要指定配置文件
    ./redis-server /home/ubuntu/redis/redis-7.0.5/bin/redis.conf
    
    • 1
    • 2

    7、修改配置文件

    # 将下面一行配置添加注释,使其他主机可以访问redis服务
    # bind 127.0.0.1 -::1
    
    # 将以下配置取消注释,修改密码
    requirepass ********
    # 下面配置必须打开,密码才能生效
    protected-mode yes
    
    # 修改日志文件
    logfile "/home/ubuntu/redis/redis.7.0.5/bin/redis.log"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    8、设置服务

    # 将源码文件中utils/install_server.sh文件以下脚本注释
    
    #bail if this system is managed by systemd
    #_pid_1_exe="$(readlink -f /proc/1/exe)"
    #if [ "${_pid_1_exe##*/}" = systemd ]
    #then
    #       echo "This systems seems to use systemd."
    #       echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
    #       exit 1
    #fi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 创建服务脚本

      • vim install_server_command.sh
      sudo REDIS_PORT=6379 \
           REDIS_CONFIG_FILE=/home/ubuntu/redis/redis-7.0.5/bin/redis.conf \
           REDIS_LOG_FILE=/home/ubuntu/redis/redis.7.0.5/bin/redis.log \
           REDIS_DATA_DIR=/home/ubuntu/redis/redis.7.0.5/bin/ \
           REDIS_EXECUTABLE=`command -v /home/ubuntu/redis/redis-7.0.5/bin/redis-server` \
           /home/ubuntu/redis/source/redis-7.0.5/utils/install_server.sh
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    • 创建服务文件

      • cd /lib/systemd/system
      • sudo touch redis.service
      • sudo chmod 644 redis.service
      • sudo vim redis.service
      [Unit]
      Description=Redis
      After=network.target
      
      [Service]
      ExecStart=/home/ubuntu/redis/redis-7.0.5/bin/redis-server /home/ubuntu/redis/redis-7.0.5/bin/redis.conf --daemonize no
      ExecStop=/home/ubuntu/redis/redis-7.0.5/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown
      
      [Install]
      WantedBy=multi-user.target
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    • 创建软链接,为服务自启动准备

    ln -s /lib/systemd/system/redis.service /etc/systemd/system/multi-user.target.wants/redis.service
    
    • 1
    • 刷新配置
    systemctl daemon-reload
    
    • 1
    • 杀死已存在的redis进程
    ps aux|grep redis
    kill -9 [pid]
    
    • 1
    • 2
    • 启动redis
    systemctl start redis
    
    • 1
  • 相关阅读:
    低调而无为而治,藏在超级应用背后的道家哲学
    基于javaweb+mysql的在线商城购物商城水果蔬菜批发商城(前台、后台)
    redis
    k8s.7-Kubernetes集群UI及主机资源监控
    What makes training multi-modal classification networks hard?
    《canvas》之第5章 文本操作
    2022年,谁才是编程语言中的天选之子?
    实体和json
    gpt-4-all模型中转实现
    Java Socket实现简易多人聊天室传输聊天内容或文件
  • 原文地址:https://blog.csdn.net/qq_36234720/article/details/134007043