centos 下memcached 构建以及php memcache安装
memcached 与memcache的区别,请google
一、构建memcached 服务器(memcached服务端程序的安装)
1)memcached用到了libevent这个库,用于Socket的处理
- wget http://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 && make install
验证libevent是否正确安装
- ls -al /usr/lib |grep libevent
- [root@test /]# ls -al /usr/lib |grep libevent
- lrwxrwxrwx 1 root root 21 Feb 28 19:27 libevent-2.0.so.5 -> libevent-2.0.so.5.0.1
- -rwxr-xr-x 1 root root 943520 Feb 28 19:27 libevent-2.0.so.5.0.1
- -rw-r--r-- 1 root root 1532202 Feb 28 19:27 libevent.a
- lrwxrwxrwx 1 root root 26 Feb 28 19:27 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.0.1
- -rwxr-xr-x 1 root root 554374 Feb 28 19:27 libevent_core-2.0.so.5.0.1
- -rw-r--r-- 1 root root 928010 Feb 28 19:27 libevent_core.a
- -rwxr-xr-x 1 root root 971 Feb 28 19:27 libevent_core.la
- lrwxrwxrwx 1 root root 26 Feb 28 19:27 libevent_core.so -> libevent_core-2.0.so.5.0.1
- lrwxrwxrwx 1 root root 27 Feb 28 19:27 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.0.1
- -rwxr-xr-x 1 root root 410763 Feb 28 19:27 libevent_extra-2.0.so.5.0.1
- -rw-r--r-- 1 root root 604266 Feb 28 19:27 libevent_extra.a
- -rwxr-xr-x 1 root root 978 Feb 28 19:27 libevent_extra.la
- lrwxrwxrwx 1 root root 27 Feb 28 19:27 libevent_extra.so -> libevent_extra-2.0.so.5.0.1
- -rwxr-xr-x 1 root root 936 Feb 28 19:27 libevent.la
- lrwxrwxrwx 1 root root 29 Feb 28 19:27 libevent_openssl-2.0.so.5 -> libevent_openssl-2.0.so.5.0.1
- -rwxr-xr-x 1 root root 85731 Feb 28 19:27 libevent_openssl-2.0.so.5.0.1
- -rw-r--r-- 1 root root 120996 Feb 28 19:27 libevent_openssl.a
- -rwxr-xr-x 1 root root 1007 Feb 28 19:27 libevent_openssl.la
- lrwxrwxrwx 1 root root 29 Feb 28 19:27 libevent_openssl.so -> libevent_openssl-2.0.so.5.0.1
- lrwxrwxrwx 1 root root 30 Feb 28 19:27 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.0.1
- -rwxr-xr-x 1 root root 18094 Feb 28 19:27 libevent_pthreads-2.0.so.5.0.1
- -rw-r--r-- 1 root root 18678 Feb 28 19:27 libevent_pthreads.a
- -rwxr-xr-x 1 root root 999 Feb 28 19:27 libevent_pthreads.la
- lrwxrwxrwx 1 root root 30 Feb 28 19:27 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.0.1
- lrwxrwxrwx 1 root root 21 Feb 28 19:27 libevent.so -> libevent-2.0.so.5.0.1
2)安装memcached
- wget http://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 && make install
安装完成后,程序会将memcached 自动放到 /usr/local/bin/memcached
- [root@test /]# which memcached
- /usr/local/bin/memcached
3)安装PHP的memcache扩展
memcache 的项目下载地址
- http://pecl.php.net/package/memcache
安装code
- wget http://pecl.php.net/get/memcache-3.0.5.tgz
- tar xf memcache-3.0.5.tgz
- cd memcache-3.0.5
- ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
- make && make install
安装完成后,提示
- Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
配置php.ini文件
修改extension_dir:
- extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
添加一行以便加载memcache扩展:extension=memcache.so
- extension=memcache.so
验证PHP中是否成功加载memcache扩展
- [root@test /]# php -m
- [PHP Modules]
- Core
- ctype
- curl
- date
- dom
- eAccelerator
- ereg
- fileinfo
- filter
- hash
- iconv
- json
- libxml
- mcrypt
- memcache
- mysql
- mysqli
- pcre
- PDO
- pdo_sqlite
- Phar
- posix
- Reflection
- session
- SimpleXML
- SPL
- SQLite
- sqlite3
- standard
- tokenizer
- xml
- xmlreader
- xmlwriter
- zlib
- [Zend Modules]
- eAccelerator
17行处,有memcahe,正确加载
后续:memcached基本配置
启动memcached 服务端程序:
- memcached -d -m 10 -u root -l localhost -p 11211 -c 256 -P /tmp/memcached.pid
各个参数的含义:
- -d选项是启动一个守护进程,
- -m是分配给Memcached使用的内存数量,单位是MB,
- -u是运行Memcache的用户,我这里是root,
- -l是监听的服务器IP地址,
- -p是设置Memcache监听的端口,最好是1024以上的端口,
- -c选项是最大运行的并发连接数,默认是1024,按照你服务器的负载量来设定,
- -P是设置保存Memcache的pid文件
PS:memcached 常用方法:
- kill `cat /tmp/memcached.pid`
- 获取运行状态
- echo stats | nc 192.168.0.122 11200
- watch “echo stats | nc 192.168.0.122 11200 ” (实时状态)
- 快速清空
- echo “flush_all” | nc 192.168.0.122 11200
- 其他工具
- http://code.sixapart.com/svn/memcached/trunk/server/scripts/memcached-tool