Linux Learning Summary (75) Memcached of non-relational database

Source: Internet
Author: User
Tags curl fpm install php key string memcached mysql query php script

A non-relational database

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
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.
Columnstore HBASE0
Figure neo4j, Infinite graph, Orientdb

Two memcached Fundamentals

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 allocation
The principle of Slab allocation
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.
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 to expire data
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.

Three memcached installation
yum install -y memcached libmemcached libeventsystemctl start memcached

vim /etc/sysconfig/memcachedParameters can be configured
For example, with the listening IP, you can change the options= "" to Options= "127.0.0.1"
Where-m specifies memcached allocates memory
-c Specifies the maximum number of concurrent
-u Specifies the user running the memcached service
View memcached Run Status
memcached-tool 127.0.0.1:11211 stats
In memcached, run the state command to view status information for the memcached service, where Cmd_get represents the total get count, Get_hits represents the total hit count for get, and hit ratio = Get_hits/cmd_get.
or echo stats |nc 127.0.0.1 11211 you need to install the NC toolyum install -y nc
If you install libmemcached, you can use the command
memstat --servers=127.0.0.1:11211View memcached Service Status

Four memcached database syntax

memcached command Line
telnet 127.0.0.1 11211
set key2 0 30 2
ab
STORED
get key2
VALUE Key2 0 2
Ab
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 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
memcached Data export and import
Export:
memcached-tool 127.0.0.1:11211 dump > Data.txt
Cat Data.txt
Import:
NC 127.0.0.1 11211 < data.txt
If the NC command does not exist, yum install NC
memstat --servers=127.0.0.1:11211View the data storage situation
Note: The exported data is provided with a timestamp, which is the point at which the data expires, and if the current time exceeds that timestamp, it is not imported. When our set write data expires at 0 o'clock, although the current representation never expires, when the data is exported, a timestamp is exported when the command was created, so when we import it expires. To successfully import, you need to manually change the timestamp.

Five PHP connection memcached

1 First install php memcache extension
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// Generate Configure file
appears can ' t find autoconf, yum install-y autoconf
./configure--with-php-config=/usr/local/php-fpm/ Bin/php-config
Make && do 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/
And then modify php.ini add a line extension= "memcache.so"
Check/usr/local/php/bin/ PHP-FPM-M
2 Test connection
Download the test script
Curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null
The contents of the script are as follows

<?php//连接Memcache Memcache$mem = new Memcache;$mem->connect("localhost", 11211);//保存数据$mem->set(‘key1‘, ‘This is first value‘, 0, 60);$val = $mem->get(‘key1‘);echo "Get key1 value: " . $val ."<br>";//替换数据$mem->replace(‘key1‘, ‘This is replace value‘, 0, 60);$val = $mem->get(‘key1‘);echo "Get key1 value: " . $val . "<br>";//保存数组数据$arr = array(‘aaa‘, ‘bbb‘, ‘ccc‘, ‘ddd‘);$mem->set(‘key2‘, $arr, 0, 60);$val2 = $mem->get(‘key2‘);echo "Get key2 value: ";print_r($val2);echo "<br>";//删除数据$mem->delete(‘key1‘);$val = $mem->get(‘key1‘);echo "Get key1 value: " . $val . "<br>";//清除所有数据$mem->flush();$val2 = $mem->get(‘key2‘);echo "Get key2 value: ";print_r($val2);echo "<br>";//关闭连接$mem->close();?>

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

Storage session in six memcached

First, check the save path of the session, by default, files, saved on the local disk under/TMP.

vi /usr/local/php-fpm/etc/php.ini[Session]; Handler used to store/retrieve data.; http://php.net/session.save-handlersession.save_handler = files

Next we create a PHP script that can generate a session

Visit Curl in the LNMP environment to access the script and view the file that holds the session in/TMP
Next we edit the relevant configuration file and save the session to Memcached
This example is implemented in the LAMP/LNMP environment
Edit PHP.ini Add two lines

session.save_handler = memcache session.save_path = "tcp://127.0.0.1:11211"  

or 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] = memcachephp_value[session.save_path] = " tcp://127.0.0.1:11211 "

Finish editing the configuration file and continue testing
curl localhost/session.phpCan be tested with any of its own virtual host, not necessarily localhost
Similar to 1443702394<br><br>1443702394<br><br>i44nunao0g3o7vf2su0hnc5440

Note: In the result of the above curl execution, the first two strings are the same number, which is the value information, and the last string is the key information. After the configuration is successful, we can log in to memcached,get the key string to view the stored Vlaue information. The IP address behind TCP in the configuration is changed according to the actual situation.

Linux Learning Summary (75) Memcached of non-relational database

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.