21.5-21.8 memcached command line, export and import, PHP connection memcached, storage session

Source: Internet
Author: User
Tags curl fpm php script

21.5 memcached Command Line

Create data

Yum install-y telnet

Connecting the memcached database with the telnet command

Telnet 127.0.0.1 11211

#写入数据

Set Key2 0 30 2

12

STORED


This is the wrong demonstration, since 0 30 already exists.

Set key 0 30 3

12


Client_error Bad Data chunk

ERROR

Detecting storage Status

#利用get Key Value View

Get key

END

Get Key2

END

#因为定义了过期时间, and has expired, so get key is no data displayed


memcached Grammar Rules

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

Note: \ r \ n is the ENTER key under Windows

<command name> can be set, add, replace

Set means that the data is stored according to the corresponding <key>, no time is added, sometimes it is overwritten

Add means that the data is added according to the appropriate <key>, but the operation fails if the <key> already exists

Replace means that the data is replaced by the appropriate <key>, but the operation fails if the <key> does not exist.

<key> client needs to save the key to the data

<flags> is a 16-bit unsigned integer (expressed in decimal notation). The flag is stored along with the data that needs to be stored and is returned when the client get data. The client can use this flag for a special purpose, and this flag is opaque to the server.

<exptime> is an expired time. If 0 means that the stored data is never expired (but can be replaced by the server algorithm: LRU, etc.). If it is not 0 (Unix time or the number of seconds in the distance), when it expires, the server can guarantee that the user will not get the data (server time is the standard).

<bytes> the number of bytes that need to be stored <bytes> can be 0 when the user wants to store empty data

<data block> needs to store the content, after the input is completed, the last client needs to add \ r \ n (directly click Enter) as the end flag.


memcached Data Sample

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

END


21.6 memcached Data export and import

Mamcached-tool command enables data export import

1 telnet in first. Create Test Data memcached database

Set Name 1 0 5

Kevin

STORED

Set age 1 0 2

18

STORED

Set Sex 1 0 4

Male

STORED


2 Exit telnet:ctrl+]

^]Ctrl +]

telnet> quit enter quit command quit

Connection closed. exit


3 View memcached Status

MemStat--servers=127.0.0.1:11211

server:127.0.0.1 (11211)

pid:617

uptime:79684

time:1530688689

version:1.4.15

Libevent:2.0.21-stable

Pointer_size:64

rusage_user:1.421258

rusage_system:0.940525

Curr_connections:10

Total_connections:17

Connection_structures:11

Reserved_fds:20

Cmd_get:13

Cmd_set:16

cmd_flush:0

cmd_touch:0

Get_hits:8

Get_misses:5

Delete_misses:2

Delete_hits:2

incr_misses:0

incr_hits:0

decr_misses:0

decr_hits:0

cas_misses:0

cas_hits:0

cas_badval:0

touch_hits:0

touch_misses:0

auth_cmds:0

auth_errors:0

bytes_read:716

bytes_written:10871

limit_maxbytes:67108864

Accepting_conns:1

listen_disabled_num:0

Threads:4

conn_yields:0

Hash_power_level:16

hash_bytes:524288

hash_is_expanding:0

bytes:216

Curr_items:3

Total_items:12

expired_unfetched:0

evicted_unfetched:0

evictions:0

Reclaimed:2


Where cmd_set:16 is the number of data items that were just created

Delete_hits:2 items that have just been deleted


4 Exporting the data

Memcached-tool 127.0.0.1:11211 Dump

Dumping Memcache Contents

Number of Buckets:1

Number of Items:3

Dumping Bucket 1-3 Total items

Add Name 1 1530609005 5

Kevin

Add age 1 1530609005 2

18

Add Sex 1 1530609005 4

Male

If you do not add a directional action, the preview effect is displayed

Export redirection to the specified file

Memcached-tool 127.0.0.1:11211 dump > Mem_data0704.txt

