linux memcache安装教程,让你快速使用memcache缓存服务器
1 下载好3个安装包memcached-1.4.14.tar.gz,libevent-2.0.20-stable.tar.gz,memcached-1.4.14.tar.gz
2 安装libevent
tar -zxf libevent-2.0.20-stable.tar.gz
cd libevent-2.0.20-stable
./configure --prefix=/usr/local/libevent/
make
make install
3 安装memcached
tar -zxf memcached-1.4.14.tar.gz
cd memcache-1.4.14
./configure --prefix=/usr/local/memcached/ --with-libevent=/usr/local/libevent/
make
make install
安装好后 可以把/usr/local/memcached/bin/memcached
4 启动memcached服务器
/usr/local/memcached/bin/memcached -d -m 10 -u root -l 192.168.0.14 -p 11211 -c 64 -P /tmp/memcached.pid
参数左右
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,
设置软链
ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /lib/libevent-2.0.so.5
5 将memcache载入php扩展中
tar vxzf memcache-2.2.7.tgz
cd memcache-2.2-7
/usr/local/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config-with-zlib-dir
make
make install
如果phpize没找到 安装 yum install php-devel
接着出现这样提示
Installing shared extensions :/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
6 修改php.ini
1 将extension_dir="./" 修改为extension_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
2 extension=memcache.so
3 重启apache
7 测试
01$mem =new Memcache;02$mem->connect('192.168.0.14',11211) or die('memcache connect failed');03$key='select * from part_tab limit 10';04$data = $mem->get($key);05if(!$data){06$con = mysql_connect('localhost','root','root') or die('mysql connect failed');07mysql_select_db('test',$con);08$result = mysql_query($key);09while($row = mysql_fetch_assoc($result)){10$data[]=$row;11}12$mem->set($key,$data,3600);13echo'ok';14}else{15print_r($data);16}