76.memcached命令行 數據導出及導入 sessions會話(php)

21.5 memcached命令行php

21.6 memcached數據導出和導入linux

21.7 php鏈接memcachednginx

21.8 memcached中存儲sessionsgit

 

 

 

21.5 memcached命令行算法

 

1.telnet 127.0.0.1 11211 #進入memcachedsql

2.set key2 0 30 2shell

set 表示存儲一條數據apache

key2 表示k的名字(k-v)vim

0 指的是flags,下面詳解(特殊需求時纔會用)windows

30 爲過時時間(秒)。若是爲0表明永不過時

2 表示你要存的數值是兩位的(或者說是兩個字節),指定了幾位就要寫幾位(好比12)

 

ab

STORED

get key2

VALUE key2 0 2

ab

END

 

Memcached語法規則

1.<command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n

注:\r\n在windows下是Enter鍵

<command name> 能夠是set, add, replace

set表示按照相應的<key>存儲該數據,沒有的時候增長,有的時候覆蓋

add表示按照相應的<key>添加該數據,可是若是該<key>已經存在則會操做失敗

replace表示按照相應的<key>替換數據,可是若是該<key>不存在則操做失敗。

2.<key> 客戶端須要保存數據的key

3.<flags> 是一個16位的無符號的整數(以十進制的方式表示)。該標誌將和須要存儲的數據一塊兒存儲,並在客戶端get數據時返回。客戶端能夠將此標誌用作特殊用途,此標誌對服務器來講是不透明的。

4.<exptime> 爲過時的時間。若爲0表示存儲的數據永遠不過時(但可被服務器算法:LRU 等替換)。若是非0(unix時間或者距離此時的秒數),當過時後,服務器能夠保證用戶得不到該數據(以服務器時間爲標準)。

5.<bytes> 須要存儲的字節數,當用戶但願存儲空數據時<bytes>能夠爲0

6.<data block>須要存儲的內容,輸入完成後,最後客戶端須要加上\r\n(直接點擊Enter)做爲結束標誌。

 

 

實例:

 

[root@axinlinux-01 ~]# telnet 192.168.208.128 11211

Trying 192.168.208.128...

Connected to 192.168.208.128.

Escape character is '^]'.

set key2 0 03 2

12

STORED

set key1 0 30 3 #若是咱們指定了他的字節是3,寫入12就不對了

12

 

CLIENT_ERROR bad data chunk

ERROR

set key1 0 30 3 #指定了字節爲3,就要寫3位

abc

STORED

get key1 #咱們查看key1就沒有的,由於已通過了指定的時間30秒

END

set key1 0 30 3 #咱們再插入一個key1

122

STORED

get key1 #用get查看他,就會獲得相應的數據

VALUE key1 0 3

122

END

演習:

set key3 1 1000 4 #先set key3

abcd

STORED

replace key3 1 0 5 #咱們作一個替換(key3),設置他永不過時

abcde

STORED

get key3

VALUE key3 1 5 #咱們再來get key3就是咱們替換之後的值

abcde

END

delete key3 #咱們再把key3刪除

DELETED

get key3 #在get就沒有了

END

 

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

21.6 memcached數據導出和導入

 

 

 

先前提到把mencached重啓的時候,要把數據存儲一下。啓動後,再把數據導回去

1.導出:

memcached-tool 127.0.0.1:11211 dump > data.txt

cat data.txt

2.導入:

nc 127.0.0.1 11211 < data.txt

若nc命令不存在,yum install nc

注意:導出的數據是帶有一個時間戳的,這個時間戳就是該條數據過時的時間點,若是當前時間已經超過該時間戳,那麼是導入不進去的

 

 

實例:

set name 1 0 6 #先寫幾條數據

amings

STORED

set age 1 0 2

22

STORED

set axin 1 0 10

1234567899

STORED

^] #退出的話,按ctrl+],在quit退出

telnet> quit

Connection closed.

[root@axinlinux-01 ~]# memcached-tool 192.168.208.128:11211 stats #看一下他的狀態

#192.168.208.128:11211 Field Value

accepting_conns 1

auth_cmds 0

auth_errors 0

bytes 225

bytes_read 282

bytes_written 178

cas_badval 0

cas_hits 0

cas_misses 0

cmd_flush 0

cmd_get 5 #get了5次

cmd_set 9 #set了9個 ,這都是咱們以上作的操做

cmd_touch 0

conn_yields 0

connection_structures 11

curr_connections 10

curr_items 3

decr_hits 0

decr_misses 0

delete_hits 1

delete_misses 0

evicted_unfetched 0

evictions 0

expired_unfetched 1

get_hits 2

get_misses 3

hash_bytes 524288

hash_is_expanding 0

hash_power_level 16

incr_hits 0

