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 Memcached 1.6 部署實戰:源碼編譯、libevent 依賴、PHP 擴展整合、系統服務化與調優

編輯:Linux 教學

Memcached 作為分佈式內存緩存標準組件,生產部署需關注 **libevent 版本匹配、編譯優化選項、systemd 服務化、內存分配策略、PHP 客戶端擴展版本兼容** 等關鍵環節。本文以 CentOS 7 + Memcached 1.6.21 為例,提供從源碼到可觀測生產實例的完整路徑。

1. 依賴安裝與 libevent 編譯


# 系統依賴
yum groupinstall -y "Development Tools"
yum install -y openssl-devel cyrus-sasl-devel libevent-devel

# 或手動編譯 libevent 2.1.12 (系統版本過舊時)
cd /usr/local/src
wget https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz
tar xzf libevent-2.1.12-stable.tar.gz
cd libevent-2.1.12-stable
./configure --prefix=/usr/local/libevent
make -j$(nproc) && make install
echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf
ldconfig -v

💡 libevent ≥ 2.0 支援多線程、epoll/kqueue/IOCP 統一事件模型,Memcached 1.6 強依賴。

2. Memcached 源碼編譯優化


cd /usr/local/src
wget https://memcached.org/files/memcached-1.6.21.tar.gz
tar xzf memcached-1.6.21.tar.gz
cd memcached-1.6.21

# 編譯選項說明:
# --with-libevent=/usr/local/libevent  指定 libevent 路徑
# --enable-sasl                        支援 SASL 認證
# --enable-tls                         支援 TLS 加密 (需 OpenSSL)
# --disable-coverage                   關閉覆蓋率檢測 (生產必禁)
./configure \
    --prefix=/usr/local/memcached \
    --with-libevent=/usr/local/libevent \
    --enable-sasl \
    --enable-tls \
    --disable-coverage

make -j$(nproc)
make install

# 驗證
/usr/local/memcached/bin/memcached -V
# memcached 1.6.21

3. systemd 服務化與生產配置


# 建立用戶與目錄
useradd -r -s /sbin/nologin -d /var/lib/memcached memcached
mkdir -p /var/lib/memcached /var/log/memcached /var/run/memcached
chown -R memcached:memcached /var/lib/memcached /var/log/memcached /var/run/memcached

# 配置文件 /usr/local/memcached/etc/memcached.conf
cat > /usr/local/memcached/etc/memcached.conf <<'EOF'
# 監聽地址 (內網 + 本地)
-l 127.0.0.1,192.168.1.100
-p 11211
-U 0                    # 關閉 UDP (除非需要)
-u memcached
-m 2048                 # 最大內存 2GB (物理內存 50-70%)
-c 1024                 # 最大並發連接
-t 4                    # 工作線程數 (CPU 核心數)
-v                      # 啟動日誌
-vv                     # 客戶端操作日誌 (調試用,生產建議關閉)
-P /var/run/memcached/memcached.pid
# SASL 認證 (可選)
# -S
# -o sasl_conf_path=/usr/local/memcached/etc/sasl2/memcached.conf
EOF

# systemd 服務 /etc/systemd/system/memcached.service
cat > /etc/systemd/system/memcached.service <<'EOF'
[Unit]
Description=Memcached High-Performance Memory Object Cache
After=network.target

[Service]
Type=simple
User=memcached
Group=memcached
EnvironmentFile=-/usr/local/memcached/etc/memcached.conf
ExecStart=/usr/local/memcached/bin/memcached $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=3
LimitNOFILE=65535
PrivateTmp=yes
ProtectSystem=full
ProtectHome=yes
NoNewPrivileges=yes

[Install]
WantedBy=multi-user.target
EOF

# 環境變量文件 /etc/sysconfig/memcached (供 EnvironmentFile 讀取)
cat > /etc/sysconfig/memcached <<'EOF'
OPTIONS="-d -u memcached -l 127.0.0.1,192.168.1.100 -m 2048 -c 1024 -t 4 -P /var/run/memcached/memcached.pid"
EOF

systemctl daemon-reload
systemctl enable --now memcached
systemctl status memcached

4. PHP 擴展整合 (php-memcached vs php-memcache)

