To set up Memcached, you'll need to follow these steps:
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:
sudo apt-get install memcached
sudo yum install memcached
brew install 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:
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
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.
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.