redis-Ubuntu20.04安装Redis7.2.5.md

redis-Ubuntu20.04安装Redis7.2.5.md


前篇

  • s

安装

ChatGPT 回复, 关键词 Ubuntu 20.04 安装 Redis 7.2.5

要在 Ubuntu 20.04 上安装 Redis 7.2.5,可以按照以下步骤操作:

  1. 更新系统软件包:

    1
    sudo apt upgrade
  2. 安装依赖:

    1
    sudo apt install build-essential tcl
  3. 下载 Redis 7.2.5 源代码: 你可以从 Redis 官方网站或 GitHub 上下载最新的 Redis 源代码。这里假设从 Redis 官方网站下载:

    1
    wget http://download.redis.io/releases/redis-7.2.5.tar.gz
  4. 解压源代码包:

    1
    2
    tar xzf redis-7.2.5.tar.gz
    cd redis-7.2.5
  5. 编译 Redis:

    1
    make
  6. 运行测试(可选):

    1
    make test
  7. 安装 Redis:

    1
    sudo make install
  8. 设置 Redis 配置文件和启动脚本: 创建目录并复制默认的配置文件:

    1
    2
    sudo mkdir /etc/redis
    sudo cp redis.conf /etc/redis

    编辑 redis.conf 文件,确保以下行是正确的:

    1
    sudo vim /etc/redis/redis.conf
    1
    2
    3
    4
    5
    6
    supervised systemd // 修改 supervised 的值为 systemd

    protected-mode no # 修改为 no 值
    bind 0.0.0.0 -::1 # bind 修改为 0.0.0.0 表示接受所有的 ip 来链接

    stop-writes-on-bgsave-error no # 修改为 no, 保存数据到硬盘
  9. 创建 systemd 服务文件:

    1
    sudo vim /etc/systemd/system/redis.service

    在文件中添加以下内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    ini复制代码[Unit]
    Description=Redis In-Memory Data Store
    After=network.target

    [Service]
    User=redis
    Group=redis
    ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
    ExecStop=/usr/local/bin/redis-cli shutdown
    Restart=always

    [Install]
    WantedBy=multi-user.target
  10. 创建 Redis 用户和组:

    1
    2
    3
    4
    sudo adduser --system --group --no-create-home redis
    sudo mkdir /var/lib/redis
    sudo chown redis:redis /var/lib/redis
    sudo chmod 770 /var/lib/redis
  11. 启动并启用 Redis 服务:

    1
    2
    sudo systemctl start redis
    sudo systemctl enable redis
  12. 验证 Redis 是否成功运行:

    1
    sudo systemctl status redis

你也可以通过 Redis CLI 验证:

1
redis-cli ping

应该返回 PONG

通过以上步骤,你应该能够在 Ubuntu 20.04 上成功安装和配置 Redis 7.2.5。


配置安全密码

  1. 修改配置文件

    1
    2
    3
    4
    vim /etc/redis/redis.conf

    # 解开 requirepass 注释, 设置并设置密码
    requirepass YOUR_PASSWORD
  2. 重启服务

    1
    systemctl stop redis && systemctl start redis

配置外网访问

  • 默认只能本机访问, 需要修改配置

    1
    2
    3
    4
    vim /etc/redis/redis.conf

    protected-mode no # 修改为 no 值
    bind 0.0.0.0 -::1 # bind 修改为 0.0.0.0 表示接受所有的 ip 来链接
  • 重启服务

    1
    systemctl stop redis && systemctl start redis

配置保存数据到硬盘

  • 不配置会报错

    1
    MISCONF Redis is configured to save RDB snapshots, but it's currently unable to persist to disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error., rerr: MISCONF Redis is configured to save RDB snapshots, but it's currently unable to persist to disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
  1. 修改配置文件

    1
    2
    3
    vim /etc/redis/redis.conf

    stop-writes-on-bgsave-error no # 修改为 no, 保存数据到硬盘
  2. 重启服务

    1
    systemctl stop redis && systemctl start redis