Memcached的安裝和應用

一,Memcached的安裝php

1.安裝libeventhtml

libevent是一個事件觸發的網絡庫,適用於windows、linux、bsd等多種平臺,內部使用select、epoll、kqueue等系統調用管理事件機制。memcached也是libevent based,並且libevent在使用上能夠作到跨平臺,並且根據libevent官方網站上公佈的數據統計,彷佛也有着非凡的性能。linux

The libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. Furthermore, libevent also support callbacks due to signals or regular timeouts.windows

libevent is meant to replace the event loop found in event driven network servers. An application just needs to call event_dispatch() and then add or remove events dynamically without having to change the event loop.緩存

安裝Memcached前須要先安裝libevent,下載地址:服務器

http://libevent.org/網絡

[root@localhost home]# tar zxvf libevent-2.0.21-stable.tar.gzsession

[root@localhost home]# cd libevent-2.0.21-stable併發

[root@localhost libevent-2.0.21-stable]# ./configureapp

[root@localhost libevent-2.0.21-stable]#make && make install

2.安裝Memcached

下載地址:

http://memcached.org/

[root@bogon home]# tar zxvf memcached-1.4.15.tar.gz

[root@bogon home]# cd memcached-1.4.15

[root@bogon memcached-1.4.15]# ./configure

[root@bogon memcached-1.4.15]# make && make install

安裝完成後,Memcached的默認安裝目錄是/usr/local/bin/memcached

3.啓動Memcached

[root@bogon run]# /usr/local/bin/memcached -m 32m -p 11211 -d -u root -P /var/run/memcached.pid -c 256

啓動時可能出現以下錯誤:

[root@bogon run]# /usr/local/bin/memcached -m 32m -p 11211 -d -u root -P /var/run/memcached.pid -c 256

/usr/local/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory

提示找不到libevent-2.0.so.5文件,解決辦法是把/usr/local/lib

加入到/etc/ld.so.conf中,/etc/ld.so.conf裏保存的是想要讀入高速緩存當中的動態函數庫所在的目錄,過程以下:

[root@bogon run]# echo "/usr/local/lib">>/etc/ld.so.conf

[root@bogon run]# ldconfig    #利用ldconfig這個可執行文件將/etc/ld.so.conf的數據讀入到高速緩存中,

[root@bogon script]# ldconfig -p|grep libevent-2.0.so.5    #ldconfig -p 查看libevent-2.0.so.5庫已經被讀入到高速緩存中了。

libevent-2.0.so.5 (libc6) => /usr/local/lib/libevent-2.0.so.5

啓動過程參數說明:

-p:使用TCP端口。默認是11211.

-m:最大內存的大小。默認是64M。

-vv:以very vrebose模式啓動,將調試信息和錯誤輸出到控制檯。

-d:作爲守護進程在後臺運行。

-c:最大運行的併發鏈接數,默認是1024,按照服務器的負載量來設定。

-P :設置保存Memcached的pid文件。

-l:監聽的服務器IP地址,若是有多個地址。

-u:運行Memcached的用戶,默認不能由root用戶啓動,因此當前用戶爲root時須要利用-u參數來指定。

經過/usr/local/bin/memcached -h命令能夠顯示全部可選項:

[root@bogon run]# /usr/local/bin/memcached -h

memcached 1.4.15

-p <num>      TCP port number to listen on (default: 11211)

-U <num>      UDP port number to listen on (default: 11211, 0 is off)

-s <file>     UNIX socket path to listen on (disables network support)

-a <mask>     access mask for UNIX socket, in octal (default: 0700)

-l <addr>     interface to listen on (default: INADDR_ANY, all addresses)

             <addr> may be specified as host:port. If you don't specify

             a port number, the value you specified with -p or -U is

             used. You may specify multiple addresses separated by comma

             or by using -l multiple times

-d            run as a daemon

-r            maximize core file limit

-u <username> assume identity of <username> (only when run as root)

-m <num>      max memory to use for items in megabytes (default: 64 MB)

-M            return error on memory exhausted (rather than removing items)

-c <num>      max simultaneous connections (default: 1024)

-k            lock down all paged memory.  Note that there is a

             limit on how much memory you may lock.  Trying to

             allocate more than that would fail, so be sure you

             set the limit correctly for the user you started

             the daemon with (not for -u <username> user;

             under sh this is done with 'ulimit -S -l NUM_KB').

-v            verbose (print errors/warnings while in event loop)

-vv           very verbose (also print client commands/reponses)

-vvv          extremely verbose (also print internal state transitions)