擴展PECL 包依賴特性推薦度
memcachedmemcachedlibmemcached + libevent完整協議支援、Binary Protocol、SASL、TLS、多實例、一致性哈希⭐⭐⭐⭐⭐ 生產首選
memcachememcache無額外庫舊協議、功能單一、不維護⭐ 僅兼容舊代碼

php-memcached 安裝 (PHP 7.4+ / 8.x)


# 安裝 libmemcached (php-memcached 依賴)
yum install -y libmemcached-devel cyrus-sasl-devel zlib-devel

# pecl 安裝 (自動編譯)
pecl install memcached
# 提示:libmemcached directory? [autodetect] → Enter
#       zlib directory? [autodetect] → Enter
#       use sasl? [yes] → yes
#       use session handler? [yes] → yes

# 或手動編譯指定版本
cd /usr/local/src
wget https://pecl.php.net/get/memcached-3.2.0.tgz
tar xzf memcached-3.2.0.tgz
cd memcached-3.2.0
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcached --enable-memcached-sasl --enable-memcached-session --with-libmemcached-dir=/usr/local
make -j$(nproc) && make install

# php.ini 加載
echo "extension=memcached.so" >> /usr/local/php/etc/php.ini
# session 驅動
echo "session.save_handler = memcached" >> /usr/local/php/etc/php.ini
echo "session.save_path = \"192.168.1.100:11211\"" >> /usr/local/memcached/etc/php.ini

# 重啟 php-fpm
systemctl restart php-fpm

5. 驗證與基準測試


# 基本操作
telnet 192.168.1.100 11211
set test_key 0 60 11
Hello Memcached
STORED
get test_key
VALUE test_key 0 11
Hello Memcached
END
quit

# PHP 測試
php -r '
$m = new Memcached();
$m->addServer("192.168.1.100", 11211);
$m->set("php_test", "Hello from PHP " . date("H:i:s"));
echo $m->get("php_test") . "\n";
'

# 基準壓測 (mcperf / memaslap)
yum install -y libmemcached-tools
memaslap -s 192.168.1.100:11211 -T 4 -c 100 -t 30s
# 關注:OPS > 100k/s、Latency p99 < 2ms

6. 調優參數速查

參數建議值說明
-m (最大內存)物理內存 50-70%超過觸發 LRU 淘汰
-c (最大連接)1024-65535配合 ulimit -n
-t (工作線程)CPU 核心數過多增加上下文切換
-I (項目大小限制)1m-128m (默認 1m)大值對象需調大
-f (塊增長因子)1.25 (默認)調整 slab 類別分佈
-n (最小分配空間)48 bytes (默認)小對象優化可調小
-L (大內存頁)啟用 (若系統支援)減少 TLB miss
-k (鎖定內存)啟用 (mlockall)防止 swap,需 CAP_IPC_LOCK

7. 監控指標


# 統計命令
echo "stats" | nc 192.168.1.100 11211
# 關鍵指標:
# curr_items, total_items, bytes, curr_connections, total_connections
# get_hits, get_misses, hit_rate = get_hits/(get_hits+get_misses)
# evictions (LRU 淘汰計數), reclaimed (過期回收)
# threads, conn_yields, auth_cmds, auth_errors

# Prometheus Exporter
docker run -d --name memcached_exporter -p 9150:9150 prometheuscommunity/memcached-exporter --memcached.address=192.168.1.100:11211

避坑清單

  • 🔴 綁定 0.0.0.0 無 SASL/TLS → 公開網絡未授權訪問 → -l 內網IP + -S (SASL) 或 --enable-tls
  • 🔴 libevent 版本過舊 (1.x) → 編譯報錯/運行崩潰 → 手動編譯 libevent 2.1+ 並 ldconfig
  • 🔴 PHP 用 memcache 而非 memcached → 缺乏 Binary Protocol、SASL、一致性哈希 → **強制遷移 php-memcached**
  • 🟡 內存碎片率高 → 調整 -f (1.125-1.25) 或啟用 -L 大頁
  • 🟡 連接數耗盡 → 檢查客戶端連接池配置、增大 -c、ulimit -n

總結

libevent 2.1+ + Memcached 1.6 編譯優化 → systemd 服務化 + 內存/線程調優 → php-memcached (Binary Protocol + SASL) → 監控命中率/淘汰率 = 高可用分佈式緩存基石。

按此標準落地,Memcached 從「簡單 K/V」進化為「可信賴的分佈式緩存層」。

Copyright © Windows教程網 All Rights Reserved