## **概述**
Memcached是一種基于內存的key-value存儲,用來存儲小塊的任意數據(字符串、對象)。這些數據可以是數據庫調用、API調用或者是頁面渲染的結果。
一般的使用目的是,通過緩存數據庫查詢結果,減少數據庫訪問次數,以提高動態Web應用的速度、提高可擴展性。
](images/screenshot_1606455285339.png)
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=<DIR> to specify the prefix where libmemcached headers and library are located
> 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=<DIR> to specify prefix where ZLIB include and library are located
> ERROR: `/var/tmp/memcache/configure --enable-memcache-session=No' failed
可以如下方法用 yum 來安裝:
> [root@lnmp lnmp.cn]#?yum install zlib zlib-devel
## **二、安裝 memcached 擴展**
嘗試用 PECL 安裝,memcached 在 PECL 上的地址是:
[https://pecl.php.net/package/memcached](https://pecl.php.net/package/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](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 擴展成功安裝了。

##
**三、安裝 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](https://pecl.php.net/package/memcache)
2013年以來為更新過。此路不通只能另想辦法,同樣是到 github 上碰碰運氣。搜索 pecl memcache
[https://github.com/search?utf8=%E2%9C%93&q=pecl+memcache&type=Repositories&ref=searchresults](https://github.com/search?utf8=%E2%9C%93&q=pecl+memcache&type=Repositories&ref=searchresults)
其中第一個([https://github.com/websupport-sk/pecl-memcache](https://github.com/websupport-sk/pecl-memcache))就是想要的,并且代碼已經支持到 PHP7,立即下載代碼編譯:
> [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 都已經成功安裝

?window下:[https://github.com/nono303/PHP7-memcache-dll](https://github.com/nono303/PHP7-memcache-dll)
## **四、安裝 Memcached 內存緩存服務器**
Centos 下可以用?**yum**?進行安裝
> [root@lnmp memcache]# yum install memcached
再啟動 Memcached 就可以測試 PHP 擴展了
> [root@lnmp memcache]# systemctl start memcached
?或**編譯**安裝?libevent?和?memcached
> [root@lnmp memcache]#?wget?https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz
>
> [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]# wget?https://memcached.org/files/memcached-1.5.14.tar.gz
>
> [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文件。
參考*:*
*[http://www.lnmp.cn/install-memcache-and-memcached-extends-under-php7.html](http://www.lnmp.cn/install-memcache-and-memcached-extends-under-php7.html)*
[http://www.shouce.ren/api/view/a/6424](http://www.shouce.ren/api/view/a/6424)
無論從事什么行業,只要做好兩件事就夠了, 一個是你的專業、一個是你的人品, 專業決定了你的存在,人品決定了你的人脈, 剩下的就是堅持,用善良專業和真誠贏取更多的信任。
- 技能知識點
- 對死鎖問題的理解
- 文件系統原理:如何用1分鐘遍歷一個100TB的文件?
- 數據庫原理:為什么PrepareStatement性能更好更安全?
- Java Web程序的運行時環境到底是怎樣的?
- 你真的知道自己要解決的問題是什么嗎?
- 如何解決問題
- 經驗分享
- GIT的HTTP方式免密pull、push
- 使用xhprof對php7程序進行性能分析
- 微信掃碼登錄和使用公眾號方式進行掃碼登錄
- 關于curl跳轉抓取
- Linux 下配置 Git 操作免登錄 ssh 公鑰
- Linux Memcached 安裝
- php7安裝3.4版本的phalcon擴展
- centos7下php7.0.x安裝phalcon框架
- 將字符串按照指定長度分割
- 搜索html源碼中標簽包的純文本
- 更換composer鏡像源為阿里云
- mac 隱藏文件顯示/隱藏
- 谷歌(google)世界各國網址大全
- 實戰文檔
- PHP7安裝intl擴展和linux安裝icu
- linux編譯安裝時常見錯誤解決辦法
- linux刪除文件后不釋放磁盤空間解決方法
- PHP開啟異步多線程執行腳本
- file_exists(): open_basedir restriction in effect. File完美解決方案
- PHP 7.1 安裝 ssh2 擴展,用于PHP進行ssh連接
- php命令行加載的php.ini
- linux文件實時同步
- linux下php的psr.so擴展源碼安裝
- php將字符串中的\n變成真正的換行符?
- PHP7 下安裝 memcache 和 memcached 擴展
- PHP 高級面試題 - 如果沒有 mb 系列函數,如何切割多字節字符串
- PHP設置腳本最大執行時間的三種方法
- 升級Php 7.4帶來的兩個大坑
- 不同域名的iframe下,fckeditor在chrome下的SecurityError,解決辦法~~
- Linux find+rm -rf 執行組合刪除
- 從零搭建Prometheus監控報警系統
- Bug之group_concat默認長度限制
- PHP生成的XML顯示無效的Char值27消息(PHP generated XML shows invalid Char value 27 message)
- XML 解析中,如何排除控制字符
- PHP各種時間獲取
- nginx配置移動自適應跳轉
- 已安裝nginx動態添加模塊
- auto_prepend_file與auto_append_file使用方法
- 利用nginx實現web頁面插入統計代碼
- Nginx中的rewrite指令(break,last,redirect,permanent)
- nginx 中 index try_files location 這三個配置項的作用
- linux安裝git服務器
- PHP 中運用 elasticsearch
- PHP解析Mysql Binlog
- 好用的PHP學習網(持續更新中)
- 一篇寫給準備升級PHP7的小伙伴的文章
- linux 安裝php7 -系統centos7
- Linux 下多php 版本共存安裝
- PHP編譯安裝時常見錯誤解決辦法,php編譯常見錯誤
- nginx upstream模塊--負載均衡
- 如何解決Tomcat服務器打開不了HOST Manager的問題
- PHP的內存泄露問題與垃圾回收
- Redis數據結構 - string字符串
- PHP開發api接口安全驗證
- 服務接口API限流 Rate Limit
- php內核分析---內存管理(一)
- PHP內存泄漏問題解析
- 【代碼片-1】 MongoDB與PHP -- 高級查詢
- 【代碼片-1】 php7 mongoDB 簡單封裝
- php與mysql系統中出現大量數據庫sleep的空連接問題分析
- 解決crond引發大量sendmail、postdrop進程問題
- PHP操作MongoDB GridFS 存儲文件,如圖片文件
- 淺談php安全
- linux上keepalived+nginx實現高可用web負載均衡
- 整理php防注入和XSS攻擊通用過濾