memcached Installation and application

Source: Internet
Author: User
Tags cas curl fpm memcached zts

21.1Nosql Introduction

What is NoSQL

Non-relational databases are NoSQL, relational databases represent MySQL
For the relational database, it is necessary to store the data in the library, table, row, field, query the time according to the conditions of a line to match, when the equivalent is very large time and resources, especially the data needs to be retrieved from the disk
NoSQL Database Storage principle is very simple (the typical data type is k-v), there is no complex chain of relationships, such as MySQL query, you need to find the corresponding library, table (usually multiple tables) and fields.
NoSQL data can be stored in memory with very fast query speed
NoSQL can outperform relational databases in performance, but it doesn't completely replace relational databases
NoSQL because there is no complex data structure, the extension is very easy, support distributed
Common NoSQL databases

K-v form: memcached, Redis is suitable for storing user information, such as session, configuration file, parameters, shopping cart and so on. This information is usually linked to the ID (key), which is a good choice for a key-value database.
Document database: MongoDB stores the data as a document. Each document is a collection of a series of data items. Each data item has a name and corresponding value, which can be a simple data type, such as a string, a number, a date, and so on, or a complex type, such as a sequence table and an associated object. The smallest unit of data storage is a document, and document properties stored in the same table can be different, and data can be stored in many forms, such as XML, JSON, or JSONB.
Column Store Hbase
Figure neo4j, Infinite graph, Orientdb
21.2memcached Introduction

System environment: CentOS 7.x86_64

Memcached is a foreign community website LiveJournal team developed to improve dynamic Web site performance by reducing database access times by caching database query results.
Official site http://www.memcached.org/
Simple data structure (K-V), data stored in memory
Multithreading
Simple protocol based on C/s architecture
Libevent-based event handling
Autonomic memory storage processing (slab allowcation)
Data expiration methods: Lazy Expiration and LRU
Slab allowcation Principle

Divide the allocated memory into blocks of various sizes (chunk) and divide the same size blocks into groups (chunk collections), each chunk collection is called slab.
The memcached memory allocation is in page, and the page default value is 1M, which can be specified at startup by the-I parameter.
Slab are made up of multiple page, and the page is cut into multiple chunk by the specified size.
Mark

Growth factor

Memcached can specify the growth factor factor at startup by using the-f option. This value controls the difference in chunk size. The default value is 1.25.
Viewing the different slab states of a specified memcached instance with the Memcached-tool command, you can see that each item occupies a size (chunk size) gap of 1.25
Command: # memcached-tool 127.0.0.1:11211 Display
memcached How data expires

Lazy Expiration
Memcached internally does not monitor whether the record is out of date, but instead looks at the timestamp of the record at get and checks whether the record is out of date. This technique is called lazy (lazy) expiration. As a result, memcached does not consume CPU time on outdated monitoring.
Lru
Memcached takes precedence over the space of a record that has timed out, but even so, there is a lack of space when appending a new record, and a space is allocated using the least recently used (LRU) mechanism. As the name implies, this is the mechanism for deleting "least recently used" records. Therefore, when there is insufficient memory space (when a new space cannot be obtained from the Slab Class), it is searched from records that have not been used recently, and its space is allocated to the new record. From a practical point of view of caching, the model is ideal.
21.3 Installing memcached

Official version:
Mark

This experiment uses the system comes with version: Memcached.x86_64 1.4.15-10.el7_3.1

Installation

[email protected] ~]# Yum install-y memcached libmemcached libevent
#memcached依赖于libevent和libmemecached, if the system does not have both packages installed, it needs to be installed manually.
Start

[Email protected] ~]# systemctl start memcached

[[Email protected] ~]# PS aux |grep memcached
memcach+ 2363 0.7 0.2 325556 1192? SSL 16:15 0:00/usr/bin/memcached-u memcached-p 11211-m 64-c 1024
Root 2370 0.0 0.1 112664 972 pts/0 s+ 16:15 0:00 grep--color=auto memcached
Description
-U: Indicates the specified user
-P: Indicates the listening port
-M: Indicates allocated memory
-C: Represents the maximum number of concurrent
[Email protected] ~]# NETSTAT-LNTP |grep memcached
TCP 0 0 0.0.0.0:11211 0.0.0.0: LISTEN 2363/memcached
TCP6 0 0::: 11211::
: LISTEN 2363/memcached
memcached Startup parameter configuration:

