Install Memcached on Ubuntu and CentOS

How to Install Memcached on Ubuntu and CentOS

Memcached is a high-performance, distributed memory object caching system designed to speed up dynamic web applications by reducing database load.

It stores frequently accessed data in memory, allowing for faster retrieval.

Installing and using Memcached on Linux, whether on Ubuntu or CentOS, helps enhance website and application performance by caching data, reducing server strain, and improving load times.

  • Use sudo apt install memcached libmemcached-tools to install Memcached on Ubuntu.
  • Use sudo yum install memcached to install Memcached on CentOS.

Prerequisites to Install Memcached on Ubuntu and CentOS

Before installing Memcached on Ubuntu and CentOS, make sure your system is meeting below specifications:

  • A Linux VPS running a compatible Linux distribution (e.g., Ubuntu, Debian, CentOS).
  • A non-root user with sudo privileges.

Complete Steps to Install Memcached on Ubuntu

On Ubuntu, the installation is straightforward and involves installing the required packages, configuring settings, and ensuring the service runs properly.

Let’s go through the steps of this guide to learn how to install Memcached on Linux Ubuntu.

Step 1: Update System Packages

Before installation, it’s essential to ensure that your package list is up to date. To do this, run the command below:

sudo apt update

This command updates the list of available packages and their versions.

Step 2: Install Memcached and Libmemcached-tools on Ubuntu

After updating the system, proceed to install Memcached along with libmemcached-tools for management purposes. To do this, run:

sudo apt install memcached libmemcached-tools
  • memcached: This installs the Memcached daemon.
  • libmemcached-tools: A collection of useful tools to interact with Memcached, including utilities for debugging and management.

Step 3: Configure Memcached on Ubuntu

After installing Memcached, you might want to adjust its configuration to suit your server’s needs.

The configuration file is located at /etc/memcached.conf. You can edit it using a text editor:

sudo nano /etc/memcached.conf

Some common settings to adjust:

  • -m: Maximum memory usage (default is 64MB).
  • -l: IP address to listen on (by default, it’s bound to 127.0.0.1).

Step 4: Start and Enable Memcached on Ubuntu

Once the configuration is complete, start Memcached and enable it to start on boot:

sudo systemctl start memcached
sudo systemctl enable memcached
  • systemctl start: Starts the Memcached service immediately.
  • systemctl enable: Configures Memcached to start automatically at system boot.

Step 5: Verify Ubuntu Memcached Installation

Check if Memcached is running properly using the status command:

sudo systemctl status memcached

You should see the output indicating that Memcached is active and running.

  • Additionally, you can check Memcached using telnet to confirm it’s listening on the correct port (11211):
telnet 127.0.0.1 11211
  • Type stats after connecting via telnet to view performance data and verify functionality.

Step 6: Check Memcached Version on Ubuntu

To check the Memcached version on Ubuntu, run:

memcached -V

This should display the installed Memcached version to finish the Memcached installation guide for Ubuntu.

How to Install Memcached on Linux CentOS?

Installing Memcached on CentOS involves downloading the required packages, configuring the service, and verifying that it’s running properly.

Step 1: Install EPEL Repository (If Needed)

Some CentOS versions may require the EPEL (Extra Packages for Enterprise Linux) repository to access the Memcached package. Install EPEL using the below command:

sudo yum install epel-release

This ensures that the Memcached package is available to be installed.

Step 2: Install Memcached on CentOS

Once the EPEL repository is in place, you can install Memcached using the package manager:

sudo yum install memcached

This installs Memcached on your CentOS system, allowing it to cache data in memory to improve application performance.

Step 3: Configure Memcached on CentOS

Memcached’s configuration file on CentOS is located at /etc/sysconfig/memcached.

Use any text editor, such as nano, to edit the file:

sudo nano /etc/sysconfig/memcached

Common configurations to modify:

  • PORT: The port on which Memcached listens (default is 11211).
  • CACHESIZE: The amount of memory (in MB) that Memcached will use.
  • OPTIONS: Additional options, such as IP binding.

Step 4: Start and Enable CentOS Memcached

Start the Memcached service and enable it to start at boot time using the following commands:

sudo systemctl start memcached
sudo systemctl enable memcached

Just like with Ubuntu, starting and enabling Memcached ensures it will run automatically on system reboots.

Step 5: Verify Memcached Installation on CentOS

Check the status of Memcached to ensure it’s running correctly:

sudo systemctl status memcached

You should see the output indicating that Memcached is active and running.

  • You can also test Memcached using telnet to see if it’s responding on port 11211:
telnet 127.0.0.1 11211
  • Once connected, use the stats command to verify that Memcached is operational.

Step 6: Check CentOS Memcached Version

Use the following command to view the current version of Memcached you have installed:

memcached -V

This command will show the version of Memcached installed on your CentOS system to complete Memcached installation tutorial for CentOS.

How to Use Memcached on Linux Ubuntu & CentOS?

