php7 memcached.exe,PHP7 下安装 memcache 和 memcached 扩展
发布日期:2021-06-24 11:49:11 浏览次数:3 分类:技术文章

本文共 5336 字,大约阅读时间需要 17 分钟。

概述

Memcached是一种基于内存的key-value存储,用来存储小块的任意数据(字符串、对象)。这些数据可以是数据库调用、API调用或者是页面渲染的结果。

一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高可扩展性。

cee11b082904

image

memcached以守护进程的方式运行于一个或多个服务器中,随时接受客户端的连接操作。比如,php中的memcache扩展等。其存储对象并不是持久的,服务停止后,里边的数据就会丢失。另外,内存容量达到指定值后,就会使用LRU算法自动删除不使用的(或者很少使用的)缓存。

memcached作为高速运行的分布式缓存服务器,具有以下的特点。

协议简单

基于libevent的事件处理

内置内存存储方式

memcached不互相通信的分布式

Memcached 是高性能的分布式内存缓存服务器,而PHP memcache 和 memcached 都是 Memcached 服务器的 PHP 扩展。其中memcache 比 memcached 早出现,所以一些老的代码可能还在用 memcache 扩展。memcached 后来出现,并且大部分框架都支持 memcached,现在相对较流行。可以根据自己需要,安装一个就可以。

一、安装依赖

首先是 memcached,这个扩展需要 libmemcached 客户端库,否则会出现如下错误

checking for libmemcached location... configure: error: memcached support requires libmemcached. Use --with-libmemcached-dir=

ERROR: `/var/tmp/memcached/configure --with-libmemcached-dir=no' failed

可以通过如下方法安装

[root@lnmp lnmp.cn]# yum install libmemcached libmemcached-devel

而 memcache 模块使用了函数 zlib 来支持数据压缩,因此安装此模块需要安装 Zlib 模块。否则会出现如下错误:

checking for the location of zlib... configure: error: memcache support requires ZLIB. Use --with-zlib-dir=

ERROR: `/var/tmp/memcache/configure --enable-memcache-session=No' failed

可以如下方法用 yum 来安装:

[root@lnmp lnmp.cn]# yum install zlib zlib-devel

二、安装 memcached 扩展

[root@lnmp lnmp.cn]# pecl install memcached

pecl/memcached requires PHP (version >= 5.2.0, version <= 6.0.0, excluded versions: 6.0.0), installed version is 7.0.8

No valid packages found

install failed

[root@localhost vagrant]#

提示很明显,PECL 上的 memcached 扩展只支持 PHP 5.2 以上,6.00 以下的版本。还未更新到 PHP7。不过还好的是在 PECL 的 memcached 页面可以找到他们在 github 上的链接:

https://github.com/php-memcached-dev/php-memcached

这上面的代码已经有可以支持到 PHP7 的分支。这里将源码统一下载到 php 源码的 ext 目录:

[root@lnmp lnmp.cn]# cd /usr/local/src/php-7.0.8/ext/

[root@lnmp ext]# git clone https://github.com/php-memcached-dev/php-memcached memcached

[root@lnmp ext]# cd memcached/

checkout 到 php7 分支:

[root@lnmp memcached]# git checkout php7

Branch php7 set up to track remote branch php7 from origin.

Switched to a new branch 'php7'

[root@lnmp memcached]#

用 phpize 安装,我的 PHP 是安装在 /usr/local/php7 下

[root@lnmp memcached]# /usr/local/php7/bin/phpize

[root@lnmp memcached]# ./configure --with-php-config=/usr/local/php7/bin/php-config

接着 make 和 make install

[root@lnmp memcached]# make

[root@lnmp memcached]# make install

Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/

[root@lnmp memcached]#

可以看到 memcached 已经安装完成,并且扩展文件已经放到提示的目录:

[root@lnmp memcached]# ls /usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/

memcached.so opcache.a opcache.so

[root@lnmp memcached]#

最后一步在 php.ini 中引入 memcached.so

[root@lnmp memcached]# vim /usr/local/php7/lib/php.ini

加入:

extension=memcached.so

记得 reload 一下 php-fpm 才能生效