[Email protected] ~]# vim/etc/sysconfig/memcached
Port= "11211"
User= "Memcached"
maxconn= "1024"
Cachesize= "64"
Options= ""
Description: Options can be customized, and you can use "memcached-h" to view its options.

21.4 View the status of Memcached

Method 1:
[Email protected] ~]# Memcached-tool 127.0.0.1:11211 stats

Method 2: (provided that the libmemcached package is installed)
[Email protected] ~]# MemStat--servers=127.0.0.1:11211

Method 3:
[[Email protected] ~]# telnet 127.0.0.1 11211
#连接memcached
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '.
Stats
#查看memcached的状态
STAT PID 1277 (Process ID)
STAT uptime 176145 (server running seconds, seconds)
STAT time 1448592684 (server current UNIX timestamp)
STAT version 1.4.15 (server version)
STAT libevent 2.0.21-stable
STAT pointer_size 64 (OS Word size 32/64 bit)
STAT rusage_user 3.869494 (Process cumulative user time, seconds)
STAT Rusage_system 4.636292 (Process cumulative system time, seconds)
STAT curr_connections 11 (number of currently open connections)
STAT total_connections 21 (total number of connections that have been opened)
STAT Connection_structures 12 (number of connection structures allocated by the server)
STAT Reserved_fds 20
STAT Cmd_get 483 (total number of execute get commands)
STAT Cmd_set 144 (total number of execute SET commands)
STAT Cmd_flush 0 (point to flush_all command total)
STAT Cmd_touch 0
STAT get_hits 369 (get hit Count)
STAT get_misses (Get Miss Count)
STAT delete_misses 0 (delete misses)
STAT delete_hits 0 (delete hit count)
STAT incr_misses 0 (incr misses)
STAT incr_hits 0 (incr hit count)
STAT decr_misses 0 (Decr misses)
STAT decr_hits 0 (DECR hit count)
STAT cas_misses 0 (CAS misses)
STAT cas_hits 0 (CAS hit count)
STAT Cas_badval 0 (using wipe count)
STAT Touch_hits 0
STAT touch_misses 0
STAT Auth_cmds 0
STAT auth_errors 0
STAT bytes_read 82819 (total bytes read)
STAT bytes_written 104266 (total bytes written)
STAT limit_maxbytes 67108864 (Bytes allocated)
STAT Accepting_conns 1 (number of connections currently accepted)
STAT Listen_disabled_num 0
STAT Threads 4 (number of threads)
STAT Conn_yields 0
STAT Hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 4789 (store item bytes)
STAT Curr_items (item number)
STAT Total_items 144 (item total)
STAT expired_unfetched 15
STAT evicted_unfetched 0
STAT Evictions 0 (Gets the total number of space-deleted item)
STAT reclaimed 36
END
Quit
#退出
Connection closed by foreign host.
Note: The ratio of get_hits (get hit Count) and Curr _items (number of current items) is a constant concern, which represents its hit ratio.

To view the memcached status using the NC command

[email protected] ~]# Yum install-y NC
[Email protected] ~]# echo stats |nc 127.0.0.1 11211
21.5 memcached Command Line

[[Email protected] ~]# telnet 127.0.0.1 11211
#连接到memcached
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '.
# # #按Ctrl +] or enter quit to exit
Set Adai 0 30 2
#添加数据
Ab
STORED
Description
Adai: Key value name;
0: Mark, record extra information
30: Length of data retention
2: Data Length (number of characters)

Get Adai
#查看数据
VALUE Adai 0 2
Ab
END
Note: You can use the Ctrl+backspace key to delete the current row data one at a time after entering memcached, Ctrl+u to clear the current row data once.

memcached Grammar Rules

Command name Set/add/replace
Key Find keyword
Flags client uses it to store additional information about key-value pairs (tokens)
Exptime the time that the data is alive, 0 means forever
Bytes Number of bytes stored
Data block stored (can be directly understood as value in the key-value structure)
Format: < command name > < key > < flags > < Expiretime > < bytes >\r\n < data block >\r\n

