Memcached is used to speed up dynamic database-driven websites by caching data and objects in RAM. This reduces the number of times an external data source must be read, which lowers overheads and speeds up response times. The memory caching software is a free, open-source project that anyone can use.
At the end of the tutorial, you will know how to install and configure Memcached on your openSUSE Leap 15 operating system.
Table of Contents
Prerequisites
- Recommended OS: openSUSE Leap – 15.x
- User account: A user account with sudo or root access.
Update Operating System
Update your openSUSE operating system to make sure all existing packages are up to date:
sudo zypper refresh
The tutorial will be using the sudo command and assuming you have sudo status.
To verify sudo status on your account:
sudo whoami
Example output showing sudo status:
[joshua@opensuse ~]$ sudo whoami
root
To set up an existing or new sudo account, visit our tutorial on adding a User to Sudoers on openSUSE.
To use the root account, use the following command with the root password to log in.
su
Install Memcached
By default, openSUSE has Memcached in its repositories. To install Memcached, enter the following command:
sudo zypper install memcached libmemcached
Type Y, then press ENTER KEY to proceed with the installation.
Note, the second install option, the “libmemcached” package, provides additional commands to bring extra interaction and execute abilities to Memcached.
Next, verify Memcached was installed correctly by verifying its version and build
sudo memcached --version
Example output:
memcached 1.5.6
The service, by default, will not be active. To start Memcached using the following command:
sudo systemctl enable memcached --now
Memcached should be activated by default. To verify this, use the systemctl status command as follows:
systemctl status memcached
Other useful commands to managing the Memcached service are as follows:
To enable the Memcached service on system boot:
sudo systemctl enable memcached
To stop the Memcached service:
sudo systemctl stop memcached
To disable the Memcached service on system boot:
sudo systemctl disable memcached
To restart the Memcached service:
sudo systemctl restart memcached
Configure Memcached
Memcached must be listening to 127.0.0.1. Open the default setting in the configuration file located at “/etc/sysconfig/memcached“.
sudo nano /etc/sysconfig/memcached
The first option is to change the traditional port number for Memcached.
First, scroll the MEMCACHED_PARAMS line:
MEMCACHED_PARAMS=" -l 127.0.0.1"
Currently, Memcached is set to listen to localhost, and you can set an internal or external IP address if required by changing 127.0.0.1.
Example:
MEMCACHED_PARAMS=" -l 192.51.5.231"
Next, in the same line, you can add additional flags to change the settings. One of the most popular settings to modify is the default size of 64MB to something more significant if you have a powerful server.
Example changing RAM to 2GB from 64MB:
MEMCACHED_PARAMS=" -l 192.51.5.231 m - 2000"
Another popular option is to disable UDP if not required.
To do this, use the following:
MEMCACHED_PARAMS=" -l 192.51.5.231 m - 2000 - U 0"
An overview of the tutorial to what your setup may look like once finished.
Example only:
Now save the file CTRL+O and hit “Y,” then CTRL+X to exit and restart your Memcached instance.
sudo systemctl restart memcached
Configure Firewalld for Memcached
By default, no rules are set up for Memcached. This means you will need to create allow rules which is essential to stop attacks. Failure to secure Memcached will lead to issues down the track, so do not skip this unless you have other means to protect your installation.
First, add a new dedicated zone for Memcached firewalld policy:
sudo firewall-cmd --permanent --new-zone=memcached
Next, specify the allowed IP addresses that are permitted to access the Memcached.
sudo firewall-cmd --permanent --zone=memcached --add-source=1.2.3.4
Replace 1.2.3.4 with the IP address that will be added to the allow list.
Once you have finished adding the IP addresses, open the port of the Memcached.
By default, this is TCP port 11211.
sudo firewall-cmd --permanent --zone=memcached --add-port=11211/tcp
Note, you can change the default port in your configuration file if you change the firewall port open rule above to the new value.
After running those commands, reload the firewall to implement the new rules:
sudo firewall-cmd --reload
Example output if successful:
success
Install Memcached PHP Libraries
Memcached comes with various extensions for the programming languages, but it is primarily used for the PHP. However, openSUSE needs some additional repositories installed to pull these packages as they do not feature on the default repository.
Import server:php:extensions for openSUSE Leap 15.3:
sudo zypper addrepo https://download.opensuse.org/repositories/server:php:extensions/openSUSE_Leap_15.3/server:php:extensions.repo
Import server:php:extensions for openSUSE Leap 15.2:
sudo zypper addrepo https://download.opensuse.org/repositories/server:php:extensions/openSUSE_Leap_15.2/server:php:extensions.repo
Once done, refresh the repository.
sudo zypper refresh
Next, install Memcached PHP support using the following command.
PHP 7.4 example:
sudo zypper install php7-memcached php7-memcached
PHP 8.0 example:
sudo zypper install php8-memcached php8-memcached
Type Y, then press ENTER KEY to proceed with the installation.
Additionally, for Memcached support with Python, you can install the following packages using the PIP library.
pip3 install pymemcache --user
pip3 install python-memcached --user
Accessing Memcached from Command Line
Memcached stats can be gathered by much additional software and WEB UI’s that it works in conjunction with. However, a better way to check is to interact with Memcached directly using the command line.
First, TELNET into your service:
telnet localhost 11211
Example output:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Note, if Telnet is not installed, run the following command to install.
sudo zypper install telnet
TELNET should never be allowed to communicate to a public network and restricted in private network environments.
Next, you can get an overview of your Memcached service by using the following command:
stats
Example output:
STAT pid 5934
STAT uptime 1275
STAT time 1631930242
STAT version 1.6.9
STAT libevent 2.1.12-stable
STAT pointer_size 64
STAT rusage_user 0.113635
STAT rusage_system 0.000000
STAT max_connections 1024
STAT curr_connections 1
STAT total_connections 2
STAT rejected_connections 0
STAT connection_structures 2
STAT response_obj_oom 0
STAT response_obj_count 1
STAT response_obj_bytes 16384
STAT read_buf_count 2
STAT read_buf_bytes 32768
STAT read_buf_bytes_free 0
STAT read_buf_oom 0
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT cmd_meta 0
STAT get_hits 0
STAT get_misses 0
STAT get_expired 0
STAT get_flushed 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 7
STAT bytes_written 0
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT time_in_listen_disabled_us 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT slab_reassign_rescues 0
STAT slab_reassign_chunk_rescues 0
STAT slab_reassign_evictions_nomem 0
STAT slab_reassign_inline_reclaim 0
STAT slab_reassign_busy_items 0
STAT slab_reassign_busy_deletes 0
STAT slab_reassign_running 0
STAT slabs_moved 0
STAT lru_crawler_running 0
STAT lru_crawler_starts 6
STAT lru_maintainer_juggles 1325
STAT malloc_fails 0
STAT log_worker_dropped 0
STAT log_worker_written 0
STAT log_watcher_skipped 0
STAT log_watcher_sent 0
STAT unexpected_napi_ids 0
STAT round_robin_fallback 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT slab_global_page_pool 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evicted_active 0
STAT evictions 0
STAT reclaimed 0
STAT crawler_reclaimed 0
STAT crawler_items_checked 0
STAT lrutail_reflocked 0
STAT moves_to_cold 0
STAT moves_to_warm 0
STAT moves_within_lru 0
STAT direct_reclaims 0
STAT lru_bumps_dropped 0
END
As above, this shows some crucial things you may want to see, such as uptime, number of items in the cache, and the number of client connections to the instance
You can refine the search by looking into the different Memcached slabs (partitions) of memory to return results.
Examples below:
List the slabs in the instance connected:
stats slabs
List of slabs which includes a count of the items stored within each slab:
stats items
Next, you can access and delete data using the cachedump command to list the keys.
First, execute the cachedump command:
stats cachedump [slab ID] [number of items, 0 for all items]
Example in action:
stats cachedump 1 0
Example output:
ITEM testkey [9 b; 1296857316 s]
END
As above, SLAB 1 has one item with the key “testkey.” To get the actual value, you can use the “get key” command as follows:
get testkey
Example output:
VALUE testkey 0 9
test data
END
Lastly, to delete a cached item, in this case, the “testkey” use the following command:
delete testkey
Example output:
DELETED
How to Remove (Uninstall) Memcached
To remove Memcached, use the following command in your terminal.
sudo zypper remove memcached libmemcached
This will remove Memcached from your system immediately.
Comments and Conclusion
The tutorial has shown you how to install Memcached on your openSUSE Leap 15 operating system, install the additional libraries and how to access the terminal command list.
For further documentation, the project’s Github Wiki page explains further in great detail for server admins any specifications or information they require.