阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux下Memcache服务搭建

152次阅读
没有评论

共计 5808 个字符,预计需要花费 15 分钟才能阅读完成。

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

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计5808字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中