Memcache服务搭建

Memcachephp

  Memcache的做用网上资料都讲的很好,说简单点就是减轻读取数据库的压力,原理也很简单:html

  被请求的数据会先到memcache里去取,若是没有就去数据库里取,顺便给memcache带一份。java

  每次更新数据也先更新memcache里的数据,若是没有则更新数据库,同时更新memcache。python

  所以须要注意的是这个数据是易失去性存储的。mysql

 

模式和端口sql

  Memcache是一个基于C/S的结构:数据库

      服务端:使用Memcached软件vim

  客户端:使用Memcache插件 (这个插件是结合后端语言好比php python java)后端

  服务端口:11211(可改)浏览器

 

软件清单:

  libevent依赖库      http://libevent.org/

  memcache插件       http://pecl.php.net/package/memcache/

  memcached服务                 http://www.memcached.org/

  lamp环境           yum -y install httpd php php-mysql mysql-server

      操做系统                          CentOS-6.5(x86_64)

 

 

1.将上传相关软件包,安装lamp环境

      yum -y install httpd php php-mysql mysql-server

      /etc/init.d/httpd start

      echo "<?php phpinfo()?>" > /var/www/html/index.php

 

      而后用浏览器访问查看php信息,在信息里面是找不到memcache的

 

2.安装libevent插件

  tar xf libevent-2.0.22-stable.tar.gz

  cd libevent-2.0.22-stable

     ./configure --prefix=/usr/local/libevent && make && make install

 

 

3.安装memcached服务端

  tar xf memcached-1.4.36.tar.gz

  cd memcached-1.4.36

  ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/

  make && make install

 

  安装好后会在/usr/local/memcached/bin/目录下生成memcached

 

4.配置环境变量

  cd  /etc/profile.d/ 

  vim mem.sh

  export PATH="/usr/local/memcached/bin:$PATH" #写入profile文件开机自动导入

  memcached -m 32 -p 11211 -d -c 8192 -u root  #m分出内存大小 p 端口 d 混合模式 c 最大链接数

  netstat -anptu | grep memcached        #查看是否启动,运行多实例更改端口便可

  free -m                      #能够看到内存愈来愈少,由于被分配出去了 
  ps -aux | grep memcached            #查看进程pid是多少

  kill -9 1234                   #关闭memcached服务

   pkill memcached                  #同上

 

 

5.memcached使用

  yum -y install nc telnet

     1)使用nc命令链接memcache

   printf "set first 0 30 5\r\nmmmmm\r\n" | nc 127.0.0.1 11211   #存数据 (字段分别为 key,标志,效期,长度,值 )

  printf "get first\r\n" | nc 127.0.0.1 11211            #取数据 

  2)使用telnet命令链接memcache

  telnet 127.0.0.1 11211    #而后就可使用相关的memcached命令了

 

6.下面是关于memcached相关的操做命令

  

    add key1 0 30 3    #添加数据30为效期(若是写0表示永不过时) 3为大小

  set key1 0 30 3    #更新数据,不存在会自动建立

  replace key1 0 30 3  #更新数据,不存在会报错

  delete key1      #删除数据

  get key1       #获取数据

  gets key 1       #获取更多信息

  stats setting    #查看配置信息

  stats slabs     #查看slab

  stats items     #查看item

  stats size      #查看大小

 

 

7.安装memcache客户端php插件

  安装phpize命令能够为php添加新模块

  若是不知道是什么包可使用 yum provides */phpize

  yum -y install php-devel

  tar xf memcache-2.2.7.tgz

  cd memcache-2.2.7

  phpize          #打模块,生成configure等文件

  which php-config      #查看php-config路径位置

  ./configure --enable-memcache --with-php-config=/usr/bin/php-config

  make && make install

 

  安装号后模块会被安装置/usr/lib64/php/modules/memcache.so

  cd /etc/php.d/

  cp mysql.ini memcache.ini  #vim进行编辑将extension的值设置成memcache.so

 

  重启服务后能够看到php已经支持了memcache模块了

  

 