<command name> can be "set", "Add", "replace"
"Set" means that the data is stored according to the corresponding <key>, there is no time to increase, some overwrite
"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 with the data that needs to be stored and returned when the client get data
Customers can use this flag for special purposes, which is opaque to the server
<exptime> expiry time
If 0 means the stored data is never obsolete (but can be replaced by 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> number of bytes required to be stored (not including the last "\ r \ n"),,<bytes> can be 0 when the user wants to store empty data
Finally, the client needs to add "\ r \ n" As the end flag for the "command Header" (=enter).
<data Block>: What to store (after the above parameters have been set, enter data)
Example:

Set Key1 0 0 2
12
STORED
Add a data
Get Key1
VALUE Key1 0 2
12
END
#查看数据
Delete Key1 0
DELETED
#删除一个数据
Get Key1
END
Quit
Connection closed by foreign host.

[[Email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '.
Set Adai 0 0 3
123
STORED
Get Adai
VALUE Adai 0 3
123
END
Replace Adai 2 200 5
12345
STORED
#更改数据
Get Adai
VALUE Adai 2 5
12345
END

Quit
Connection closed by foreign host.

To view the data status:
[Email protected] ~]# Memcached-tool 127.0.0.1:11211 display

Item_size max_age Pages Count full? Evicted Evict_time OOM

1 96B 22s 1 1 Yes 0 0 0
#Item_Size项目大小 (similar to the concept of a block), the column is displayed incrementally
Note: The data disappears after a data exceeds its expire time, and if expire time=0 means that it never expires, the data can be replaced with an algorithm such as LRU.

21.6 memcached Data export and import

Because the memcached data is not persisted (restarting the Memcached service data is lost), it is best to export the data before restarting the service, and then import the data into memcached to prevent data loss until the reboot is complete.

Data export

Prepare the data:

[[Email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '.
Set Name 1 0 4
Adai
STORED
Set age 1 0 2
26
STORED
Set weight 1 0 2
80
Set high 1 0 3
180
STORED
^]
Telnet> quit
Connection closed.
To view the data status:

[Email protected] ~]# MemStat--servers=127.0.0.1:11211
Curr_items:4
That is, there are currently 4 items.

Export Data:

[Email protected] ~]# memcached-tool 127.0.0.1:11211 dump >/tmp/memdata.bak
Dumping Memcache Contents
Number of Buckets:1
Number of Items:4
Dumping Bucket 1-4 Total items

[Email protected] ~]# Cat/tmp/memdata.bak
Add High 1 1506759339 3
180
Add Name 1 1506759339 4
Adai
Add weight 1 1506759339 2
80
Add age 1 1506759339 2
26
Import data

[[Email protected] ~]# NC 127.0.0.1 11211 </tmp/memdata.bak
Not_stored
Not_stored
Not_stored
Not_stored
#数据已经存在, so it cannot be imported.

[Email protected] ~]# systemctl restart memcached

[[email protected] ~]# date-d "+1 Hour" +%s
1506768265
#获取1小时后的时间戳,
Change the timestamp in #将memdata. bak to this value

[Email protected] ~]# Vim/tmp/memdata.bak
Add High 1 1506768265 3
180
Add Name 1 1506768265 4
Adai
Add weight 1 1506768265 2
80
Add age 1 10000000 2
26

[[Email protected] ~]# NC 127.0.0.1 11211 </tmp/memdata.bak
STORED
STORED
STORED
STORED

[[Email protected] ~]# 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 4
Adai
END
Get age
END
Get high
VALUE High 1 3
180
END
Get weight
VALUE Weight 1 2
80
END
Note: The exported data is with a timestamp, which is the time when the data expires, and if the data already exists or the current time has passed, the data will not be imported successfully.

21.7 PHP Connection memcached

Using PHP to connect memcached requires that you first compile the memcached extension module that installs PHP. (This machine has already installed PHP before, so recompile directly, if it is the first time installation, you need to download the installation package.) )

[Email protected] src]# pwd
/usr/local/src

Download module:
[Email protected] src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz

[Email protected] src]# TAR-ZXVF memcache-2.2.3.tgz

[Email protected] src]# CD memcache-2.2.3/

[Email protected] memcache-2.2.3]#/usr/local/php-fpm/bin/phpize
Error:
Cannot find autoconf. Please check your autoconf installation and the
$PHP _autoconf environment variable. Then, rerun the this script.
That is, missing autoconf. Unable to produce configure file at this time

[email protected] memcache-2.2.3]# Yum install-y autoconf

[Email protected] memcache-2.2.3]#/usr/local/php-fpm/bin/phpize
Get!!

Compile:
[Email protected] memcache-2.2.3]#/configure--with-php-config=/usr/local/php-fpm/bin/php-config
[[email protected] memcache-2.2.3]# echo $?
0