Dumping Memcache Contents

Number of Buckets:1

Number of Items:3

Dumping Bucket 1-3 Total items


Cat!$

Cat Mem_data0704.txt

Add Name 1 1530609005 5

Kevin

Add age 1 1530609005 2

18

Add Sex 1 1530609005 4

Male


5 Importing a database from a file

NC 127.0.0.1 11211 < mem_data0704.txt

Not_stored

Not_stored

Not_stored

not_stored reason, because the data just created is add in, so the data exists, and then import is the overwrite operation, meaningless,

So you need to restart the Memcached service to clear the database, do not forget that the memcached capacity is stored in memory, the service restarts, the data will be emptied.

Systemctl Restart memcached

Import the data again, the import succeeds.

NC 127.0.0.1 11211 < mem_data0704.txt

STORED

STORED

STORED

Note: The exported data is a timestamp, which is the time when the data expires, if the current time has exceeded the timestamp, then it is not imported

You need to modify the timestamp of the exported file in order for the data to be kept for a long time.

For example, to add a time stamp of one day, remove the timestamp value of 1 days first

Date-d "+1 Day" +%s

1530776745

See what the file stamp is for the export time

Cat Mem_data0704.txt

Add Name 1 1530609005 5

Kevin

Add age 1 1530609005 2

18

Add Sex 1 1530609005 4

Male

Modify the ideal timestamp

Sed-i ' s/1530609005/1530776745/g ' mem_data0704.txt

Cat Mem_data0704.txt

Add Name 1 1530776745 5

Kevin

Add age 1 1530776745 2

18

Add Sex 1 1530776745 4

Male

Import the database again,

NC 127.0.0.1 11211 < mem_data0704.txt

STORED

STORED

STORED

Enter the database again to see if the data exists

Telnet 127.0.0.1 11211

Trying 127.0.0.1 ...

Connected to 127.0.0.1.

Escape character is ' ^] '.

Get Name

VALUE Name 1 5

Kevin

END

Get age

VALUE Age 1 2

18

END

Get Sex

VALUE Sex 1 4

Male

END

Import succeeded, done.


Q&a:

Why is it that when you build a library, the value of the defined expiration time is different from the actual, and when the data is exported it becomes a timestamp value?

This is because the system specifies the expiration time when the data is created, and the exported file is exported from memcached.


21.7 PHP Connection memcached

Install the PHP memcache extension first

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--with-php-config=/usr/local/php-fpm/bin/php-config

Make && make install

After installation, there will be a hint like this: Installing Shared extensions:/usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/

Vim/usr/local/php-fpm/etc/php.ini

Find extension=

Add a line extension= "memcache.so"

Check if memcached was added.

/usr/local/php/bin/php-fpm-m

/usr/local/php-fpm/sbin/php-fpm-m

[PHP Modules]

cgi-fcgi

Core

CType

Curl

Date

Dom

Ereg

Exif

FileInfo

Filter

Ftp

Gd

Hash

Iconv

Json

Libxml

Mbstring

MCrypt

Memcache

Mysql

Mysqli

Openssl

Pcre

Pdo

Pdo_mysql

Pdo_sqlite

Phar

Posix

Reflection

Session

SimpleXML

Soap

Spl

Sqlite3

Standard

Tokenizer

Xml

XmlReader

XmlWriter

Zlib


Download Test Scripts

Curl Www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null

1.php content can also refer to https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/1.php

Execute script

/usr/local/php-fpm/bin/php 1.php

Or put 1.php into a virtual host root directory, in the browser access, you can see the effect

Finally, you can see the following data:

[0] = AAA

[1] = BBB

[2] = = CCC

[3] = DDD


21.8 memcached in storage session

See how the session is stored

Vim/usr/local/php-fpm/etc/php.ini

Session.save_handler = Files

Files indicate that sesision is stored in a local/tmp/, and each session produces a file

The test is as follows:

Pre-test file ls status