8.后面能够结合php网站测试数据库相关

  tar xf memcache_page.tar.gz -C /var/www/html/

  cd !$

  

  测试页面有 mysql_connect.php 编辑一下

     

 

  所以须要先把mysql的用户设置一下

  /etc/init.d/mysqld  start

  mysql_secure_installation 

  或者本身在数据库里

  grant all on *.* to 'root'@'127.0.0.1' identified by '123456'

  flush privileges

  而后浏览器访问mysql_connect.php

  

  对接成功

 

  这里能够阅读read.php和write.php了解memcache的读写原理

read.php

<?php $memcachehost = '192.168.1.113'; $memcacheport = 11211; $memcachelife = 60;          #memcache默认有效期
$memcache = new Memcache; $memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");  #链接memcache服务器
$num=$_POST["num"]; $db=db1; $tb=T1; $query="select * from $tb where ID=$num";  #mysql查询语句 #$key=md5($query);
$key=md5($num);                  #对参数进行加密,能够看出memcache存储的值是进过加密的
if(!$memcache->get($key))            #尝试先从memcache取值,若是没有去数据库取,顺便给memcache来一份
{ $conn=mysql_connect("127.0.0.1","root","123456"); mysql_select_db($db); $result=mysql_query($query); # echo "mysql $num";
                while ($row=mysql_fetch_assoc($result)) { $arr[]=$row; } $f = 'mysql'; $memcache->add($key,serialize($arr),0,30); $data = $arr ; } else{ $f = 'memcache'; $data_mem=$memcache->get($key); $data = unserialize($data_mem); } echo "$f $num"; echo "key is $key"; echo "<br>"; ?>

 

write.php

<?php $memcachehost = '192.168.1.113'; $memcacheport = 11211; $memcachelife = 60; $memcache = new Memcache; $memcache->connect($memcachehost,$memcacheport) or die ("Could not connect"); $num=$_POST["num"]; $db=db1; $tb=T1; $query="insert into $tb values($num)"; #$key=md5($query);
$key=md5($num); if(!$memcache->get($key))            //先尝试更新memcache,若是不存在,则再去更新数据库,同时更新存储到memcachce
{ $conn=mysql_connect("127.0.0.1","root","123456"); mysql_select_db($db); $result=mysql_query($query); while ($row=mysql_fetch_assoc($result)) { $arr[]=$row; } $f = 'mysql'; $memcache->add($key,serialize($arr),0,30);        //mysql 插入成功后,插入 memcached
                $data = $arr ; #} #else{
        $f1 = 'memcache'; $data_mem=$memcache->get($key); $data = unserialize($data_mem); } echo "$f $f1 $num"; echo "<br>"; ?>

 关于php memcache简单用法参见http://www.cnblogs.com/demonxian3/p/6868361.html

 上面两个php里能够看到调用了数据库的db1 和 表T1所以须要建立一下

  seq 1 999 > /tmp/sum  #建立1-999的测试数据

 

  链接数据库导入数据

  create database db1; create T1(id int)engine=innodb;

  load data infile '/tmp/sum' into table T1;  #导入测试数据

  科普一下:使用history查看历史命令,输入!+数字能够执行编号的那条命令

  !111

 

  用浏览器来访问那个测试页面

  

 

   测试读取数据,从数据库里查询出id为5的值

    

    

  后退再去取一次

   

  测试写入数据

   

   

  最后给你们推荐一款很好用的memcache管理工具:memadmin    php写的

 

搭建memcache过程可能会出现的问题:

执行 memcached 启动命令时,报错,提示:error while loading shared libraries: libevent-2.1.so.6: cannot open shared object file: No such file or directory

  • 查看 memcached 命令缺失什么库
ldd /usr/local/memcached/bin/memcached

结果: libevent-2.1.so.6 模块找不到

 

  • 查看 libevent-2.1.so.6 是否存在
locate libevent-2.1.so.6

结果: 系统已经安装了该模块,在路径 /usr/local/lib/

 

  • 查看 memcached 查找依赖库的路径
LD_DEBUG=libs /usr/local/memcached/bin/memcached -v

结果: 在 /lib64/ 目录中查找,因此找不到已经安装好的

 

  • 映射 libevent-2.1.so.6 到 /lib64 路径中
ln -s /usr/local/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6

结果:这样处理后,memcached就能够搜索到该文件了

 

  • 启动memcached服务
/etc/init.d/memcached restart
相关文章
相关标签/搜索