Question: How to set up Memcached?
Answer
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:
- Ubuntu/Debian:
sudo apt-get install memcached
- CentOS/Fedora:
sudo yum install memcached
- macOS:
brew install memcached
- 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
.
- Start Memcached:
After configuring Memcached, you can start it using the commandmemcached
. 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.
- Test Memcached:
To test if Memcached is running correctly, you can use thetelnet
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?
Other Common Memcached Questions (and Answers)
- Does Memcached expire?
- Does Memcached write to disk?
- What is the location of Memcached error log?
- How does Memcached consistent hashing work?
- Does restarting Memcached clear cache?
- Does Memcached support encryption?
- How to pronounce Memcached?
- How to check Memcached version in Linux?
- How to install Memcached in CentOS 7?
- How is Memcached implemented?
- How to clear Memcached data?
- How does Memcached work in PHP?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost