安裝php
yum install -y memcached libmemcached libevent systemctl start memcached
/usr/bin/memcached -u memcached -p 11211 -m 64-c 1024 或者是 vi /etc/sysconfig/memcached
memcached-tool 127.0.0.1:11211 stats
或者mysql
echo stats |nc 127.0.0.1 11211
須要安裝nc工具 linux
yum install -y nc rpm -qf `which nc`
若安裝libmemcached後,可使用命令git
memstat --servers=127.0.0.1:11211
查看memcached服務狀態
在memcached中,運行state命令能夠查看memcached服務的狀態信息,其中cmd_get表示總的get次數,get_hits表示get的總命中次數,命中率 = get_hits/cmd_ge
總結,查看狀態三種方法web
memcached-tool 127.0.0.1:11211 stats echo stats |nc 127.0.0.1 11211 memstat --servers=127.0.0.1:11211
Memcached語法規則
<command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n
注:\r\n在windows下是Enter鍵 redis
telnet 127.0.0.1 11211 #進入memcached
set key2 0 30 2 #30(過時時間30s,若是是0就表示永不過時,2兩個字節)
ab
STORED
get key2
VALUE key2 0 2
ab
END算法
ctrl + } 退出
quit 退出 telnetsql
Memcached數據導出和導入
導出:mongodb
memcached-tool 127.0.0.1:11211 dump > data.txt
cat data.txt
導入:數據庫
nc 127.0.0.1 11211 < data.txt
若nc命令不存在,yum install nc
注意:導出的數據是帶有一個時間戳的,這個時間戳就是該條數據過時的時間點,若是當前時間已經超過該時間戳,那麼是導入不進去的
cd /usr/local/src/ wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz tar zxf memcache-2.2.3.tgz cd memcache-2.2.3 /usr/local/php-fpm/bin/phpize
遇到問題
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
解決方法:
yum install -y m4 autoconf
./configure --with-php-config=/usr/local/php-fpm/bin/php-config make && make install
安裝完後會有相似這樣的提示:Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
而後修改php.ini添加一行
vi /usr/local/php-fpm/etc/php.ini extension=memcache.so
檢查
/usr/local/php-fpm/bin/php -m /etc/init.d/php-fpm restart
curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null
1.php內容也能夠參考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/1.php
執行腳本
/usr/local/php-fpm/bin/php 1.php
或者將1.php放到某個虛擬主機根目錄下面,在瀏覽器訪問,便可看到效果
vim /usr/local/php-fpm/etc/php.ini
搜索session
如圖,路徑爲file,即保存在本地,默認的session存放在/tmp
cd /usr/local/src wget http://study.lishiming.net/.mem_se.txt mv .mem_se.txt /data/wwwroot/default/session.php 其中session.php內容能夠參考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/session.php curl localhost/session.php
php-fpm.conf對應的pool中添加
vim /usr/local/php-fpm/etc/php-fpm.conf php_value[session.save_handler] = memcache php_value[session.save_path] = " tcp://192.168.127.133:11211 "
/etc/init.d/php-fpm restart rm -rf /tmp/sess* curl localhost/session.php
memcached-tool 127.0.0.1:11211 dump > data.txt cat data.txt