memcached command Line
Enter into the memcached, if no Yum can be installed
Telnet 127.0.0.1 11211
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '.
Set Key2 0 30 2
Set: is used to store data.
Key: Here Key2 is the name of the key, because memcached belongs to Key-valux, the data to have a key, but also to have a valux.
2: The 2 in this is the value you want to save to two bits, or two bytes. For example, I input 11, the result is stored, if the input 3 characters will be an error.
The name of the Get+key can be used to see if the storage is successful, and the stored content is displayed as +end.
30: The 30 in this is the expiration time, that is, the data will disappear after 30 seconds. Using Get view will only get the result of end.
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 special purposes, 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 completes, the final client needs to add \ r \ n (directly click Enter) as the end flag
<delete> delete+ Library name to delete data
Create data
Set Key1 1 110 4
1234
STORED
Get Key1
VALUE Key1 1 4
1234
END
Replace data
Replace Key1 1 110 5
98756
STORED
Get Key1
VALUE Key1 1 5
98756
END
Add data
Add Key1 1 110 2
12
Not_stored
Add Key2 1 110 3
123
STORED
Get Key2
VALUE Key2 1 3
123
END
Delete data
Get Key6
VALUE Key6 1 3
Llt
END
Delete Key6
DELETED
Get Key6
END
memcached Data export and import
Enter data first
Set Name 1 0 5
Aming
STORED
Set age 1 0 2
20
STORED
Set K1 1 0 5
12345
STORED
Telnet> quit
Connection closed.
Then check the status: MemStat--servers=127.0.0.1:11211
server:127.0.0.1 (11211)
pid:1428
uptime:5322
time:1530712057
version:1.4.15
Libevent:2.0.21-stable
Pointer_size:64
rusage_user:0.114001
rusage_system:0.114001
Curr_connections:10
Total_connections:13
Connection_structures:11
Reserved_fds:20
Cmd_get:8
Cmd_set:10
cmd_flush:0
cmd_touch:0
Get_hits:5
Get_misses:3
delete_misses:0
Delete_hits:1
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:356
bytes_written:264
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:9
expired_unfetched:0
evicted_unfetched:0
evictions:0
Reclaimed:3
Export:
[Email protected] ~]# 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 K1 1 1530706735 5
12345
Add Name 1 1530706735 5
Aming
Add age 1 1530706735 2
20
We can also redirect the results to a file.
Memcached-tool 127.0.0.1:11211 dump > Data.txt
[[email protected] ~]# memcached-tool 127.0.0.1:11211 Dump > Data.txt
Dumping memcache contents
Number of buckets:1
number of items:3
dumping bucket 1-3 tot Al items
[[email protected] ~]# cat data.txt
Add K1 1 1530706735 5
12345
Add name 1 1530706735 5
aming
Add age 1 1530706735 2
Import: NC 127.0.0.1 11211 < data.txt
If the NC command does not exist, yum install NC
[[email protected] ~]# NC 127.0.0.1 11211 < Data.txt
not_stored
not_stored
not_stored
Here we find that the import was unsuccessful because there is still data in memcached, so it is not successful. At this point we have to reboot memcached to be able to import, this is because the memcached is in the form of memory, the database will be emptied after reboot.
[[email protected] ~]# systemctl restart memcached
[[email protected] ~]# NC 127.0.0.1 11211 < Data.txt
STORED
STORED
STORED
This will do.
then go to memcached view
[[email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '. The
Get name
END
found no data, because the import failed because the data was timestamp after the data we had everywhere.
[[email protected] ~]# cat data.txt
Add K1 1 1530706735 5
12345
Add name 1 1530706735 5
aming
Add Age 1 1530706735 2
We're going to get rid of the timestamp of the data before we can continue importing the data.
Add K1 1 10000 5
12345
Add name 1 1530706735 5
aming
Add age 1 10000 2
All
We'll get rid of the time stamp for the hour and the K1 and try again.
[[email protected] ~]# NC 127.0.0.1 11211
Get K1
VALUE K1 1 5
12345
End
Get name
End
Get Age
VALUE 1 2
END
This will successfully import the data.
PHP connection memcached
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
[[Email protected] memcache]#/usr/local/php-fpm/bin/phpize
Configuring for:
php Api version:20131106
Zend Module API no:20131226
Zend Extension API no:220131226
./configure--with-php-config=/usr/local/php-fpm/bin/ Php-config
Make && do install
will be installed in/usr/local/php-fpm/lib/php/extensions/ The no-debug-non-zts-20131226/
Directory generates memcache.so opcache.a opcache.so three files, of which memcache.so is the most important.
then modify Vim/usr/local/php-fpm/etc/php.ini
to add a line in the extension extension area extension= "memcache.so"
Check/usr/local/ Php-fpm/bin/php-m
[[email protected] memcache]#/usr/local/php-fpm/bin/php-m|grep memcache
Memcache (module already generated)
PHP connection memcached
Download Test Scripts
Curl Www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null
Execute script
/usr/local/php-fpm/bin/php 1.php
[Email protected] ~]#/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
)
Storage sessions in memcached
This example is implemented in the LAMP/LNMP environment
Add the corresponding virtual host in the httpd.conf
Php_value Session.save_handler "Memcache"
Php_value Session.save_path "tcp://127.0.0.1:11211"
or add the php-fpm.conf corresponding pool
Php_value[session.save_handler] = Memcache
Php_value[session.save_path] = "tcp://127.0.0.1:11211"
memcached command line and data export and import, PHP connection memcached, and storage to sessions