memcached的安装、配置及使用

1.安装

1.1下载libevent库Libevent 是一个用C语言编写的、轻量级的开源高性能网络库,memcached 需要本文开头libevent 库

#yum install libevent-devel

1.2下载安装memcached

#wget https://storage.googleapis.com/google-code-archive-downloads/v2/cdoe.google.com/memcached/memcached-1.4.15.tar.gz

#tar zxvf  memcached-1.4.15.tar.gz#cd memcached-1.4.15#./configure --prefix=/usr/local/memcached --with-libevent=/usr/lib64/

ps:有的版本须有另加选项--with-php-config=/usr/local/php/bin/php-config ,根据./configure -help查看是否有此选项。

安装:

#make && make install

2.启动memcached

2.1查看memcached端口11211的占用情况:

#lsof -i:11211              //-i 用以显示符合条件的进程情况

或者:

#netstat -apn | grep 11211  //-a (all)显示所有选项,默认不显示LISTEN相关,-n 拒绝显示别名,能显示数字的全部转化成数字,-p                                   显示建立相关链接的程序名     

2.2启动参数说明    

-d 选项是启动一个守护进程,    

-m 是分配给Memcache使用的内存数量,单位是MB,默认64MB    

-M return error on memory exhausted (rather than removing items)    

-u 是运行Memcache的用户,如果当前为root 的话,需要使用此参数指定用户。    

-l 是监听的服务器IP地址,默认为所有网卡。    

-p 是设置Memcache的TCP监听的端口,最好是1024以上的端口    

-c 选项是最大运行的并发连接数,默认是1024    

-P 是设置保存Memcache的pid文件    

-f chunk size growth factor (default: 1.25)    

-I Override the size of each slab page. Adjusts max item size(1.4.2版本新增)    

-vv 用very vrebose模式启动,调试信息和错误输出到控制台

2.3启动方式举例

#/usr/local/memcached/bin/memcached -d -m 64 -u root -l 127.0.0.1 -p 11211 -c 128 -P       /tmp/memcached.pid -vv

ps:也可以启动多个守护进程,但是端口不能重复

2.4设置开机自动启动

#vim vi /etc/rc.d/rc.local

添加下面的语句

#/usr/local/memcached/bin/memcached -d -m 64 -u root -l 127.0.0.1 -p 11211 -c 128 -P /tmp/memcached.pid

3.停止memcached服务

#ps aux | grep memcached#kill -15 ***

4.配置nginx

4.1添加如下代码
localtion / {
set $memcached_key "$uri"; #通常将$uri设置为key
memcached_pass 127.0.0.1:11211; #memcached主机及其端口
error_page 404 /callback.php; #自定义错误处理页面,将其放于$document_root目录下
}
4.2重启nginx
#/usr/local/memcached/bin/memcached -s reload

5.编程举例

查看memcache的使用方法:查看PHP手册
查看nginx中各个变量的值http://nginx.org/en/docs/varindex.html
5.1nginx中$request_uri和$uri的区别
$request_uri这个变量等于从客户端发送来的原生请求URI,包括参数,它不可以进行修改。
$uri这个变量指当前的请求URI,不包括任何参数。
例如:
$request_uri为    /stat.php?id=1585378&web_id=1585378
$uri为            /stat.php
5.2举例
//获取请求者的uri
$request_uri=$_SERVER['REQUEST_URI'];
$uri /stat.php
//连接数据库,取出数据
$conn=new mysqli('localhost','root','root','forum');
$sql="select * from admin where admin_id=2";
$res=$conn->query($sql);

if($res){
	$arr=$res->fetch_assoc();
	$user=$arr['username'];
	//----------创建memcache----------
	$mem=new memcache();
	$mem->connect('localhost',11211);
	$mem->add($request_uri,$user,false,60); //$request_uri是key的名字、$user是存储的变量值、false不使用压缩、缓存超时为60秒
	$mem->close();
}

这样,第一次是从数据库中取出数据,在缓存没有超时之前的第二次、第三次……都是从内存中取,而不再是从数据库。

6.安装一致性哈希模块

6.1登陆官网下载模块,并查看其使用方法
https://www.nginx.com/resources/wiki/modules/consistent_hash/ 
6.2下载Upstream Consistent Hash模块
#wget https://codeload.github.com/replay/ngx_http_consistent_hash/zip/master
#unzip master
#cd  ngx_http_consistent_hash-master/

6.3配置nginx,将第三方模块编译进nginx
进入到nginx的解压目录
#cd /usr/local/src/nginx-1.10.2
查看nginx如何编译第三方模块
#./configure --help | grep add
配置nginx,准备安装
#./configure --prefix=/usr/local/nginx/ --add-module=/usr/local/src/ngx_http_consistent_hash-master/

根据https://www.nginx.com/resources/wiki/modules/consistent_hash/提供的方法,向nginx.conf中添加upstream段。

6.4举例 [来源]

upstream somestream {
  consistent_hash $request_uri;
  server 10.50.1.3:11211;
  server 10.50.1.4:11211;
  server 10.50.1.5:11211;
}

...

server {
  listen       80;
  server_name  localhost;

  location / {
    default_type text/html;
    set $memcached_key $request_uri;
    memcached_pass somestream;
    error_page      500 404 405 = @fallback;
  }

  location @fallback {
    root /srv/www/whatever;
    fastcgi_intercept_errors on;
    error_page 404 = @404;

    set $script $uri;
    set $path_info "";

    include /usr/local/nginx/conf/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /srv/www/whatever/test.php;
    fastcgi_param SCRIPT_NAME $script;
    fastcgi_param REQUEST_URI $uri;
    fastcgi_pass   127.0.0.1:9000;
  }
}
This example uses three backend servers. On initialization NGINX will create a hashring which contains each server (160 * weight) times in the same way as the  php-memcache  module with  hash_strategy = consistent  does. Based on a hash of  $request_uri  it will decide which backend server has to be used. Now the  test.php  script from the above example could look like following:

$memcache = new Memcache;

$memcache->addServer('10.50.1.3', 11211);
$memcache->addServer('10.50.1.4', 11211);
$memcache->addServer('10.50.1.4', 11211);

$memcache->set($_SERVER["REQUEST_URI"], $_SERVER["REQUEST_URI"] . "from memcache");

7.配置php也使用一致性哈希算法

7.1参考网址
http://pecl.php.net/package-search.php,Package搜索memcache
或者直接打开
http://php.net/manual/en/memcache.ini.php#ini.memcache.hash-strategy
7.2打开php.ini文件
根据上面网址中的说明,可知默认的memcache.hash_strategy为standard,如果要使用一致性哈希,则需要将其修改为consistent
添加:memcache.hash_strategy=consistent
位置:extension=php_shmop.dll下面的一行

8.注意

upstream做负载均衡时,server只能使用IP,不能使用localhost。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值