incr_misses 0

libevent 2.0.21-stable

limit_maxbytes 67108864

listen_disabled_num 0

pid 1697

pointer_size 64

reclaimed 2

reserved_fds 20

rusage_system 0.111099

rusage_user 0.152030

threads 4

time 1541686324

total_connections 12

total_items 8

touch_hits 0

touch_misses 0

uptime 2180

version 1.4.15

[root@axinlinux-01 ~]# memcached-tool 192.168.208.128:11211 dump > data.txt #這樣來導出

Dumping memcache contents

Number of buckets: 1

Number of items : 3

Dumping bucket 1 - 3 total items

[root@axinlinux-01 ~]# ls #存在了當前目錄下

22.txt anaconda-ks.cfg data.txt logs rsync2.test shell

[root@axinlinux-01 ~]# cat data.txt

add axin 1 1541684144 10

1234567899

add name 1 1541684144 6 #這裏爲add

amings

add age 1 1541684144 2

22

[root@axinlinux-01 ~]# systemctl restart memcached #咱們重啓一下,數據會清空

[root@axinlinux-01 ~]# telnet 192.168.208.128 11211

Trying 192.168.208.128...

Connected to 192.168.208.128.

Escape character is '^]'.

set name

ERROR

quit

Connection closed by foreign host.

[root@axinlinux-01 ~]# nc 192.168.208.128 11211 < data.txt #這樣來導入

STORED

STORED

STORED

導出時系統會記錄一個時間戳,咱們在這個時間戳以後導入,就會過時(也就是進去後沒有數據)

[root@axinlinux-01 ~]# date -d "+1 hour" +%s #咱們能夠日後加一個小時的時間戳

1541691370

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

21.7 php鏈接memcached(爲下面的sessions會話爲準備)

 

php-fpm須要有一個模塊(memcache)來鏈接memcached,這個模塊能夠理解爲他們的中間件。

1.先安裝php的memcache擴展(模塊)

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 #這一步是要生成configure文件,要用他來編譯

./configure --with-php-config=/usr/local/php-fpm/bin/php-config

make && make install

2.安裝完後會有相似這樣的提示:Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/。咱們須要這個路徑下的memcache.so的文件

3.而後修改php.ini添加一行extension=memcache.so

4.檢查/usr/local/php/bin/php-fpm -m

 

測試

下載測試腳本

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放到某個虛擬主機根目錄下面,在瀏覽器訪問,便可看到效果

最終能夠看到數據以下:

[0] => aaa

[1] => bbb

[2] => ccc

[3] => ddd

 

 

實例

[root@axinlinux-01 ~]# cd /usr/local/src/

