Install and configure Memcached source code in CentOS 6.6

Source: Internet
Author: User
Tags touch command install perl

Install and configure Memcached source code in CentOS 6.6

Host environment: CentOS 6.6 x86_64

1. Install Memcached

1. Because Memcached depends on the libevent library and uses it for Socket processing, we need to install the libevent library. Here I will use yum to install it.

Yum install libevent

2, download memcached source code package to the official website.

The latest version is v1.4.24.

# Wget

# Tar xf memcached-1.4.24.tar.gz

# Cd memcached-1.4.24

Compile and install

# Yum install cyrus-sasl-devel.x86_64

#./Configure -- prefix =/usr/local/memcached_1.4.24 -- enable-sasl

However, an error is reported.

The libevent library file is not found.


# Rpm-ql libevent

It was found that it was installed in the/usr/lib64 directory.

3. Continue to install the latest libevent.

Libevent official website is

Download the latest

# Wget

# Tar xf libevent-2.0.22-stable.tar.gz

# Cd libevent-2.0.22-stable

#./Configure -- prefix =/usr/local/libevent_2.0.22

# Make & make install

4. Continue to install memcached.

#./Configure -- prefix =/usr/local/memcached_1.4.24 -- enable-sasl -- with-libevent =/usr/local/libevent_2.0.22/

# Make & make install

5. Run memcached to check whether an error is reported.

Check the directory tree after installation.

There are very few files. The executable program only has one memcached in the bin directory. Therefore, you can only use it to start the memcached service. Let's take a look at the usage of the memcached command. Use memcached-h to view it.

6. memcached Parameters

-P: Specifies the tcp listening port. The default value is 11211.

-U: Specifies the UDP listening port. The default port is 11211.

-S: Specifies the unix socket path and uses unix socket to listen.

-A: Set the socket mask. The format is octal. The default value is 0700.

-L: Specifies the network address of the listener. The default value is

-D: Use memcached as the daemon and run it in the background.

-R: Maximum number of kernel files.

-U: Specifies the running identity of memcached.

-M: specifies the maximum memory size used to store cache entries. The unit is MB (64 MB by default)

-M: Never delete expired old data, but directly reports an error after the memory is exhausted.

-C: Limit the maximum number of connections at the same time (1024 by default)

-K: Lock all paging memory.

Note: This is a restricted value that limits the amount of memory you can lock. If you allocate an operation that exceeds the limit value, the Operation will fail. therefore, you must set the correct limits for the user process that starts memcached, instead of specifying the user to run the command using the-u parameter. in sh, this operation can also be completed using ulimit-S-l NUM_KB. this is a little dangerous for a large cache, so check the README document and the memcached homepage to view its configuration suggestions.

-V: verbose mode

-Vv: For more details ,,

-Vvv: the most detailed ,,

-H: help information.

-I: print the memcached and libevent licenses.

-P: Save the path and file name of the PID file. This parameter takes effect only when the-d option is used.

-F: Set the growth factor of the chunk size. The default value is 1.25.

-N: specify the minimum space allocated to key value flags. The default value is 48.

-L: Try to use a large memory page (if valid ). increasing the size of the Memory Page can reduce the number of TLB errors and improve the performance. to obtain a large memory page from the operating system, memcached allocates all cache entries to a large chunk. this mechanism is related to the operating system in use and is only valid for your operating system support.

-D: Use <char> as the delimiter of the key prefix and ID, and use it in status report. The default <char> is colon:. If this parameter is used to specify the delimiter, it will be automatically opened in status collection. If it is not set, you need to send the stats detail on command to the server to open it.

-T: Number of threads used. The default value is 4.

-R: this parameter is used to set a limit value and define the maximum value of each event request to protect the ability to process individual client requests, once a connection exceeds this value, memcached will try to process I/O through other connections. the default value is 20.

-C: Disable CAS

-B: set the limit for waiting (backlog) queues. The default value is 1024.

-B: Set the binding protocol-ascii, binary, or auto (default) is available ).

-I: set the size of each block (slab) page. The default size is 1 MB, the minimum size is 1 kb, and the maximum size is 128 MB, adjusting this value can change the cache entry size limit. note that this operation will also increase the number of slab (use-v to view) and use all memcached memory.

7. Start memcached.

Below are two ways to start memcached.


#./Memcached-u memcached-p 11211-m 64 m-vv

Method 2:

