Memcached安装
Posted by 机器人 on 9th 十二月 2009 in linux/server
软件列表
memcached-1.4.4.tar.gz
libevent-1.4.13-stable .tar.gz
1. libevnet安装
tar zxvf libevent-1.4.13-stable .tar.gz cd libevent-1.4.13-stable ./configure --prefix=/usr/local/libevent make sudo make install
2. memcached安装
tar zxvf memcached-1.4.4.tar.gz cd memcached-1.4.4 ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent make sudo make install
3. 启动memcached
cd /usr/local/memcached/bin ./memcached -d -m 10 -u root -l 127.0.0.1 -p 11211-c 250 -P /tmp/memcached.pid
4.查看运行状态
netstat -nl | grep 11211 tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN tcp6 0 0 :::11211 :::* LISTEN udp 0 0 0.0.0.0:11211 0.0.0.0:* udp6 0 0 :::11211 :::*
运行正常
5.安装php扩展
ubuntu直接使用
sudo apt-get install php5-memcache
6. php连接测试
< ?php $memcache = new Memcache; $memcache->connect('localhost', 11211) or die ("Could not connect"); $version = $memcache->getVersion(); echo "Server's version: ".$version."<br />\n"; $tmp_object = new stdClass; $tmp_object->str_attr = 'test'; $tmp_object->int_attr = 123; $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server"); echo "Store data in the cache (data will expire in 10 seconds)<br />\n"; $get_result = $memcache->get('key'); echo "Data from the cache:<br />\n"; var_dump($get_result);
运行结果
Server's version: 1.4.4
Store data in the cache (data will expire in 10 seconds)
Data from the cache:
object(stdClass)#3 (2) {
["str_attr"]=>
string(4) "test"
["int_attr"]=>
int(123)
}7. 将memcached的启动和关闭命令写到脚本中
startup.sh
#!/bin/bash MEM_SIZE=10 PORT=11211 CONCURRENT=256; PID_FILE=/tmp/memcached.pid echo "Memcached startup..."; ./memcached -d -m $MEM_SIZE -u root -p $PORT -c $CONCURRENT -P $PID_FILE if [ $? = "0" ]; then echo "Success..."; else echo "Failure..."; fi
shutdown.sh
#!/bin/bash ps aux | grep 'memcached' | grep -v 'grep' | awk '{print $2}' | xargs kill
参数资料
http://www.ccvita.com/257.html
http://www.php.net/manual/en/intro.memcache.php
机器人 2009年12月9日 16:25 于 北京 阴
看了最后那个shutdown.sh,感觉你是在练习管道和grep.
其实可以用 killall memcached 完成相应的任务。
解决问题的办法不是唯一的,你完全还可以说取个pid有这么麻烦吗?还用awk,完全可以用`cat /tmp/memcached.pid` 来取得pid,然后直接用 kill `cat /tmp/memcached.pid`就可以搞定!这分明是要练习awk嘛!哈哈!!