memcached高缓存搭建

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了memcached高缓存搭建相关的知识,希望对你有一定的参考价值。

memcached高缓存搭建
搭建服务端和客户端,让服务端去链接客户端
web客户端:192.168.247.161
服务端:192.168.247.160

[root@localhost ~]# hostnamectl set-hostname client
[root@localhost ~]# su
[root@client ~]# 
[root@nginx ~]# hostnamectl set-hostname server
[root@nginx ~]# su
[root@server ~]# 

搭建服务端
部署libevent 事件通知库

[root@server ~]# mkdir /abc
mkdir: cannot create directory ‘/abc’: File exists
[root@server ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:  
[root@server ~]# cd /abc
[root@server abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
[root@server abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt
[root@server abc]# cd /opt
[root@server opt]# ls
data  libevent-2.1.8-stable  memcached-1.5.6  nginx-1.12.2  rh
[root@server opt]# cd libevent-2.1.8-stable/
[root@server libevent-2.1.8-stable]# yum install gcc gcc-c++ make -y
[root@server libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@server libevent-2.1.8-stable]# make && make install

libevent安装完毕,接下来安装memcached

[root@server libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/
[root@server memcached-1.5.6]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
[root@server memcached-1.5.6]# make && make install
[root@server memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin/

命令注释:

  • -d 指定守护进程
  • -m 32m 指定缓存32兆
  • -p 11211 指定端口
  • -u root 指定用户管理
    [root@server memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
    [root@server memcached-1.5.6]# netstat -natp | grep memc
    tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      115996/memcached    
    tcp6       0      0 :::11211                :::*                    LISTEN      115996/memcached    
    [root@server memcached-1.5.6]# systemctl stop firewalld
    [root@server memcached-1.5.6]# setenforce 0

    先在本地连接这个缓存数据库,看看是否可以正常连接

    [root@server memcached-1.5.6]# yum install telnet -y
    [root@server memcached-1.5.6]# telnet 127.0.0.1 11211
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is ‘^]‘.

    add 添加
    键值名称 username
    0 代表不设置序列号
    0 无时间要求,不设置过期时间
    7 字节长度,接下来如果输入的字节长度不符合要求,就会报错

    add username 0 0 7
    1234567
    STORED
    add users 0 0 7
    123
    STORED
    ERROR

    查看输入的键值

    get username
    VALUE username 0 7
    1234567
    END

    value 后面的1 是更新因子,如果更新一次,就会自加一

    gets username
    VALUE username 0 7 1
    1234567
    END
    set username 0 0 8
    12345678
    STORED
    gets username
    VALUE username 0 8 3
    12345678
    END

    replace 是更新的意思,如果当前的键值名称不存在,就会更新失败
    set 也是更新的意思,如果当前的键值名称不存在,会新建一个键值

    replace school 0 0 2
    NOT_STORED
    get school
    END
    replace username 0 0 9
    123456789
    STORED
    gets username
    VALUE username 0 9 4
    123456789
    END

    cas 更新,依据更新因子进行更新,相同则更新

    cas username 0 0 7 4
    1234567
    STORED
    gets username
    VALUE username 0 7 5
    1234567
    END
    cas username 0 0 8 2
    12345678
    EXISTS
    gets username
    VALUE username 0 7 5
    1234567
    END

    键值后追加数据

    append username 0 0 4
    appe
    STORED
    get username
    VALUE username 0 11
    1234567appe
    END

    键值前追加数据

    prepend username 0 0 5
    prepe
    STORED
    get username
    VALUE username 0 16
    prepe1234567appe
    END

    删除键值

    delete username
    DELETED
    get username
    END

    stats 显示状态信息
    清除所有缓存数据 flush_all

    flush_all
    OK
    get users
    END

    退出:quit

    quit
    Connection closed by foreign host.
    [root@server memcached-1.5.6]# 

    下一步安装客户端,安装客户端首先要部署LAMP架构环境,LAMP架构部署详细过程可查看博客:https://blog.51cto.com/14557905/2458323

LAMP安装完毕,接下来安装memcacahe客户端

[root@client abc]# yum install autoconf -y
[root@client htdocs]# cd /abc
[root@client abc]# tar zxvf memcache-2.2.7.tgz -C /opt
[root@client abc]# cd /opt
[root@client opt]# ls
httpd-2.4.29  memcache-2.2.7  mysql-5.6.26  package.xml  php-5.6.11  rh
[root@client opt]# cd memcache-2.2.7/
[root@client memcache-2.2.7]# ls
config9.m4  example.php                 memcache.php        memcache_standard_hash.c
config.m4   memcache.c                  memcache_queue.c    php_memcache.h
config.w32  memcache_consistent_hash.c  memcache_queue.h    README
CREDITS     memcache.dsp                memcache_session.c
[root@client memcache-2.2.7]# 
[root@client memcache-2.2.7]# /usr/local/php5/bin/phpize
[root@client memcache-2.2.7]# ls
acinclude.m4    config.sub    Makefile.global             memcache_standard_hash.c
aclocal.m4      configure     memcache.c                  missing
autom4te.cache  configure.in  memcache_consistent_hash.c  mkinstalldirs
build           config.w32    memcache.dsp                php_memcache.h
config9.m4      CREDITS       memcache.php                README
config.guess    example.php   memcache_queue.c            run-tests.php
config.h.in     install-sh    memcache_queue.h
config.m4       ltmain.sh     memcache_session.c
[root@client memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/usr/local/php5/bin/php-config
[root@client memcache-2.2.7]# make && make install
[root@client memcache-2.2.7]# vim /usr/local/php5/php.ini 
 736 ; extension_dir = "ext"
 737 extension_dir="/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/"      #增加
 738 extension = memcache.so    #增加

在web客户端去检测服务端是否可以连接

[root@client memcache-2.2.7]# cd /usr/local/httpd/htdocs/
[root@client htdocs]# ls
index.php
[root@client htdocs]# vim gsy.php
<?php
$memcache = new Memcache();
$memcache->connect(‘192.168.247.160‘,11211);
$memcache->set(‘key‘,‘Memcache test Successfull‘,0,60);
$result = $memcache->get(‘key‘);
unset($memcache);
echo $result;
?>
[root@client htdocs]# service httpd restart

接下来就可以在浏览器上查看了

以上是关于memcached高缓存搭建的主要内容,如果未能解决你的问题,请参考以下文章

Memcached集群/分布式/高可用 及 Magent缓存代理搭建过程 详解

搭建Memcache缓存数据库

Memcached搭建

phalcon: 缓存片段,文件缓存,memcache缓存

为 memcached 和 Rails 组合片段和对象缓存的最佳方式

Magent+keepalived+Memcached缓存高可用群集