#./Memcached-d-m 10-u memcached-l 11211-c 50-P/tmp/memcached. pid-vv

The output content is as follows:

Slab class 1: chunk size 96 perslab 10922

Slab class 2: chunk size 120 perslab 8738

Slab class 3: chunk size 152 perslab 6898


Slab class 40: chunk size 616944 perslab 1

Slab class 41: chunk size 771184 perslab 1

Slab class 42: chunk size 1048576 perslab 1

<26 server listening (auto-negotiate)

<27 send buffer was 124928, now 268435456

<28 server listening (udp)

<30 server listening (udp)

<29 server listening (udp)

<27 server listening (udp)

The analysis is as follows:

Here, the slab is equivalent to the cache provided by memcached. The memory allocation unit of the memcached Server Heap is slab. The size of an slab is 1 MB by default, each slab is divided into several chunks, which are separated by a layer to the smallest unit, just like the disk or memory usage. Here, the chunk is the smallest unit, then we keep the items cached in these chunks, and the chunk size is the same. Therefore, the number of chunks that one or ten bytes may use is the same, that is to say, there is at least a waste.

In addition to the value of the cache entry, each chunk also contains struct, key.

The chunk growth in the figure is due to the default-f 1.25 growth factor.

Add a soft connection of the memcached executable program to the system PATH directory.


8. Other memcached tools

There is a scripts directory in the memcached source code package, which contains some built-in scripts and tools.

8.1, damemtop

This tool uses the perl module. If these modules have not been installed before, they can be used only after installation.

8.1.1 install perl-related modules

The following describes how to install the AnyEvent YAML TermReadKey module.

# Yum install perl-TermReadKey perl-YAML.noarch-y


YAML Error: Couldn't open/etc/damemtop. yaml for input: \ n


At./damemtop line 543.

An error is reported when the script is run. The/etc/damemtop. yaml file is required.

Copy damemtop. yaml In the scripts directory to the/etc/directory.

Analyze the configuration file damemtop. yaml