[root@axinlinux-01 src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz

[root@axinlinux-01 src]# tar xzf memcache-2.2.3.tgz

[root@axinlinux-01 src]# cd memcache-2.2.3/

[root@axinlinux-01 memcache-2.2.3]# ls #看一下並無用來編譯的config文件

config9.m4 config.w32 example.php memcache_consistent_hash.c memcache_queue.c memcache_session.c php_memcache.h

config.m4 CREDITS memcache.c memcache.dsp memcache_queue.h memcache_standard_hash.c README

[root@axinlinux-01 memcache-2.2.3]# /usr/local/php-fpm/bin/phpize #因此要執行一下phpize,來生成一下config文件

Configuring for:

PHP Api Version: 20131106

Zend Module Api No: 20131226

Zend Extension Api No: 220131226

[root@axinlinux-01 memcache-2.2.3]# ls #再次查看就會發現已經有了config文件

acinclude.m4 config9.m4 config.sub CREDITS Makefile.global memcache_queue.c missing run-tests.php

aclocal.m4 config.guess configure example.php memcache.c memcache_queue.h mkinstalldirs

autom4te.cache config.h.in configure.in install-sh memcache_consistent_hash.c memcache_session.c php_memcache.h

build config.m4 config.w32 ltmain.sh memcache.dsp memcache_standard_hash.c README

[root@axinlinux-01 memcache-2.2.3]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config #只須要指定這一個就能夠了

[root@axinlinux-01 memcache-2.2.3]# echo $?

0

[root@axinlinux-01 memcache-2.2.3]# make

Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/

[root@axinlinux-01 memcache-2.2.3]# echo $?

0

[root@axinlinux-01 memcache-2.2.3]# make install

Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/

#在提示的這個路徑下,就生成了一個memcache.so的文件。咱們要的就是他

[root@axinlinux-01 memcache-2.2.3]# echo $?

0

[root@axinlinux-01 memcache-2.2.3]# ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/

memcache.so opcache.a opcache.so

[root@axinlinux-01 memcache-2.2.3]# vim /usr/local/php-fpm/etc/php.ini

;extension=php_pdo_pgsql.dll

;extension=php_pdo_sqlite.dll

;extension=php_pgsql.dll

;extension=php_shmop.dll

extension=memcache.so #在這個區域加入這一行。以上這些就是擴展模塊的區域

 

; The MIBS data available in the PHP distribution must be installed.

; See http://www.php.net/manual/en/snmp.installation.php

;extension=php_snmp.dll

[root@axinlinux-01 memcache-2.2.3]# /usr/local/php-fpm/sbin/php-fpm -m #在來看一下有沒有這個模塊

[PHP Modules]

。。。

memcache

測試

[root@axinlinux-01 memcache-2.2.3]# curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null

[root@axinlinux-01 memcache-2.2.3]# cat 1.php

[root@axinlinux-01 memcache-2.2.3]# /usr/local/php-fpm/bin/php 1.php

Get key1 value: This is first value<br>Get key1 value: This is replace value<br>Get key2 value: Array

(

[0] => aaa

[1] => bbb

[2] => ccc

[3] => ddd

)

<br>Get key1 value: <br>Get key2 value: <br>[root@axinlinux-01 memcache-2.2.3]#

#出現以上,即爲成功

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

21.8 memcached中存儲sessions

 

 

應用場景:lnmp的架構中負載均衡,爲了使用戶的登陸狀態在一臺服務器上,能夠使用upstream,ip_hash來解決。那要是使用lvs負載均衡呢?就能夠把sessions不在存儲到服務器磁盤上,而是存到memcached裏面去。memcached能夠做爲一個公共的服務器,訪問的時候使用其中某一個內網ip,而不是127.0.0.1啦

那怎麼在php中指定他的,怎麼把sessions存到memcached服務裏面去呢?如下是幾種方法:

1.

本實例是在lamp/lnmp環境下實現

方法1.(不成功,不建議使用)

編輯php.ini添加兩行

session.save_handler = memcache 

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

方法2. (httpd使用)

或者httpd.conf中對應的虛擬主機中添加

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

方法3.

或者php-fpm.conf對應的pool中添加(nginx能夠使用)

php_value[session.save_handler] = memcache

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

 

2.

wget http://study.lishiming.net/.mem_se.txt

mv .mem_se.txt /usr/local/apache2/htdocs/session.php

其中session.php內容能夠參考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/session.php

3.

curl localhost/session.php

相似於1443702394<br><br>1443702394<br><br>i44nunao0g3o7vf2su0hnc5440

4.

telnet 127.0.0.1 11211

5.

get i44nunao0g3o7vf2su0hnc5440

 

實例:(針對第三種方法)

[root@axinlinux-01 vhost]# cd /usr/local/php-fpm/etc/php-fpm.d/

[root@axinlinux-01 php-fpm.d]# ls

axin.conf.bak www.conf

[root@axinlinux-01 php-fpm.d]# vim www.conf

php_value[session.save_handler] = memcache #直接在最下面加入

php_value[session.save_path] = " tcp://192.168.208.128:11211 "

[root@axinlinux-01 php-fpm.d]# /etc/init.d/php-fpm restart

Gracefully shutting down php-fpm . done

Starting php-fpm done

[root@axinlinux-01 ~]# wget http://study.lishiming.net/.mem_se.txt

[root@axinlinux-01 ~]# mv .mem_se.txt /data/wwwroot/default/index.php

[root@axinlinux-01 ~]# cat /data/wwwroot/default/index.php

<?php

session_start();

if (!isset($_SESSION['TEST'])) {

$_SESSION['TEST'] = time();

}

$_SESSION['TEST3'] = time();

print $_SESSION['TEST'];

print "<br><br>";

print $_SESSION['TEST3'];

print "<br><br>";

print session_id();

?>

[root@axinlinux-01 php-fpm.d]# curl localhost/index.php

1541775242<br><br>1541775242<br><br>1v3tr7dji2d65vagi9ft2tpbl3 #正常的話是有後面這個數值的,若是沒有就表明不成功。這個數值就是Key

[root@axinlinux-01 php-fpm.d]# telnet 192.168.208.128 11211 #進入memcached

Trying 192.168.208.128...

Connected to 192.168.208.128.

Escape character is '^]'.

get 1v3tr7dji2d65vagi9ft2tpbl3 #get上面的那個KEY 。若是get不出來,退出memcached多試幾回curl的操做

VALUE 1v3tr7dji2d65vagi9ft2tpbl3 0 37

TEST|i:1541775242;TEST3|i:1541775242; #這個值是他的VALUE

END

 

 

總結:

要想把sessions存儲到memcached裏面去,

第一步要去下載php的擴展包(php鏈接memcached那一部分)

第二部就是將sessions增長到memcached裏面去

相關文章
相關標籤/搜索