ls-lt/tmp/

Srw-rw-rw-1 root root 0 Jul 4 18:18 aming.sock

Srw-rw-rw-1 root root 0 Jul 4 18:18 php-fcgi.sock

Srw-rw-rw-1 root root 0 Jul 4 17:29 aaa.sock

Drwxr-xr-x 2 root root 4096 Jul 2 10:59 hsperfdata_root

srwxrwxrwx 1 mysql MySQL 0 June 15:48 Mysql.sock

-rw-r--r--1 root root 1283 June 18:02 Fuxi.com_access.log

Srwxr-xr-x 1 root root 0 June 04:06 Aegis-<guid (5A2C30A2-A87D-490A-9281-6765EDAD7CBA) >

drwx------3 root root 4096 Apr 14:24 Systemd-private-f7277ce00b84475293449af32306aba8-ntpd.service-bg2rez


Create a PHP script in the virtual host directory

cd/data/wwwroot/

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

mv. Mem_se.txt 11.php

Curl localhost/11.php

1530699618<br><br>1530699618<br><br>vt380k4sru58kad7k7qfsio8a6

Curl localhost/11.php

1530699618<br><br>1530699618<br><br>vt380k4sru58kad7k7qfsio8a6

Curl localhost/11.php

1530699618<br><br>1530699618<br><br>vt380k4sru58kad7k7qfsio8a6

Curl 3 times, found that more than 3 PHP-FPM generated temporary files, this is the session file to find

ls-lt/tmp/

Total 24

-RW-------1 php-fpm php-fpm 4 18:25 Sess_0j4a3r7b5ec00668rn0hjsonk4

-RW-------1 php-fpm php-fpm 4 18:20 sess_vt380k4sru58kad7k7qfsio8a6

-RW-------1 php-fpm php-fpm 4 18:18 SESS_JBV3FIHDERVAT4DCEO879MCCS7

Srw-rw-rw-1 root root 0 Jul 4 18:18 aming.sock

Srw-rw-rw-1 root root 0 Jul 4 18:18 php-fcgi.sock

Srw-rw-rw-1 root root 0 Jul 4 17:29 aaa.sock

Drwxr-xr-x 2 root root 4096 Jul 2 10:59 hsperfdata_root

srwxrwxrwx 1 mysql MySQL 0 June 15:48 Mysql.sock

-rw-r--r--1 root root 1283 June 18:02 Fuxi.com_access.log

Srwxr-xr-x 1 root root 0 June 04:06 Aegis-<guid (5A2C30A2-A87D-490A-9281-6765EDAD7CBA) >

drwx------3 root root 4096 Apr 14:24 Systemd-private-f7277ce00b84475293449af32306aba8-ntpd.service-bg2rez


To modify the storage path for these session files, you need to do the following.

To save the session file to memcached, do the following:

Php-fpm.conf the corresponding pool is added

Php_value[session.save_handler] = Memcache

Php_value[session.save_path] = "Tcp://ip:port"

Restart PHP

/ETC/INIT.D/PHP-FPM restart

Delete the session file under/tmp/

Rm-f/tmp/se*

Again enter can see the session file is not loaded under/tmp

ls-lt/tmp/

Perform Curl Testing

Curl localhost/11.php

1530701432<br><br>1530701432<br><br>e75l0ab8qpp36oapkmun763h53

E75L0AB8QPP36OAPKMUN763H53 is the key value, with the key value is the normal output, if no key value is an error output

Go to memcached View K-v

Telnet 127.0.0.1 11211

Trying 127.0.0.1 ...

Connected to 127.0.0.1.

Escape character is ' ^] '.

Get e75l0ab8qpp36oapkmun763h53

VALUE e75l0ab8qpp36oapkmun763h53 0 37

test|i:1530701432; test3|i:1530701432;

END

Normal output, done.







21.5-21.8 memcached command line, export and import, PHP connection memcached, storage session

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.