Introducing Dragonfly Cloud! Learn More

Question: How to set up Memcached?

Answer

To set up Memcached, you'll need to follow these steps:

  1. Install Memcached: You can install Memcached on your system using a package manager or by downloading and compiling the source code. Below are the instructions for installing Memcached via package manager for some common operating systems:

    • Ubuntu/Debian: sudo apt-get install memcached
    • CentOS/Fedora: sudo yum install memcached
    • macOS: brew install memcached
  2. Configure Memcached: Once installed, you can configure Memcached by modifying its configuration file /etc/memcached.conf (for Linux) or /usr/local/etc/memcached.conf (for macOS).

    By default, Memcached listens on port 11211 and uses all available network interfaces. You can modify these settings as per your requirement.

    For example, if you want Memcached to listen only on the local interface (i.e., localhost), you can add the following line to the configuration file: -l 127.0.0.1.

  3. Start Memcached: After configuring Memcached, you can start it using the command memcached. If you have modified the configuration file, you can specify the path to the configuration file using the -d option.

    For example, to start Memcached with the configuration file /etc/memcached.conf, you can use the following command: memcached -d -u memcached -l 127.0.0.1 -p 11211 -m 64.

    This command starts Memcached as a daemon process with the user memcached, listens only on the localhost interface, uses port 11211, and allocates a maximum of 64 MB of memory for storing cached data.

  4. Test Memcached: To test if Memcached is running correctly, you can use the telnet command to connect to the Memcached server and set/get some sample key-value pairs.

    Here's an example of setting a key-value pair in Memcached using telnet:

    $ telnet localhost 11211
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    set mykey 0 100 5
    hello
    STORED
    

    In the above example, we connected to the Memcached server running on the localhost interface at port 11211 and set a key-value pair with key "mykey", value "hello", and an expiry time of 100 seconds.

That's it! You've set up Memcached on your system and tested it successfully.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.