memcache 安装 (windows和linux)


windows 64bit 环境下安装

下载 memcache 安装包 :http://pan.baidu.com/s/1pLcjTzl

解压到d盘任意位置 , cmd(以管理员身份运行,否则安装失败) 执行命令

memcache 安装 (windows和linux)_第1张图片

clipboard.png

没有报错说明我们已经安装成功

下载 php_memcache.dll : https://pecl.php.net/package/memcache/3.0.8/windows

memcache 安装 (windows和linux)_第2张图片

我的是5.6的版本 所以下载 64位 TS版本

将dll扩展放入php扩展文件夹中

memcache 安装 (windows和linux)_第3张图片

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

memcache 安装 (windows和linux)_第4张图片

然后重启wampserver
注意:apache文件夹中的php.ini 是用于浏览器访问 php文件夹中的php.ini 是适用于命令行运行php脚本

php测试代码 (我的是命令行测试)

memcache 安装 (windows和linux)_第5张图片


linux 安装 (centos 64bit)

安装环境说明:

操作系统: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

你可能感兴趣的:(linux,php,windows,memcached)