memcache 安裝 (windows和linux)


windows 64bit 環境下安裝

下載 memcache 安裝包 :http://pan.baidu.com/s/1pLcjTzlphp

解壓到d盤任意位置 , cmd(以管理員身份運行,不然安裝失敗) 執行命令 html

clipboard.png

clipboard.png

沒有報錯說明咱們已經安裝成功linux

下載 php_memcache.dll : https://pecl.php.net/package/memcache/3.0.8/windowsredis

clipboard.png

個人是5.6的版本 因此下載 64位 TS版本 apache

將dll擴展放入php擴展文件夾中vim

clipboard.png

在php.ini 中添加 extension=php_memcache.dllwindows

clipboard.png

而後重啓wampserver
注意:apache文件夾中的php.ini 是用於瀏覽器訪問 php文件夾中的php.ini 是適用於命令行運行php腳本centos

php測試代碼 (個人是命令行測試)瀏覽器

clipboard.png


linux 安裝 (centos 64bit)

安裝環境說明:tcp

操做系統:Centos 6.5 -64bit
安裝路徑:/usr/local/memcached


安裝編譯須要的擴展包

yum install libevent libevent-devel

下載memcached tgz包 到home文件夾

[root@localhost home]# wget http://memcached.org/files/memcached-1.4.25.tar.gz

解壓並編譯memcached

tar zxvf memcached-1.4.25.tar.gz

clipboard.png

  • 打開文件夾 指定安裝路徑 並編譯

    cd memcached-1.4.25

    ./configure --prefix=/usr/local/memcached

    make && make install


安裝PHP memcache 擴展包

  • 下載php memcache 擴展 tgz包

    [root@localhost home]# wget https://pecl.php.net/get/memcache-3.0.8.tgz
    [root@localhost home]# tar zxf memcache-3.0.8.tgz
    [root@localhost home]# cd memcache-3.0.8
    [root@localhost memcache-3.0.8]# /usr/local/php/bin/phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config
    make
    make install
    # 安裝成功後會輸出這個地址,這是php擴展模塊文件夾 /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/


修改 php.ini 文件

#php.ini 中的 extension_dir =」 ./」  
#修改成extension_dir = 「/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/」 
(若已經修改過請忽略)

#添加 
extension=memcache.so

#重啓 php-fpm 
/etc/init.d/php-fpm restart

安裝PHP memcached 擴展包

  • 下載libevent tgz包 並安裝
    官網:http://libevent.org/

    [root@localhost home]# tar zxf libevent-2.0.21-stable.tar.gz
    [root@localhost home]# cd libevent-2.0.21-stable
    [root@localhost home]# ./configure --prefix=/usr/local/libevent/
    [root@localhost libevent-2.0.21-stable]# make && make install

  • 從新編譯安裝memcached服務,並配置libevent

    [root@localhost home]# cd memcached-1.4.25
    [root@localhost memcached-1.4.25]# ./configure --prefix=/usr/local/memcache/ --with-libevent=/usr/local/libevent/
    [root@localhost memcached-1.4.25]# make && make install

  • 下載 libmemcached tgz包 並編譯安裝

    安裝SASL相關環境
    yum install cyrus-sasl-plain cyrus-sasl  cyrus-sasl-devel  cyrus-sasl-lib 
    
    官網:http://libmemcached.org/libMemcached.html
    [root@localhost home]# wget https://launchpadlibrarian.net/165454254/libmemcached-1.0.18.tar.gz
    [root@localhost home]# tar zxf libmemcached-1.0.18.tar.gz
    [root@localhost home]# cd libmemcached-1.0.18
    [root@localhost libmemcached-1.0.18]# ./configure --prefix=/usr/local/libmemcached  --with-memcached --enable-sasl
    [root@localhost libmemcached-1.0.18] make
    [root@localhost libmemcached-1.0.18] make install
  • 下載php memcached 擴展 tgz包

    [root@localhost home]# wget https://pecl.php.net/get/memcached-2.2.0.tgz
    [root@localhost home]# tar zxf memcached-2.2.0.tgz
    [root@localhost home]# cd memcached-2.2.0
    [root@localhost memcached-2.2.0]# /usr/local/php/bin/phpize

    clipboard.png

