Memcached is a free and open source high-performance memory key-value database. It is mainly used for system caching to improve the response speed of the application by caching the results in the database.
In this guide, we will explain how to install and configure Memcached on CentOS 8.
The Memcached package is included in the default CentOS 7 software source. The installation is very simple. Enter the following command as root or another user with sudo privileges:
sudo dnf install memcached libmemcached
The libmemcached
package provides some command-line tools for managing Memcached servers.
Once the installation is complete, start and enable the Memcached service.
sudo systemctl enable memcached --now
To verify that memcached is running, enter:
sudo systemctl status memcached
The output is similar to the following:
● memcached.service - memcached daemon
Loaded:loaded(/usr/lib/systemd/system/memcached.service; enabled; vendor preset: disabled)
Active:active(running) since Mon 2020-04-1320:12:52 UTC; 2s ago
...
that's it. You have installed Memcached on your CentOS 8 system and you can start using it.
Memcached can be configured by editing the /etc/sysconfig/memcached
file. By default, Memcached listens to all network interfaces.
If the client connecting to the server is also running on the same server, you do not need to make any changes.
If the application you connect to Memcached is hosted on a remote server, you need to configure your firewall and allow access to Memcached port `11211 from the client IP address.
When improperly configured, Memcached can be used to perform DDOS attacks.
The following example assumes that you want to connect to the Memcached server on the same LAN. The Memcached server IP is 192.168.100.20
, and the client's IP address is 192.168.100.30
.
The first step is to edit the Memcached configuration file and set the service to monitor the LAN interface:
Open the memcached
configuration file:
sudo nano /etc/sysconfig/memcached
In the OPTIONS
parameter, add the server IP address -l 192.168.100.20
. This informs Memcached to only listen on the specified interface.
OPTIONS="-l 192.168.100.20"
Save the file and restart the Memcached service to make the application take effect:
sudo systemctl restart memcached
Once the service is configured, the next step is to open the memcached port on your firewall.
The CentOS firewall configuration tool is FirewallD
. This command will create a new zone with the name memcached
, open port 11211
and only allow access from the client IP address.
sudo firewall-cmd --new-zone=memcached --permanent
sudo firewall-cmd --zone=memcached --add-port=11211/udp --permanent
sudo firewall-cmd --zone=memcached --add-port=11211/tcp --permanent
sudo firewall-cmd --zone=memcached --add-source=192.168.100.30/32--permanent
sudo firewall-cmd --reload
To connect to the Memcached server, you need to use a client in a specific language.
To use Memcached as your PHP application such as Wordpress, Drupal or Magento, you need to install the php-pecl-memcached
:
sudo dnf install php-pecl-memcache
There are some libraries on Python that can be used to interact with memcache, you can use pip to install your favorite libraries:
pip install pymemcache
pip install python-memcached
We have shown you how to install Memcached on CentOS 8. For more information on this topic, read Memcached Wiki.
Recommended Posts