Linux下Memcache服务搭建

栏目: 数据库 · 发布时间: 8年前

内容简介:Linux下Memcache服务搭建

Memcache

Memcache的作用网上资料都讲的很好,说简单点就是减轻读取数据库的压力,原理也很简单:被请求的数据会先到memcache里去取,如果没有就去数据库里取,顺便给memcache带一份。每次更新数据也先更新memcache里的数据,如果没有则更新数据库,同时更新memcache。

因此需要注意的是这个数据是易失去性存储的。

模式和端口

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

服务端:使用 Memcached 软件

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

服务端口:11211(可改)

软件清单:

libevent依赖库 http://www.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的

Linux下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模块了

Linux下Memcache服务搭建

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

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

  cd !$

Linux下Memcache服务搭建

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

Linux下Memcache服务搭建

因此需要先把 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

Linux下Memcache服务搭建

对接成功

这里可以阅读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简单用法参见

//create a memcache object

        $mem = new Memcache();

//create connection

        $mem->connect('localhost',11211);

//save a value

        $mem->set('key1','This is first value',0,60);

//save a array

        $arr = array(1,2,3,4,5);

        $mem->set('key2',$arr,0,60);

        print_r($mem);

//get a value

        $val1 = $mem->get('key');

//replace the value

        $mem->replace('key1'."This is second value",0,60);

//delete the value

        $mem->delete('key2');

//flush all value

        $mem->flush();

//close connection

        $mem->close();

上面两个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

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

Linux下Memcache服务搭建

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

Linux下Memcache服务搭建

Linux下Memcache服务搭建

后退再去取一次

Linux下Memcache服务搭建

测试写入数据

Linux下Memcache服务搭建

Linux下Memcache服务搭建

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

为PHP安装Memcached扩展连接Memcached http://www.linuxidc.com/Linux/2016-05/131690.htm

Linux下Memcached安装与启用 http://www.linuxidc.com/Linux/2016-07/133423.htm

Memcached构建缓存加速集群部署 http://www.linuxidc.com/Linux/2017-02/140656.htm

Linux CentOS 7 下通过Memcached实现Session共享 http://www.linuxidc.com/Linux/2016-09/135552.htm

Memcached的安装配置及将PHP的session保存在Memcached中 http://www.linuxidc.com/Linux/2017-02/140679.htm

Linux CentOS 7 下通过Memcached实现Session共享 http://www.linuxidc.com/Linux/2016-09/135552.htm

CentOS 6.6下Memcached 源码安装配置 http://www.linuxidc.com/Linux/2015-09/123019.htm

Linux CentOS 7下Memcached 安装与配置 http://www.linuxidc.com/Linux/2016-09/135553.htm

Memcached 的详细介绍 请点这里

Memcached 的下载地址请点这里

本文永久更新链接地址 http://www.linuxidc.com/Linux/2017-05/144291.htm


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Tagging

Tagging

Gene Smith / New Riders / 2007-12-27 / GBP 28.99

Tagging is fast becoming one of the primary ways people organize and manage digital information. Tagging complements traditional organizational tools like folders and search on users desktops as well ......一起来看看 《Tagging》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

MD5 加密
MD5 加密

MD5 加密工具