متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 90Memcached · Data

Install and set up Memcached

Memcached is a simple, very fast memory cache: apps store the results of slow work (database queries, rendered pages, API answers, sessions) under a key and read them back in microseconds. It has no persistence and no complex data types — and that makes it light and predictable. Many PHP apps (WordPress, Magento, Drupal) and big websites use it. This guide installs Memcached on RHEL and Ubuntu, sets memory and connection limits, keeps it local and safe (an open Memcached was used for record DDoS attacks), connects PHP, and shows how to read its stats.

  • 10 الدروس
  • 7 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

90.1

Know it before you install

Memcached keeps key → value pairs in RAM up to a size limit. When memory is full, it removes the least recently used items (eviction). Data is lost on restart — it is a cache, never the only copy. Clients spread keys over several servers themselves (consistent hashing).

ItemRHEL / Rocky / AlmaUbuntu / Debian
Packagememcached (+ libmemcached tools)memcached (+ libmemcached-tools)
Servicememcached.servicethe same
Config/etc/sysconfig/memcached/etc/memcached.conf
Port11211/tcp (UDP must be off)the same
Toolsmemcached-tool, memcstatthe same

Memcached or Redis? Memcached: the simplest pure cache, multi-threaded, no persistence. Redis/Valkey: data types, persistence, queues, replication. If you need more than a cache, choose Redis.

90.2

Install on RHEL, Rocky, Alma and Fedora

dnf -y install memcached libmemcached-awesome-tools

الأوامر في هذا الدرس

90.3

Install on Ubuntu and Debian

apt-get update
apt-get install -y memcached libmemcached-tools

90.4

First configuration: memory, connections, listen only locally

On RHEL:

/etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="2048"
CACHESIZE="1024"
OPTIONS="-l 127.0.0.1,::1 -U 0 -t 4"

On Ubuntu, the same in /etc/memcached.conf (one option per line):

/etc/memcached.conf
-d
-m 1024
-p 11211
-u memcache
-l 127.0.0.1
-c 2048
-U 0
-t 4
logfile /var/log/memcached.log
OptionWhy
CACHESIZE / -m 1024Max memory in MB for items (here 1 GB). Leave RAM for the OS and apps.
-l 127.0.0.1Listen only on localhost (or a private IP when app servers are elsewhere).
-U 0UDP off. Open UDP Memcached servers were used for 1+ Tbps amplification DDoS attacks.
MAXCONN / -cEnough connections for all app workers.
-t 4Worker threads (about the number of CPU cores used for it).
systemctl enable --now memcached
systemctl restart memcached
ss -lntup | grep 11211
tcp   LISTEN 0  1024  127.0.0.1:11211  0.0.0.0:*  users:(("memcached",pid=1820,fd=26))

Only TCP on 127.0.0.1 — that is what you want. If app servers on other hosts need it, listen on the private IP and allow only them:

firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.1.0/24" port port="11211" protocol="tcp" accept'
firewall-cmd --reload

For untrusted networks, Memcached supports SASL authentication (-S) and TLS (-Z, when built with TLS) — or keep it on localhost and avoid the problem.

90.5

Connect PHP (sessions and app cache)

dnf -y install php-pecl-memcached
apt-get install -y php-memcached
/etc/php.d/50-memcached-sessions.ini
session.save_handler = memcached
session.save_path = "127.0.0.1:11211"

(Ubuntu: /etc/php/8.3/fpm/conf.d/.) Restart PHP-FPM. For WordPress, a Memcached object-cache plugin uses the same server. With several web servers and sessions in Memcached, users stay logged in on every server — but sessions are lost when Memcached restarts (use Redis if that matters).

الأوامر في هذا الدرس

90.6

Daily use: the commands and the real uses

memcached-tool 127.0.0.1:11211 stats | grep -E 'curr_items|get_hits|get_misses|evictions|bytes |limit_maxbytes|curr_connections'
memcached-tool 127.0.0.1:11211 display
memcstat --servers=127.0.0.1
printf 'stats\r\nquit\r\n' | nc 127.0.0.1 11211 | grep -E 'get_hits|get_misses'
printf 'flush_all\r\nquit\r\n' | nc 127.0.0.1 11211

The numbers that matter:

  • Hit ratio = get_hits / (get_hits + get_misses) — 90 %+ is good for most apps.
  • evictions growing fast = the cache is too small (raise -m) or keys live too long.
  • curr_connections near MAXCONN = raise the limit or fix connection leaks.

flush_all empties the whole cache — the database gets all the load at once; do it only when needed.

Real uses at work:

  • Cache database results and rendered fragments in PHP/Python/Java apps.
  • Shared sessions for a few web servers (when losing them on restart is OK).
  • WordPress / WooCommerce / Magento object cache.

90.7

Make it safe

Security checklist:

  • Listen on 127.0.0.1 (or a private IP with firewall rules) — never public.
  • UDP off (-U 0).
  • No secrets or personal data in the cache unless the network is fully private.
  • SASL or TLS if it must cross untrusted networks.
  • Monitor evictions, hit ratio and connections (Prometheus memcached_exporter).
  • Keep the package updated.

90.8

Update and remove

dnf -y upgrade memcached
apt-get install --only-upgrade -y memcached
systemctl restart memcached
dnf -y remove memcached

A restart empties the cache — plan it at a quiet time.

90.9

Fix common problems

ProblemCauseFix
App can not connect from another serverListens on localhost only, or firewall.Add the private IP to -l; open 11211/tcp for the app subnet.
Many evictions and low hit ratioCache too small or TTLs too long.Raise memory (-m); shorter TTLs; cache only what helps.
SERVER_ERROR out of memory storing objectAn item larger than the max item size (1 MB default).Store smaller items, or raise -I (for example -I 2m) carefully.
Users logged out after a restartSessions were only in Memcached.Expected; use Redis with persistence for important sessions.
Service will not start after editingA wrong option line.journalctl -u memcached; on Ubuntu one option per line in memcached.conf.
A security scan reports UDP 11211 openUDP was not turned off.Add -U 0 and restart.

90.10

Next steps

1Why must UDP be off on Memcached?

Open UDP Memcached servers were abused for huge DDoS amplification attacks.

2What does a fast-growing evictions counter tell you?

The cache is too small (or keys live too long), so useful items are thrown out.

كل الأوامر في هذا الفصل