The format of this file uses YAML (short for YAML Ain't a Markup Language). Let's take a look at the configuration file content:

After necessary configuration, you can directly run damemtop.


8.2 memcached-sysv service script

As the name suggests, this file is a system service script.

# Cp memcached. sysv/etc/init. d/memcached

# Chkconfig -- add memcached

Check the service script. Because our memcached is a custom source code installation, pay attention to the Black font section in the script,

#! /Bin/sh


# Chkconfig:-55 45

# Description: The memcached daemon is a network memory cache service.

# Processname: memcached

# Config:/etc/sysconfig/memcached

# Source function library.

./Etc/rc. d/init. d/functions

PORT = 11211

USER = memcached

MAXCONN = 1024



If [-f/etc/sysconfig/memcached]; then



# Check that networking is up.

If ["$ NETWORKING" = "no"]


Exit 0



Prog = "memcached"

Start (){

Echo-n $ "Starting $ prog :"

# Insure that/var/run/memcached has proper permissions

Chown $ USER/var/run/memcached

Daemon memcached-d-p $ PORT-u $ USER-m $ CACHESIZE-c $ MAXCONN-P/var/run/memcached. pid $ OPTIONS



[$ RETVAL-eq 0] & touch/var/lock/subsys/memcached


Stop (){

Echo-n $ "Stopping $ prog :"

Killproc memcached



If [$ RETVAL-eq 0]; then


Rm-f/var/run/memcached. pid



Restart (){




# See how we were called.

Case "$1" in








Status memcached


Restart | reload)




[-F/var/lock/subsys/memcached] & restart |:



Echo $ "Usage: $0 {start | stop | status | restart | reload | condrestart }"

Exit 1


Exit $?


# Mkdir/var/run/memcached

# Chown memcached: memcached/var/run/memcached

# Service memcached restart // check whether the service can be restarted and started frequently.

Of course, as shown in the configuration file, the response parameters can be separately written to the/etc/sysconfig/memcached file, so that the memcached configuration file structure is clearer.

8.3, memcached-tool

Memcached-tool is a perl-Based Script. Make sure you have installed perl before using it.

#./Memcached-tool // run the tool directly to view the effect.

An example is provided after the script without parameters is executed.

Six formats: (there are only four formats before version 1.4.5. The new version adds settings and sizes, and the prompt information clearly indicates that the sizes command is a developer option, the actual production environment is not recommended)

Format 1: memcached-tool ip: port display

Format 2: memcached-tool ip: port

In this way, the same format 1 is a default method, and its default parameter is display.

Format 3: memcached-tool ip: port stats

Limit details, such as memcached version number, process pid Number, libevent version number, number of threads, write count, error count, total number of connections, current number of connections ,,,,,,

Format 4: memcached-tool ip: port dump

The dump parameter transfers the key-value cached objects in the memory.

Example: memcached-tool 11211 dump> memcached.txt

9 view the running status of the memcached Service

There are many commands available after telnet to the memcached server, such as add get set incr decr replace delete. In addition, there are also a series of commands for getting server information, these commands start with stats.

Memcached provides many commands that can be executed using the telnet tool. Of course, you can also run Memcached: getStats ($ cmd) provided by PHP.

Telnet command

[Root @ _ 212 scripts] # telnet 11211


Connected to

Escape character is '^]'.


STAT pid 23736 // Memcached process pid

STAT uptime 1495 // the time elapsed since the service was started, in seconds

STAT time 1442132353 // The current system time of the host where the server is located, in seconds

STAT version 1.4.24 // version of The Memcached Server

STAT libevent 2.0.22-stable // version of libevent on Memcached Server

STAT pointer_size 64 // the pointer size of the operating system of the host, which is generally a 32-or 64-32-bit system.

STAT rusage_user 0.018997 // user time used by the Process

STAT rusage_system 0.030995 // system time used by the Process

STAT curr_connections 10 // number of currently opened connections

STAT total_connections 36 // all connections

STAT connection_structures 11 // Number of connection structures allocated by the server

STAT reserved_fds 20 // Number of misc fds instances

STAT cmd_get 4 // total number of get commands executed

STAT cmd_set 2 // total number of set commands executed

STAT cmd_flush 0 // total number of flush commands executed

STAT __touch 0 // The number of touch executions. touch can refresh the expiration time.

STAT get_hits 3 // number of hits of the get command

STAT get_misses 1 // number of failed get commands

STAT delete_misses 0 // number of failures of the delete command

STAT delete_hits 0 // number of hits of the delete command

STAT incr_misses 0 // number of failures of the incr command

STAT incr_hits 0 // number of hits of the incr command

STAT decr_misses 0 // number of failures of the decr command

STAT decr_hits 0 // number of hits of the decr command

STAT cas_misses 0 // Number of cas command failures

STAT cas_hits 0 // number of hits of the cas command

STAT cas_badval 0 // number of times the bad data value is erased

STAT touch_hits 0 // number of hits of the touch command

STAT touch_misses 0 // number of failures of the touch command

STAT auth_cmds 0 // Number of authentication executions

STAT auth_errors 0 // number of failed authentication executions

STAT bytes_read 698 // number of bytes read by the server from the network

STAT bytes_written 15636 // number of bytes written by the server from the network

STAT limit_maxbytes 67108864 // maximum available memory limit, which is the-m value in the command line.

STAT accepting_conns 1 // number of currently accepted connections

STAT listen_disabled_num 0 //

STAT threads 4 // maximum number of available threads

STAT conn_yields 0 // The number of opened connections since memcached was started. The number of internal requests reaches the threshold specified by the-R parameter and the connection is passively abandoned.

STAT hash_power_level 16 // hashpower level. You can set $ memcached-o hashpower = 20 at startup.

STAT hash_bytes 524288 // memory usage in bytes

STAT hash_is_expanding 0 // whether the hash table is being expanded

STAT malloc_fails 0 //

STAT bytes 72 // number of bytes of all stored entries

STAT curr_items 1 // Number of cached items

STAT total_items 2 // total number of cache entries

STAT expired_unfetched 0 // item has not been touched before expiration, that is, the number of expiration times has not been updated after it is put in.

STAT evicted_unfetched 0 // The number of expiration times that have not been touched before the item is replaced.

STAT evictions 0 // Number of cached objects removed from the cache to free space for row data items

STAT reclaimed 0 // The number of times since memcached was started, the space for storing expired data is used.

STAT crawler_reclaimed 0 // Number of retrieved items

STAT crawler_items_checked 0 //

STAT lrutail_reflocked 0 //


Memcached installation and startup script

Performance problems of using Memcached in PHP

Install Memcached in Ubuntu and its command explanation

Install and apply Memcached

Use Nginx + Memcached's small image storage solution

Getting started with Memcached

For details about Memcached, click here
Memcached: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.