1、memcached了解了一段时间了,一直没有去实践。
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
2、系统环境,centos6.3的系统。上面安装了mysql数据库。
3、下面开始安装了,
http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
4、解压,配置,编译,安装
./configure --prefix=/usr/local/libevent && make && make install ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent make && make install
5、为memcached建立随机启动的服务脚本,创建/etc/init.d/memcached文件,内容:
#!/bin/bash # # Init file for memcached # # chkconfig: 345 60 60 # description: The memcached daemon is a network memory cache service. # # processname: memcached # config: /etc/sysconfig/memcached . /etc/rc.d/init.d/functions [ -f /etc/sysconfig/memcached ] && . /etc/sysconfig/memcached #将memcached的参数定义设置为独立配置文件 ## Default variables PORT=${PORT:-11211} USER=${USER:-nobody} MAXCONN=${MAXCONN:-1024} CACHESIZE=${CACHESIZE:-64} OPTIONS=${OPTIONS:-""} RETVAL=0 prog="/usr/local/memcached-1.4.15/bin/memcached" desc="network memory cache service" lockfile="/var/lock/subsys/memcached" start() { echo -n $"Starting $desc (memcached): " daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE $OPTIONS RETVAL=$? echo [ $RETVAL -eq 0 ] && touch $lockfile return $RETVAL } stop() { echo -n $"Shutting down $desc (memcached): " killproc $prog RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f $lockfile return $RETVAL } restart() { stop start } reload() { echo -n $"Reloading $desc ($prog): " killproc $prog -HUP RETVAL=$? echo return $RETVAL } case "$1" in start) start ;; stop) stop ;; restart) restart ;; condrestart) [ -e $lockfile ] && restart RETVAL=$? ;; reload) reload ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|restart|condrestart|status}" RETVAL=1 esac exit $RETVAL
当然也可以借鉴官网的:
https://pressflow.atlassian.net/wiki/display/PF/Replacement+Init+Script+for+memcached+on+Debian+or+Ubuntu
6、建立memcached参数配置文件:
echo -e 'PORT="11211" \nUSER="root" \nMAXCONN="1024" \nCACHESIZE="64" \nOPTIONS="" ' > /etc/sysconfig/memcached
7、添加执行权限,加入到系统服务:
chmod 755 /etc/init.d/memcached chkconfig --add memcached chkconfig memcached on service memcached start
8、安装到这里已经基本结束了,我们查看下他是否启动了
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 7645/mysqld tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 2342/memcached tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2498/httpd tcp 0 0 0.0.0.0:10000 0.0.0.0:* LISTEN 2265/perl tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1624/sshd
9、可以看到端口也开放了。接下来我们就可以安装memadmin来管理mencache
安装memcached的php扩展 首先到http://pecl.php.net/package/memcache下载memcache的php扩展最新稳定版本 wget http://pecl.php.net/get/memcache-2.2.5.tgz wget http://www.junopen.com/memadmin/memadmin-1.0.12.tar.gz tar zxvf memadmin-1.0.12.tar.gz cd memadmin mv * /var/www/html/memadmin tar zxvf memcache-2.2.5.tgz cd memcache-2.2.5 phpize /configure --enable-memcache --with-php-config=/usr/bin/php-config --with-zlib-dir make && make install
10、在php.ini文件中添加memcache扩展
vim /etc/php.d/php.ini 添加以下内容 extension_dir = "/usr/lib/php/modules/" extension=memcache.so 检验memcache安装 [root@Linux ~]#php -m [...] memcache mysql mysqli [...]
11、查看memcache使用情况