[[email protected] memcache-2.2.3]# make && make install
Installing Shared extensions:/usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/

[Email protected] memcache-2.2.3]# ls/usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
Memcache.so
That is, generate the Memcache.so module file.

Load module:
[Email protected] memcache-2.2.3]# Vim/usr/local/php-fpm/etc/php.ini
Add the following parameters:
Extension=memcache.so

[Email protected] memcache-2.2.3]#/usr/local/php-fpm/sbin/php-fpm-m
Memcache
That is, the Memcache module is loaded successfully!

Test

Download the test script to see if PHP supports Memcache extensions.

The

Test script reads as follows:
[[[email protected] src]# vim 1.php
<?php
//Connection Memcache Memcache
$mem = new Memcache;
$mem->connect ("localhost", 11211);
//Save Data
$mem->set (' key1 ', ' This is first value ', 0);
$val = $mem->get (' Key1 ');
echo "Get key1 value:". $val. " <br> ";
//Replace data
$mem->replace (' key1 ', ' This is replace value ', 0,);
$val = $mem->get (' Key1 ');
echo "Get key1 value:". $val. "<br>";
//Save array data
$arr = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');
$mem->set (' Key2 ', $arr, 0, 60);
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "<br>";
//delete data
$mem->delete (' Key1 ');
$val = $mem->get (' Key1 ');
echo "Get key1 value:". $val. "<br>";
//Clear All data
$mem->flush ();
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "<br>";
//close connection
$mem->close ();
?
Test:

[Email protected] src]#/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>
This condition indicates that the configuration was successful!

21.8 memcached in storage session

Change the session storage location (memcached).

Session Test

The following is the location of the session file before changing the storage location:

The test script contents are as follows:
[email protected] ~]# cat. Mem_se.txt
<?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 ();
?>

Place the script at the root of the current machine's default site,/data/wwwroot/www:
[Email protected] ~]# cd/data/wwwroot/www
[Email protected] www]# Mv/root/.mem_se.txt 1.php
#进行移动并重命名

Execute the following command:
[Email protected] www]# Curl localhost/1.php
1506768299<br><br>1506768299<br><br>pcqf6d4r4sjeede4nkpvjni9n6
# #此时存储了一个session

[Email protected] www]# ls/tmp/
Sess_pcqf6d4r4sjeede4nkpvjni9n6
# #该文件为执行上面命令生成的session文件
Change Session Store Path

Method 1:

[Email protected] ~]# Vim/usr/local/php-fpm/etc/php.ini
; Session.save_handler = Files
The default value is files, which is stored under/tmp/
Session.save_handler = Memcache
# #指定存储类型
Session.save_path = "tcp://192.168.8.131:11211"
# #指定memcached服务器的IP和端口

To restart the PHP service:
[Email protected] www]#/etc/init.d/php-fpm restart
Gracefully shutting down php-fpm. Done
Starting php-fpm Done

Delete the/tmp/session file for the following reason:
[Email protected] www]# rm-rf/tmp/sess*

Create session here:
[Email protected] www]# Curl localhost/1.php
1506769084<br><br>1506769084<br><br>qe73bqse8rmckfchgb7j5b8ba6
# # #注: The last value here is key

To view session information:
[[Email protected] www]# telnet 127.0.0.1 11211
Trying 127.0.0.1 ...
Connected to 127.0.0.1.
Escape character is ' ^] '.
Get Qe73bqse8rmckfchgb7j5b8ba6
VALUE Qe73bqse8rmckfchgb7j5b8ba6 0 37
test|i:1506769084; test3|i:1506769084;
END

If you cannot see the key value of the session data,
Can not be viewed directly in the memcached,
The workaround is to export the memcached data:
[Email protected] www]# memcached-tool 127.0.0.1:11211 dump >/tmp/memdata.bak

[Email protected] www]# Cat/tmp/memdata.bak
Add Qe73bqse8rmckfchgb7j5b8ba6 0 1506770524 37
test|i:1506769084; test3|i:1506769084;
Success!

Method 2:

Define the save path of the session in Httpd.conf:

Php_value Session.save_handler "Memcache"
Php_value Session.save_path "tcp://192.168.8.130:11211"
Method 3:

Define the save path for the session in the php-fpm.conf corresponding pool:

Php_value[session.save_handler] = Memcache
Php_value[session.save_path] = "tcp://192.168.8.130:11211"

memcached Installation and application

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.