-----NOSQL数据库
服务端安装
需要先安装libevent
wget http://120.52.73.43/jaist.dl.sourceforge.net/project/levent/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz
tar -zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure
make
make install
操作这个步骤,不然启动memcache报错
echo "/usr/local/lib/" > /etc/ld.so.conf.d/libevent.conf
ldconfig -v
安装memcache memcached-1.4.25.tar.gz //服务端
wget http://www.memcached.org/files/memcached-1.4.25.tar.gz
tar zxvf memcached-1.4.25.tar.gz
cd memcached-1.4.25
./configure
make && make install
命令 memcached -h
//memcached -m 内存 -p 端口 -d 后台启动 -u root -c 8192 并发连接数
memcached -m 16m -p 11211 -d -u root -c 8192
memcached -m 16m -p 11212 -d -u root -c 8192 //多实例
netstat -lntup | grep mem
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 10785/memcached
tcp 0 0 :::11211 :::* LISTEN 10785/memcached
udp 0 0 0.0.0.0:11211 0.0.0.0:* 10785/memcached
udp 0 0 :::11211 :::* 10785/memcached
命令格式
printf "set key008 0 0 10\r\nhequan1234\r\n" | nc 127.0.0.1 11211
STORED //存进去了
printf "get key008\r\n" | nc 127.0.0.1 11211 //delete
VALUE key008 0 10
hequan1234
END //取出来了
telnet 127.0.0.1 11211
客户端安装
wget http://pecl.php.net/get/memcache-3.0.8.tgz
tar zxvf memcache-3.0.8.tgz
cd memcache-3.0.8
/application/php/bin/phpize
./configure --enable-memcache --with-php-config=/application/php/bin/php-config
make
make install
查看目录
ls /application/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/
memcache.so
加载
vim /application/php/lib/php.ini
extension_dir = "/application/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/"
extension=memcache.so //添加一行
重启
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload
pkill php-fpm
/application/php/sbin/php-fpm
可以写一个测试php网页
$memcache = new Memcache; //创建一个memcache对象
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect"); //连接Memcached服务器
$memcache->set('key001', 'oldboy001'); //设置一个变量到内存中,名称是key001值是oldboy001
$memcache->set('key002', 'oldboy002'); //设置一个变量到内存中,名称是key002值是oldboy002
$get_value01 = $memcache->get('key001'); //从内存中取出key001的值
$get_value02 = $memcache->get('key002'); //从内存中取出key002的值
echo $get_value02."
";
echo $get_value01;
?>
集群session共享 可以解决多个服务器共用session的难题
session.save_handler = memcache
session.save_path = "tcp://192/168.10.11:11211"
监控
#!/bin/sh
[ $# -ne 2 ]&&{
echo "$0 ip port"
exit
}
export oldboy=key
export wwwServerIp=$1
export wwwServerPort=$2
cmd="nc $wwwServerIp $wwwServerPort"
printf "delete $oldboy\r\n" | $cmd >/dev/null 2>&1
sleep 1
printf "set $oldboy 0 0 6\r\noldboy\r\n"|$cmd >/dev/null 2>&1
if [ `printf "get $oldboy\r\n"|$cmd|grep oldboy|wc -l` -eq 1 ]
then
echo "mc is alive."
exit 0
else
echo "mc is dead."
exit 2
fi