[root@lnmp memcached]# systemctl reload php-fpm

打开 phpinfo 页面,已经已经看到 memcached 扩展成功安装了。

cee11b082904

php7 memcached

三、安装 memcache 扩展

同样尝试用 PECL 来安装:

[root@lnmp memcached]# pecl install memcache

但同样失败

/tmp/pear/temp/memcache/memcache.c:40:40: fatal error: ext/standard/php_smart_str.h: No such file or directory

include "ext/standard/php_smart_str.h"

^

compilation terminated.

make: *** [memcache.lo] Error 1

ERROR: `make' failed

貌似原因也是 PECL 还不支持在 PHP7 下安装 memcache 扩展,

https://pecl.php.net/package/memcache

[root@lnmp memcached]# cd ../

[root@lnmp ext]# git clone https://github.com/websupport-sk/pecl-memcache memcache

[root@lnmp ext]# cd memcache

用 phpize 安装,步骤和 memcached 一模一样

[root@lnmp memcache]# /usr/local/php7/bin/phpize

[root@lnmp memcache]# ./configure --with-php-config=/usr/local/php7/bin/php-config

[root@lnmp memcache]# make

[root@lnmp memcache]# make install

Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/

[root@lnmp memcache]#

类似 memcached , 将 memcache.so 在 php.ini 中引入

[root@lnmp memcache]# vim /usr/local/php7/lib/php.ini

加入:

extension=memcache.so

最后 reload php-fpm

[root@lnmp memcache]# systemctl reload php-fpm

大功告成,可以在 phpinfo 页面看到 memcahce 和 memchaced 都已经成功安装

cee11b082904

php7 memcache

四、安装 Memcached 内存缓存服务器

Centos 下可以用 yum 进行安装

[root@lnmp memcache]# yum install memcached

再启动 Memcached 就可以测试 PHP 扩展了

[root@lnmp memcache]# systemctl start memcached

或编译安装 libevent 和 memcached

[root@lnmp memcache]# tar xzvf libevent-2.1.8-stable.tar.gz

[root@lnmp memcache]# cd libevent-2.1.8-stable

[root@lnmp memcache]# ./configure --prefix=/usr/local/libevent

[root@lnmp memcache]# make && make install

libevent是一套跨平台的事件处理接口的封装。memcached使用libevent来进行网络并发连接的处理,能够在很大并发的情况下,仍保持快速的响应能力。

[root@lnmp memcache]# tar xzvf memcached-1.5.14.tar.gz

[root@lnmp memcache]# cd memcached-1.5.14

[root@lnmp memcache]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent

[root@lnmp memcache]# make && make install

启动 Memcached

[root@lnmp memcache]# /usr/local/memcached/lib/memcached -d -m 128 -u memcache -p 11211 -P /run/memcached.pid

或 默认端口是 11211,默认memcached可以使用的内存大小是 64MB

[root@lnmp memcache]# /usr/local/memcached/lib/memcached -u memcache &

关闭 Memcached

[root@lnmp memcache]# kill cat /run/memcached.pid

五、Memcached 运行

Memcached命令的运行:

$ /usr/local/memcached/bin/memcached -h 命令帮助

注意:如果使用自动安装 memcached 命令位于 /usr/local/bin/memcached。

启动选项:

-d是启动一个守护进程;

-m是分配给Memcache使用的内存数量,单位是MB;

-u是运行Memcache的用户;

-l是监听的服务器IP地址,可以有多个地址;

-p是设置Memcache监听的端口,,最好是1024以上的端口;

-c是最大运行的并发连接数,默认是1024;

-P是设置保存Memcache的pid文件。

参考:

无论从事什么行业,只要做好两件事就够了, 一个是你的专业、一个是你的人品, 专业决定了你的存在,人品决定了你的人脉, 剩下的就是坚持,用善良專業和真诚赢取更多的信任。

转载地址:https://blog.csdn.net/weixin_32573931/article/details/115543691 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:计算机二级java技巧,计算机二级报java难考吗
下一篇:用Java的类来封装常见算法,JAVA–2018-8-21 类定义,类方法,对象

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月02日 09时28分02秒