memcached命令行、Memcached數據導出和導入、php鏈接memcache、php的session存儲到memcached

一、memcached命令行php

telnet 127.0.0.1 11211
set key2 0 30 2
ab
STORED
get key2
VALUE key2 0 2
ab
END
git

 如:github

set key3 1 100 4
abcd
STORED
get key3
VALUE key3 1 4
abcd
END
replace key3 1 200 5
abcdx
STORED
get key3
VALUE key3 1 5
abcdx
END
delete key3
DELETED
get key3
ENDweb

二、memcached數據導出和導入apache

 
查看狀態:
session

而後導出:
php7

導入:tcp

memcached-tool 127.0.0.1:11211 dump
memcached

注意,在導入的時候,會有過時時間,因此,在導入的時候,要調時間值,不然導入後,就沒有數據

php-fpm

三、php鏈接memcache

先安裝php的memcache擴展
cd /usr/local/src/
 wget -c http://pecl.php.net/get/memcache-2.2.7.tgz  (php5最新穩定版)
wget -c http://pecl.php.net/get/memcache-3.0.8.tgz (php5最新開發版)
wget -c https://github.com/websupport-sk/pecl-memcache/archive/php7.zip (php7可用版)
解壓:tar zxf memcache****.tgz
cd memcache***
/usr/local/php7fpm/bin/phpize
./configure --with-php-config=/usr/local/php7fpm/bin/php-config
make && make install
安裝完後會有相似這樣的提示:Installing shared extensions: /usr/local/php7fpm/lib/php/extensions/no-debug-non-zts-20170718/
而後修改php.ini添加一行echo "extension=memcache.so" >>/usr/local/php7fpm/etc/php.ini
檢查/usr/local/php7fpm/bin/php-fpm -m

php安裝memcached:

wget -c https://pecl.php.net/get/memcached-3.0.4.tgz
tar -xvf memcached-3.0.4.tgz
cd memcached-3.0.4/
/usr/local/php7fpm/bin/phpize
./configure
若缺libmemcached 須要安裝這兩個依賴包:yum install libmemcached libmemcached-devel
./configure
make && make install
echo "extension=memcached.so" >>/usr/local/php7fpm/etc/php.ini
/etc/init.d/php7fpm restart


具體相關:


測試:url www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null
/usr/local/php7fpm/bin/php 1.php

 

四、php的session存儲到memcached

編輯php.ini添加兩行
for memcache:
session.save_handler = memcache
session.save_path="tcp://192.168.0.9:11211"
for memcahed:
session.save_handler = memcached
session.save_path = "localhost:11211" 或 session.save_path = "127.0.0.1:11211"

或者httpd.conf中對應的虛擬主機中添加
for memcache:
php_value session.save_handler "memcache"

php_value session.save_path"tcp://192.168.0.9:11211"

或者php-fpm.conf對應的pool中添加

for memcache:

php_value[session.save_handler]=memcache
php_value[session.save_path]="tcp://192.168.0.9:11211"

for memcahed:

php_value[session.save_handler] = memcached
php_value[session.save_path] = "127.0.0.1:11211" 或 php_value[session.save_path] = "localhost:11211"

測試:
wget http://study.lishiming.net/.mem_se.txt
mv .mem_se.txt /usr/local/apache2/htdocs/session.php

telnet 127.0.0.1 11211

for memcahed:

 

相關文章
相關標籤/搜索