* 安装libevent

https://blog.csdn.net/fareast_mzh/article/details/100055821

* 安装zlib

链接: https://pan.baidu.com/s/19jBLzGAIxnXY4MbQyBGUig 提取码: ssmk 复制这段内容后打开百度网盘手机App,操作更方便哦

解压安装到目录 /usr/local/zlib

* 安装 libmemcached

从 这里 找到最新的 libmemcached 源码包,然后下载。

链接: https://pan.baidu.com/s/1OpIHtLaet67tMrIFgAnp3w 提取码: 9ma3 复制这段内容后打开百度网盘手机App,操作更方便哦

wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar -zxf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18/

sudo mkdir /usr/local/libmemcached
./configure --prefix=/usr/local/libmemcached
make -j2

sudo make install

Mac系统 编译过程中报错

libmemcached/byteorder.cc:66:10: error: use of undeclared identifier 'ntohll'

  return ntohll(value);

         ^

查看C/C++头文件位置

 gcc -v -E -x c++ -

emacs libmemcached/byteorder.cc

在引入头文件之后加入:

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

emacs clients/memflush.cc 

Replace string if (opt_servers == false) with: if (opt_servers == NULL)

 

* 安装memcached服务端

链接: https://pan.baidu.com/s/1u8xhIXPK4Qm6OEEY9y1bEg 提取码: q57w 复制这段内容后打开百度网盘手机App,操作更方便哦

# http://memcached.org/latest
wget https://www.memcached.org/files/memcached-1.5.17.tar.gz
./configure --prefix=/usr/local/memcached           # 配置
make && make test
sudo make install

memcached作为前台程序运行

/usr/local/memcached/bin/memcached -p 11211 -m 64m -vv

memcached作为后台程序运行
 

/usr/local/memcached/bin/memcached -d -m 64M -u root -l 0.0.0.0 -p 11211 -c 256 -P /tmp/memcached.pid

-d  daemon

-m  memory

-l   listen

-p port

-c concurrent

-P Process ID

查看服务启动

$ lsof -i:11211

COMMAND     PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME

memcached 18543  Mch   21u  IPv4 0x6d987ca41eb9a305      0t0  TCP *:11211 (LISTEN)

memcached用法:

https://www.runoob.com/memcached/memcached-set-data.html

$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
set runoob 0 900 9
memcached
STORED
get runoob 
VALUE runoob 0 9
memcached
END
get runoob
VALUE runoob 0 9
memcached
END
quit
Connection closed by foreign host.

 

* 安装php-memcached扩展

从 github 克隆 memcached 后,需要手动切换到 php7 分支,不然会提示 fatal error : ext / standard / php_smart_str . h : No such file or directory 错误。

git clone https://github.com/php-memcached-dev/php-memcached.git
cd php-memcached/
git checkout php7
/opt/lampp/bin/phpize
./configure --with-php-config=/opt/lampp/bin/php-config \
    --disable-memcached-sasl \
   --with-libmemcached-dir=/usr/local/libmemcached \
   --with-zlib-dir=/usr/local/zlib
make -j2 

sudo make install

./configure --with-php-config=/Applications/XAMPP/bin/php-config \
   --disable-memcached-sasl \
   --with-libmemcached-dir=/Volumes/Applications/usr/local/libmemcached \
   --with-zlib-dir=/Volumes/Applications/usr/local/zlib/

 

Installing shared extensions:     /Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20170718/

ls /Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20170718/

mcrypt.so    opcache.a    pcntl.so     pdo_dblib.so pgsql.so     sockets.so   xdebug.so

memcached.so opcache.so   pdo_dblib.a  pgsql.a      redis.so     swoole.so

sudo emacs /Applications/XAMPP/xamppfiles/etc/php.ini

extension="memcached.so"

$ php -m | grep memcached

memcached

 

php5源代码安装Memcache客户端扩展

wget http://pecl.php.net/get/memcache-3.0.8.tgz

tar xvzf memcache-3.0.8.tgz -C .

cd memcache-3.0.8

/Applications/XAMPP/bin/phpize

sudo mkdir /Volumes/Applications/usr/local/memcache

./configure --prefix=/Volumes/Applications/usr/local/memcache \
        --enable-memcache \
        --with-php-config=/Applications/XAMPP/bin/php-config \
        --with-zlib-dir=/Volumes/Applications/usr/local/zlib/

# copy PHP默认扩展目录
# cp -r /Volumes/Applications/php/php-7.2.9/ext ./ext
# cp ./ext/standard/php_smart_string_public.h ./ext/standard/php_smart_str_public.h
# cp ./ext/standard/php_smart_string.h ./ext/standard/php_smart_str.h


make -j2

sudo make install

这里编译失败

./memcache_pool.h:45:10: fatal error: 'ext/standard/php_smart_str_public.h' file not found

#include "ext/standard/php_smart_str_public.h"

         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

make: *** [Makefile:199: memcache_ascii_protocol.lo] Error 

这个只适用于php5

http://pecl.php.net/package/memcache/3.0.8/windows

换一种方法:

链接:https://pan.baidu.com/s/1B9kNYz1HeDXHIgb1UByGbg  密码:07hg

git clone https://github.com/websupport-sk/pecl-memcache
cd pecl-memcache/
/Applications/XAMPP/bin/phpize

./configure --with-php-config=/Applications/XAMPP/bin/php-config-7.2.4  --with-zlib-dir=/Volumes/Applications/usr/local/zlib/

make -j4
sudo make install
sudo emacs /Applications/XAMPP/xamppfiles/etc/php.ini
# extension=memcache.so
php -m | grep memcache
# memcache
# memcached

重启php-fpm或apache

https://www.runoob.com/memcached/php-connect-memcached.html

* test:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$memcache = null;
try {
    $memcache = new Memcache();
    $memcache->connect("127.0.0.1", 11211, 1);

    echo "Memcached's version: " . $memcache->getversion() . "<br />";
    $data = array(
        'url' => "http://www.cnblogs.com/fareast_mzh",
        'name' => "fareast_mzh"
    );
    $memcache -> set("info", $data, 0, 10);
    $info = $memcache->get("info");
    echo '<pre>';
    print_r($info);

    var_dump($memcache->get('xxx'));

} catch (MemcachedException $e) {
    echo $e->getMessage();
}

Memcached's version: 1.4.15

Array
(
    [url] => http://www.cnblogs.com/fareast_mzh
    [name] => fareast_mzh
)
bool(false)

memcache incr

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$memcache = null;
try {
    $memcache = new Memcache();
    $memcache->addserver('172.16.0.224');
    $memcache->connect("xx.xx.xx.xx", 11211, 1);

    // $memcache->delete('visitors');
    if ( ! $memcache->get('visitors') ) {
        $memcache->set('visitors', 0);
        echo 'init visitors = 0';
    }
    $memcache->increment('visitors', 1);
    $number = $memcache->get('visitors');
    echo '<pre>'; var_dump($number);

} catch (MemcachedException $e) {
    echo $e->getMessage();
}

 

 

直接用yum安装

 

安装memcached

* 安装libevent库

sudo apt-get install libevent ibevent-dev 自动下载安装(Ubuntu/Debian)
yum install libevent libevent-devel 自动下载安装(Redhat/Fedora/Centos)

 * 安装 Memcached

yum install memcached

* 启动服务

memcached -p 11211 -m 64m -d
memcached -d -m 64M -u root -l 192.168.0.200 -p 11211 -c 256 -P /tmp/memcached.pid