Ubuntu memcached Installation and configuration

Source: Internet
Author: User
Tags crc32 memcached memory usage
 ubuntu memcached Installation and Configuration

It is divided into two processes: Memcache server-side installation and memcached client installation.

The so-called server-side installation is to install Memcache on the server (generally Linux system) to implement data storage

The so-called client installation refers to PHP (or other programs, memcache and other good API interface) to use the server-side memcache provided functions, PHP needs to add extensions.

1) Install Memcache server

sudo apt-get install memcached

After installing the Memcache server, we need to start the service:

memcached-d-M 128-p 11111-u root

Here you need to explain the startup parameters of the Memcached service:

-P Listening Port

-L connected IP address, default is native

-D Start memcached service

-D Restart Restart memcached service

-D Stop|shutdown Close the running memcached service

-D Install memcached service

-d Uninstall Uninstall memcached service

-U Run as (only valid when running as root)

-m maximum memory usage, in megabytes. Default 64MB

-M running out of memory and returning an error instead of deleting an item
-c Maximum number of simultaneous connections, default is 1024

-F Block size growth factor, default is 1.25-n minimum allocation space, key+value+flags default is 48
-H Display Help

2) Installing the Memcache client (PHP5 as an example)

sudo apt-get install Php5-memcache

After installation we need to make a simple configuration in php.ini, open the/etc/php5/apache2/php.ini file at the end to add the following:

[Memcache]

; Whether to transparently fail over to the other server when an error is encountered.

Memcache.allow_failover = On

; The maximum number of servers to try when accepting and sending data, only valid when Memcache.allow_failover is turned on. Memcache.max_failover_attempts = 20

The data will be transferred according to the block size set by this value. The smaller this value is, the more additional network traffic is required.

; If you find that unexplained speed decreases, you can try to increase this value to 32768.

Memcache.chunk_size = 8192

; The default TCP port to use when connecting to the memcached server.

Memcache.default_port = 11111

; Controls the policy that maps the key to the server. The default value of "standard" means using the previous version of the old hash policy.

; Set to "consistent" to allow the server to be added/removed from the connection pool without recalculating the mapping between key and server.

; memcache.hash_strategy = "standard"; Controls the hash function that maps the key to the server. The default value Crc32″ uses the CRC32 algorithm, while "FNV" indicates the use of the FNV-1A algorithm.

; The fnv-1a is slightly lower than the CRC32, but the hash effect is better.

; memcache.hash_function = "Crc32″

finally , save php.ini, perform sudo/etc/init.d/apache2 restart restart Apache.

3) using Memcache in PHP

<?php

$mem = new Memcache; Create a Memcache object

$mem->connect ("127.0.0.1″, 11111); Connecting Memcache servers

$val = "This is a memcache test.";

$key = MD5 ($val);

$mem->set ($key, $val, 0, 120); Add a cache with a cache time of 120s

if ($k = $mem->get (' key ')) {//To determine whether to get to the specified key

Echo ' Fromcache: '. $k;

} else {

echo ' normal '; Here we need to replace the query database and create the cache in actual use.

}

?>

so far, through the above steps, we have completed the configuration and basic use of memcache ...

4) methods provided by the Php5-memcache extension

Memcache::add-adds a value that returns false if it already exists
memcache::addserver-Add a server address that is available for use
memcache::close-Close a Memcache object
memcache::connect-Creating a Memcache Object
memcache_debug-Control Debugging function
Memcache::d ecrement-to subtract a value from a saved key
Memcache::d elete-Delete a key value
memcache::flush-to clear all cached data
memcache::get-get a key value
memcache::getextendedstats-get run System statistics for all processes in the process pool
memcache::getserverstatus-getting parameters to run the server
memcache::getstats-returns some running statistics for the server
memcache::getversion-returns the version information of the running Memcache
memcache::increment-an addition to the value in a saved key
Memcache: Create a Memcache Persistent connection object:p connect-
Memcache::replace-r Overwrite an existing key
Memcache::set-adds a value that, if already present, overwrite
memcache::setcompressthreshold-compression of data larger than a certain size
memcache::setserverparams-modifying the server's parameters at run time

The following are the installation and use of memcached on the operating system Ubuntu 10.04:

1. Download unzip, install Libevent

mkdir ~/SRC

CD ~/SRC

Wgethttp://www.monkey.org/~provos/libevent-2.0.10-stable.tar.gz

Tar xzvf libevent-2.0.10-stable.tar.gz

CD libevent-2.0.10-stable

./configure--prefix=/usr

Make

sudo make install

Cd..

2. Test whether the Libevent is installed successfully:

# Ls-al/usr/lib | grep libevent

3. Download unzip, install memcached

Wgethttp://memcached.googlecode.com/files/memcached-1.4.5.tar.gz

Tar xzvf memcached-1.4.5.tar.gz

CD memcached-1.4.5

./configure--with-libevent=/usr

Make

sudo make install

Cd..

4. Test whether the memcached is installed successfully:

# ls-al/usr/local/bin/mem*

5. Start the server side of the memcache:

#/usr/local/bin/memcached-d-M 10-u root-l127.0.0.1-p 11211-c 256-p/tmp/memcached.pid

The-D option is to start a daemon,

-M is the amount of memory allocated to Memcache, in megabytes, I'm 10MB,

-U is the user running memcache, I am root here,

-L is the server IP address of the listener, if there are multiple addresses, I specify the server IP address 127.0.0.1,

-P is the port that sets Memcache listening, I set here 11211, preferably more than 1024 ports,

The-c option is the maximum number of concurrent connections to run, the default is 1024, I set the 256 here, according to the load of your server to set,

-P is set to save memcache PID file, I am here to save in/tmp/memcached.pid,

6. If you want to end the memcache process, execute:

# Kill ' Cat/tmp/memcached.pid '

You can also start multiple daemons, but the ports cannot be duplicated.

7. Restart the Web service

/etc/init.d/lighttpd-flighttpd.conf Restart

8. Memcache Environmental test:

telnet localhost 11211

Save

Set

Good 32 0 10

HelloWorld

STORED

Retrieve

Gets good

VALUE Good 32 10 10

HelloWorld

END

Replace

Replace good 32 0 10

Worldhello

STORED

Get good

VALUE Good 32 10

Worldhello

END

Trailing add

Append Good 32 0 5

After

STORED

Get good

VALUE Good 32 15

Worldhelloafter

END

Add header

Prepend Good 32 0 6

Before

STORED

Get good

VALUE Good 32 21

Beforeworldhelloafter

END

Delete

Delete Good

DELETED

Get good

END

Installing the server

sudo apt-get install memcached

$ memcached-d-M 50-p 11211-uroot

The parameter description-m specifies how many megabytes of cache space to use;-p specifies the port to listen on;-u specifies which user to run

Installing the PHP module

sudo apt-get install Php5-memcache

Editing a configuration file

$ sudo vim/etc/php5/conf.d/memcache.ini

; Uncomment the next line to Enablethe module

extension=memcache.so

 

[Memcache]

memcache.dbpath= "/var/lib/memcache"

memcache.maxreclevel=0

memcache.maxfiles=0

memcache.archivememlim=0

memcache.maxfilesize=0

memcache.maxratio=0

 

$mem = new Memcache;

$mem->connect ("127.0.0.1", 11211);

$mem->set (' key ', ' This is amemcached test! ', 0;

$val = $mem->get (' key ');

Echo $val;

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: info-contact@alibabacloud.com 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.