Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 安裝 Redis 完整指南:從源碼編譯到常見錯誤修復

編輯:關於 Linux

Redis 作為高效能鍵值存儲,廣泛用於快取、Session、佇列、排行榜等場景。本文詳細記錄 Linux 上從源碼編譯安裝 Redis、解決 jemalloc 編譯錯誤、配置系統服務、基本安全加固與效能調優全流程。

環境準備

# 安裝編譯工具鏈
# RHEL/CentOS/Rocky/Alma
sudo dnf install gcc gcc-c++ make tcl

# Ubuntu/Debian
sudo apt install build-essential tcl

源碼編譯安裝

  1. 下載穩定版:
    VERSION=7.2.5
    wget https://download.redis.io/releases/redis-${VERSION}.tar.gz
    tar xzf redis-${VERSION}.tar.gz
    cd redis-${VERSION}
  2. 編譯 (關鍵:指定 malloc=libc 避開 jemalloc 問題):
    make MALLOC=libc
    # 或永久生效
    echo "MALLOC=libc" >> .make.settings
    make

    原理: Redis 預設使用 jemalloc,部分舊版核心/glibc 環境缺少 jemalloc 開發頭文件導致編譯失敗 (jemalloc/jemalloc.h: No such file or directory)。指定 MALLOC=libc 改用系統 glibc malloc,犧牲少量記憶體碎片管理效能,換取編譯通過與相容性。

  3. 測試 (可選但推薦):
    make test
    # 需安裝 tcl,耗時 1-2 分鐘,全綠通過表示穩定
  4. 安裝:
    sudo make install PREFIX=/usr/local/redis
    # 預設安裝到 /usr/local/bin,PREFIX 可自訂

配置系統服務

  1. 建立配置目錄與檔案:
    sudo mkdir -p /etc/redis /var/lib/redis /var/log/redis
    sudo cp redis.conf /etc/redis/redis.conf
    sudo chown -R redis:redis /var/lib/redis /var/log/redis  # 需先建立 redis 使用者
  2. 關鍵配置項 (/etc/redis/redis.conf):
    bind 127.0.0.1 -::1          # 僅監聽本地,生產環境按需放開
    port 6379
    daemonize yes                # 守護進程模式
    pidfile /var/run/redis/redis.pid
    logfile /var/log/redis/redis.log
    dir /var/lib/redis           # RDB/AOF 檔案目錄
    appendonly yes               # 開啟 AOF 持久化
    appendfsync everysec         # 平衡效能與安全
    maxmemory 2gb                # 記憶體上限,按機器配置
    maxmemory-policy allkeys-lru # 淘汰策略
    requirepass YourStrongPass   # 必設密碼
    rename-command FLUSHDB ""    # 禁用危險命令
    rename-command FLUSHALL ""
    rename-command KEYS ""
  3. 建立 systemd 服務檔 (/etc/systemd/system/redis.service):
    [Unit]
    Description=Redis In-Memory Data Store
    After=network.target
    
    [Service]
    Type=notify
    User=redis
    Group=redis
    ExecStart=/usr/local/redis/bin/redis-server /etc/redis/redis.conf
    ExecStop=/usr/local/redis/bin/redis-cli shutdown
    Restart=on-failure
    LimitNOFILE=65535
    
    [Install]
    WantedBy=multi-user.target
  4. 建立 redis 使用者並啟動:
    sudo useradd -r -s /sbin/nologin -M redis
    sudo systemctl daemon-reload
    sudo systemctl enable --now redis
    sudo systemctl status redis

常見編譯/運行錯誤修復表

錯誤現象原因解決
jemalloc/jemalloc.h: No such file缺少 jemalloc-develmake MALLOC=libc 或 dnf install jemalloc-devel
Newer version of jemalloc required系統 jemalloc 版本過舊make MALLOC=libc (推薦)
Can't open config file配置檔路徑錯誤確認 -c /etc/redis/redis.conf 或 CONFIG_FILE 環境變數
Permission denied 啟動失敗目錄權限/SELinuxchown -R redis:redis /var/lib/redis /var/log/redis;setsebool -P redis_enable_notify on
max number of clients reached連線數超限調大 maxclients、檢查連線洩漏、使用連線池
OOM killer 終止進程記憶體不足設定 maxmemory、啟用 swap、調整 vm.overcommit_memory=1

基本安全加固清單

  • ✅ 設定強密碼 requirepass
  • ✅ 綁定本地/內網 IP bind
  • ✅ 禁用/重命名危險命令 (FLUSHALL、KEYS、CONFIG)
  • ✅ 啟用 TLS (Redis 6.0+ 支援 tls-port、tls-cert-file)
  • ✅ 限制最大記憶體 maxmemory + 淘汰策略
  • ✅ 定期備份 RDB/AOF 到異地

效能基準測試

# 內建基準工具
redis-benchmark -h 127.0.0.1 -p 6379 -a YourPass -n 100000 -c 50 -t set,get
# -n 總請求數 -c 並發連線 -t 測試命令類型

遵循本指南,可在 Linux 上構建穩定、安全、高效能的 Redis 服務節點,為上層業務提供可靠的資料基礎設施。

Copyright © Windows教程網 All Rights Reserved