Once you have installed and configured Memcached on your Linux distribution, you can interact with it using various client libraries or tools.

Below are steps for common tasks, including basic commands and client interactions.

While you tested whether Memcached is running on Step #5, we assume that Memcached is active and running. So, let’s learn and use Memcached commands:

  • Basic Memcached Commands

Here are some basic commands you can use once you are connected to Memcached via telnet:

Store a value:

set mykey 0 3600 11
Hello opera
  • set: Command to store data.
  • mykey: The key you are using to store the value.
  • 0: Flags (used for data serialization).
  • 3600: Expiration time (in seconds; 3600 seconds = 1 hour).
  • 11: Length of the data you are storing.
  • Hello opera: The data you are storing.

Retrieve a value:

get mykey

This command retrieves the value associated with mykey. You should see:

VALUE mykey 0 11
Hello opera

Delete a value:

delete mykey

This command removes the value associated with mykey.

Check statistics:

stats

This command returns various statistics about the Memcached instance, such as the number of items stored, memory usage, and hit/miss rates.

Using Memcached with Programming Languages

You can also use Memcached in your applications using client libraries available for various programming languages. Here are a few examples:

Memcached for PHP:

Install the Memcached extension running the commands below:

On Ubuntu

sudo apt install php-memcached

On CentOS

sudo yum install php-memcached

Example usage in PHP:

<?php
$mem = new Memcached();
$mem->addServer('127.0.0.1', 11211);

// Store a value
$mem->set('mykey', 'Hello opera', 3600);

// Retrieve the value
$value = $mem->get('mykey');
echo $value; // Outputs: Hello opera
?>

Memcached for Python:

Use the following command to install the pymemcache library:

pip install pymemcache

Example usage in Python:

from pymemcache.client import base

client = base.Client(('127.0.0.1', 11211))

# Store a value
client.set('mykey', 'Hello opera', expire=3600)

# Retrieve the value
value = client.get('mykey')
print(value.decode('utf-8'))  # Outputs: Hello opera

Memcached for Node.js:

To install the memcached package, run:

npm install memcached

Example usage in Node.js:

const Memcached = require('memcached');
const memcached = new Memcached('127.0.0.1:11211');

// Store a value
memcached.set('mykey', 'Hello World', 3600, (err) => {
    if (err) throw err;

    // Retrieve the value
    memcached.get('mykey', (err, data) => {
        if (err) throw err;
        console.log(data); // Outputs: Hello World
    });
});

Monitoring and Managing Memcached

You can monitor and manage your Memcached server through the stats command in telnet, which provides information about memory usage, cache hits and misses, and more.

You can also consider using monitoring tools like:

  • Munin: A networked resource monitoring tool that can monitor Memcached along with other system resources.
  • Prometheus: With exporters for monitoring Memcached, you can track performance metrics over time.

That’s it! Whether you are connecting via telnet or using a client library in your application, the flexibility and ease of use of Memcached make it a valuable asset in web application development.

What ports does Memcached use?

By default, Memcached listens on port 11211. You can change this in the configuration file if needed.

Why Memcached does not start after installation?

If Memcached fails to start, check the logs for error messages:

journalctl -u memcached

Common issues include incorrect configurations or insufficient memory settings. Make sure the configuration file is set correctly and that your server has enough resources.

Why Memcached is not connecting using telnet?

If you cannot connect, check the following:

  • Ensure Memcached is running (sudo systemctl status memcached).
  • Check the firewall settings to ensure that port 11211 is open.
  • Make sure that Memcached is bound to the correct IP address in the configuration file (-l option).

How to solve “No such key” error when trying to retrieve data?

This message means that the key you are trying to retrieve does not exist. Ensure that you have set the key correctly and that it hasn’t expired (check the expiration time when setting the key).

How to increase the memory limit for Memcached?

You can increase the memory limit by modifying the -m option in the configuration file (/etc/memcached.conf for Ubuntu or /etc/sysconfig/memcached for CentOS).

For example, to set it to 128MB:

-m 128

After making changes, restart Memcached:

sudo systemctl restart memcached

How to secure Memcached installation?

To secure Memcached:

  • Bind it to localhost or specific IPs using the -l option in the configuration.
  • Implement firewall rules to restrict access to the Memcached port (11211).
  • Consider using SASL (Simple Authentication and Security Layer) for authentication.

Conclusion

Memcached is a powerful tool for optimizing the performance of web applications by reducing database load and speeding up data retrieval.

By following the steps outlined above, you can easily install and configure Memcached on both Ubuntu and CentOS systems.

After installing Memcached on Ubuntu and CentOS, consider:

  • Monitoring Memcached performance metrics and logs regularly to ensure it operates efficiently and adjust configurations as needed.
  • Also, fine-tune memory allocation and other settings in the configuration file to suit your application’s needs as usage patterns evolve.

Leave a Reply

Your email address will not be published. Required fields are marked.