-h            print this help and exit

-i            print memcached and libevent license

-P <file>     save PID in <file>, only used with -d option

-f <factor>   chunk size growth factor (default: 1.25)

-n <bytes>    minimum space allocated for key+value+flags (default: 48)

-L            Try to use large memory pages (if available). Increasing

             the memory page size could reduce the number of TLB misses

             and improve the performance. In order to get large pages

             from the OS, memcached will allocate the total item-cache

             in one large chunk.

-D <char>     Use <char> as the delimiter between key prefixes and IDs.

             This is used for per-prefix stats reporting. The default is

             ":" (colon). If this option is specified, stats collection

             is turned on automatically; if not, then it may be turned on

             by sending the "stats detail on" command to the server.

-t <num>      number of threads to use (default: 4)

-R            Maximum number of requests per event, limits the number of

             requests process for a given connection to prevent

             starvation (default: 20)

-C            Disable use of CAS

-b            Set the backlog queue limit (default: 1024)

-B            Binding protocol - one of ascii, binary, or auto (default)

-I            Override the size of each slab page. Adjusts max item size

             (default: 1mb, min: 1k, max: 128m)

-o            Comma separated list of extended or experimental options

             - (EXPERIMENTAL) maxconns_fast: immediately close new

               connections if over maxconns limit

             - hashpower: An integer multiplier for how large the hash

               table should be. Can be grown at runtime if not big enough.

               Set this based on "STAT hash_power_level" before a

               restart.

4.關閉Memcached

關閉Memcached的命令以下:

[root@bogon script]# kill `cat /var/run/memcached.pid`  #注意是返單引號。

5.安裝Memcache的PHP擴展

下載地址:http://pecl.php.net/package/memcache 我下載的是memcache-2.2.7.tgz穩定版

[root@bogon home]# tar zxvf memcache-2.2.7.tgz

[root@bogon home]# cd memcache-2.2.7

執行phpize擴展安裝程序,假設phpzie的路徑爲/usr/local/php/bin/phpize,具體的路徑得根據本身的環境修改。

[root@bogon memcache-2.2.7]# /usr/bin/phpize

Configuring for:

PHP Api Version:         20041225

Zend Module Api No:      20050922

Zend Extension Api No:   220051025

注意,必定要在memcache-2.2.7目錄下執行,不然會提示錯誤:

Cannot find config.m4.

Make sure that you run '/usr/bin/phpize' in the top level source directory of the module

[root@bogon memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/usr/bin/php-config

若是提示以下錯誤提示:

configure: error: memcache support requires ZLIB. Use --with-zlib-dir=<DIR> to specify prefix where ZLIB include and library are located

ERROR: `/var/cache/php-pear/memcache-2.2.5/configure --enable-memcache-session=yes' failed

make sure you have zlib-devel installed

這樣解決:

yum install zlib-devel

[root@bogon memcache-2.2.7]# make && make install

完成上述安裝後會有相似如下提示:

Installing shared extensions:     /usr/lib/php/modules/

6.修改php.ini中extension_dir爲:

extension_dir = "/usr/lib/php/modules"

增長以下一行來載入memcache.so擴展:

extension=memcache.so

7.測試Memcache的PHP擴展是否安裝成功:

運行下面PHP代碼,若是輸出「Hello world!「,就表示環境搭建成功。

<?php

$mem =new Memcache;

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

$mem->set(' test' , 'Hello world!' , 0, 12);

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

echo $val;

?>

7.memcache的監控

利用memcache.php對單臺Memcached進行監控

memcache.php下載地址:

http://livebookmark.net/journal/2008/05/21/memcachephp-stats-like-apcphp/

使用memcache.php需先修改用於訪問的用戶名和密碼:

vi memcache.php

define('ADMIN_USERNAME','memcache');    // Admin Username

define('ADMIN_PASSWORD','password');    // Admin Password

   …………

$MEMCACHE_SERVERS[] = '192.168.56.2:11211'; // add more as an array #定義要查看的IP和端口

$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array #可定義多個

而後能夠經過URL訪問memcache.php,輸入用戶名和密碼後的狀態圖,以下:

175917228.png

若是出現Cant connect to:192.168.56.2:11211錯誤:

175956830.png

可經過關閉防火牆或開啓防火牆端口11211的訪問策略後重試,若是還不行就設置selinux爲寬容模式,或者設置memcache.php文件正確的selinux策略。

關閉iptables:

[root@bogon html]# service iptables stop

設置selinux爲寬容模式:

[root@bogon html]# setenforce 0

相關文章
相關標籤/搜索