Memcached的安裝php
1.安裝libevent
libevent是一個事件觸發的網絡庫,適用於windows、linux、bsd等多種平臺,內部使用 select、epoll、kqueue等系統調用管理事件機制。memcached也是libevent based,並且libevent在使用上能夠作到跨平臺,並且根據libevent官方網站上公佈的數據統計,彷佛也有着非凡的性能。
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.
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.gz
[root@localhost home]# cd libevent-2.0.21-stable
[root@localhost libevent-2.0.21-stable]# ./configure
[root@localhost libevent-2.0.21-stable]#make && make installlinux
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/memcachedwindows
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擴展app
下載地址: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,具體的路徑得根據本身的環境修改。
/usr/local/php/bin/phpizesocket