./configure --with-php-config=/usr/local/php/bin/php-config 
    --with-libmemcached-dir=/usr/local/libmemcached  --enable-memcached-sasl(注意這個參數)

若是提示下面錯誤信息 說明咱們要安裝 libsasl2-dev

clipboard.png

yum直接搜索是沒有對應的package的 咱們用笨方法
[root@localhost home]# yum search sasl |  grep  dev
[root@localhost home]# yum install cyrus-sasl-devel

clipboard.png

  • 繼續編譯

    ./configure --with-php-config=/usr/local/php/bin/php-config 
    --with-libmemcached-dir=/usr/local/libmemcached  --enable-memcached-sasl(注意這個參數)
     make 
     make install

    # 安裝成功後會輸出這個地址,這是php擴展模塊文件夾
    /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/


修改 php.ini 文件

#php.ini 中的 extension_dir =」 ./」  
#修改成extension_dir = 「/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/」 
(若已經修改過請忽略)

#添加 
extension=memcached.so

#重啓 php-fpm 
/etc/init.d/php-fpm restart

建立memcached自啓動腳本 , 在 /etc/init.d/中新建文件 memcached

[root@localhost init.d]# touch memcached
[root@localhost init.d]# chmod 777 memcached

複製粘貼如下內容於 redis文件 保存退出

#! /bin/sh
#
# chkconfig: - 55 45
# description:  The memcached daemon is a network memory cache service.
# processname: memcached
# config: /etc/sysconfig/memcached

# Source function library - for other linux
. /etc/rc.d/init.d/functions
# Source function library - for suse linux
#. /lib/lsb/init-functions


ip="127.0.0.1"             # 綁定偵聽的IP地址
port="11211"                    # 服務端口
username="root"                 # 運行程序的用戶身份
max_memory=64                   # default: 64M | 最大使用內存
max_connection=1024             # default: 1024 | 最大同時鏈接數



if [ -f /etc/sysconfig/memcached ];then
  . /etc/sysconfig/memcached
fi

# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
  exit 0
fi

RETVAL=0
prog="memcached"
pidfile=/tmp/memcached.pid
basedir=/usr/local/memcached
cmd=${basedir}/bin/memcached

start () {
  echo -n $"Starting service:$prog "
  chown $username $basedir
  $cmd -d -p $port -u $username  -m $max_memory -c $max_connection -P $pidfile $OPTIONS
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached
  return $retval
}

stop() {
    echo -n $"Stopping service: $prog  "
    current_user=$(whoami)
    pidlist=$(ps -ef | grep $current_user | grep memcached | grep -v grep | awk '{print $2}')
    for pid in $pidlist ; do
        kill -9 $pid
        if [ $? -ne 0 ]; then
           return 1
        fi
    done
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/memcached
}

restart () {
  stop
  start
}

# See how we were called.
case "$1" in
  start)
  start
  ;;
  stop)
  stop
  ;;
  status)
  status memcached
  ;;
  restart|reload)
  restart
  ;;
  condrestart)
  [ -f /var/lock/subsys/memcached ] && restart || :
  ;;
  *)
  echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
  exit 1
esac

exit $?
  • 配置開啓自啓動

    chkconfig --add memcached

  • 增長開啓啓動設置啓動級別

    chkconfig --level 2345 memcached on

  • 測試啓動,中止,重啓memcached

    # 服務啓動,中止service memcached start service memcached stop
    # init.d目錄啓動,中止/etc/init.d/memcached start/etc/init.d/memcached stop


Memcache 防火牆配置

# 打開防火牆規則vim /etc/sysconfig/iptables

# 增長內容 -A INPUT -p tcp -m tcp --dport 11211 -j ACCEPT   




# 重啓火牆規則當即生效service iptables restart

Memcached 類測試

$memcached = new Memcached();
$memcached->addServer('192.168.206.128',11211);
$memcached->set('key','TK');
echo $memcached->get('key'); // TK

Memcache類測試

$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211);
$memcache ->set('key','TK');
echo $memcache ->get('key'); // TK
相關文章
相關標籤/搜索