Linux

How to Install Memcached on Ubuntu 22.04

In this tutorial, we will learn how to quickly install and configure the Memcached database in Ubuntu 22.04.
Captain Salem 4 min read
How to Install Memcached on Ubuntu 22.04

Memcached is a high-performance, distributed, in-memory caching system designed to accelerate data retrieval by storing frequently accessed data in RAM (Random Access Memory).

It operates as a key-value store, where data is associated with unique keys and can be quickly fetched or updated. Memcached uses a client-server architecture, with multiple clients sending requests to a central server or a cluster of servers.

Installing Memcached

Luckily, Memcached packages are available by default in recent versions of Ubuntu, hence, we can use the default repositories to install it.

Start by updating the package list with the command:

sudo apt update

Install Memcached packages with the command

sudo apt install memcached libmemcached-tools

The above command will install thememcached and memcached-tools on your Ubuntu system. The tools will allow you to interact with Memcached server from the terminal.

Start Memcached

Once installed, we can start the Memcached service with the command:

sudo systemctl start memcached

Output:

● memcached.service - memcached daemon
     Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2023-09-21 14:39:10 EAT; 24s ago
       Docs: man:memcached(1)
   Main PID: 4183 (memcached)
      Tasks: 10 (limit: 2236)
     Memory: 2.1M
        CPU: 31ms
     CGroup: /system.slice/memcached.service
             └─4183 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

Sep 21 14:39:10 ubuntu-linux-22-04-desktop systemd[1]: Started memcached daemon.

In this case, we can see that the Memcached instance is running on the Ubuntu system.

Configuring Memcached

Once we have installed Memcached, we can perform some basic configurations which will allow us to work with the Memcached database more efficiently.

To configure Memcached, we ned to edit the configuration file located in /etc/memcached.conf file.

Enable Remote Access

Like most databases, Memcached will only listen and accept connections from the local machine. To enable remote access, we can edit the configuration and allow remote access from all address

sudo vim /etc/memcached.conf

Locate the directive that begins with -l 127.0.0.1 and replace 127.0.0.1 with the server address 0.0.0.0 as shown:

-l 0.0.0.01

NOTE: Setting Memcached to listen on all interfaces can lead to severe security issues such as denial of service attack. We recommend setting the specific IP from which you wish to access the Memcached server. Ensure this is under a firewall.

Once configured, you can restart Memcached server:

sudo systemctl restart memcached

Connecting and Testing Memcached - Telnet

To test Memcached server, we can login into the server by using telnet as shown in the command:

telnet localhost 11211

This should attempt to connect to the Memcached database and return an output as shown:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

As you can see, we are now connected to the Memcached database.

Store a Value in Memcached

To store a value in Memcached, we use the set command. The syntax is as shown:

set <key> <flags> <exptime> <bytes>
<value>
  • <flags> - User-defined metadata for the value.
  • <exptime>- The expiration time in seconds.
  • <bytes>- The length of the data in bytes.
  • <value>- The data to be stored.

Example

set my_key 0 3600 5
hello

Retrieve Value in Memcached

To retrieve a value from Memcached, we use the get command as shown in the example syntax below

get <key>

For example:

get my_key

If the key exists, Memcached will return the stored value.

Delete Value in Memcached

To delete a key from Memcached, we use the delete command:

delete <key>

For example:

delete my_key

Check the Server Status

To check the server status and statistics, we can use the stats command:

stats

Output:

STAT pid 4183
STAT uptime 845
STAT time 1695297193
STAT version 1.6.14
STAT libevent 2.1.12-stable
STAT pointer_size 64
STAT rusage_user 0.184631
STAT rusage_system 0.157479
STAT max_connections 1024
STAT curr_connections 1
STAT total_connections 3
STAT rejected_connections 0
STAT connection_structures 2
STAT response_obj_oom 0
STAT response_obj_count 1
STAT response_obj_bytes 32768
STAT read_buf_count 4
STAT read_buf_bytes 65536
STAT read_buf_bytes_free 16384
STAT read_buf_oom 0
STAT reserved_fds 20
STAT cmd_get 1
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT cmd_meta 0
STAT get_hits 0
STAT get_misses 1
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 store_too_large 0
STAT store_no_memory 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 105
STAT bytes_written 2269
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 5
STAT lru_maintainer_juggles 894
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 log_watchers 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

Close Connection

To close the Telnet connection, use the quit command:

quit

Conclusion

In this tutorial, we covered all the fundamentals of installing and configuring Memcached on Ubuntu. We also covered how to connect to the Memcached server, create, get, and delete values from the database.

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.