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

接下來就可以在瀏覽器上查看了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章