memcached +mysql+php test Case

Source: Internet
Author: User
Tags fpm memcached mysql query access database

Reference: http://deidara.blog.51cto.com/400447/317526/


First, the test environment:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/72/D8/wKioL1Xup4ihJABKAACwHLXitCo559.jpg "title=" memcached test environment diagram. jpg "alt=" wkiol1xup4ihjabkaacwhlxitco559.jpg "/>


Second, the test steps:

1, complete the construction of the basic Environment (NGINX+PHP-FPM+MEMCACHE+MEMCACHED+MYSQL)

Attention:

Separation of NGINX+PHP-FPM from MySQL environment

How PHP expands the module memcache

The difference between memcache and memcached


2, start the environment, to ensure that the normal start

Attention:

Start memcached:

Memcached-d-M 100-l 192.168.1.120-p 11211-u Root


3. Database authorized user test and IP address can access database Jiang

Grant all on jiang.* to ' test ' @ ' 192.168.1.120 ' identified by ' test ';


4. Create DATABASE and table content

Create Database Jiang;

mysql> use Jiang;


mysql> CREATE TABLE ' Personal_info ' (

' pi_id ' bigint (a) not NULL auto_increment,

' pi_name ' varchar (not NULL),

' Pi_tel ' varchar () default NULL,

' PI_QQ ' varchar () default NULL,

' pi_email ' varchar default NULL,

PRIMARY KEY (' pi_id ')

) Engine=myisam DEFAULT Charset=utf8 auto_increment=4;

Mysql> INSERT into ' jiang '. ' Personal_info ' (

' pi_id ',

' Pi_name ',

' Pi_tel ',

' Pi_qq ',

' Pi_email '

)

VALUES (

' 1 ', ' jiang ', ' 1586544556 ', ' 42423423 ', ' [email protected] '

);


Mysql> SELECT * from Personal_info

;

+-------+---------+------------+----------+--------------------+

| pi_id | Pi_name | Pi_tel | PI_QQ | Pi_email |

+-------+---------+------------+----------+--------------------+

| 1 | Jiang | 1586544556 | 42423423 | [Email protected] |

+-------+---------+------------+----------+--------------------+


5, PHP test code (principle, if there is a query value in memecached, then read directly, if not read from the MySQL database, and re-write a copy to memcached)


<?php

$memcachehost = ' 192.168.1.120 ';

$memcacheport = 11211;

$memcachelife = 60;

$memcache = new Memcache;

$memcache->connect ($memcachehost, $memcacheport) or Die ("Could not Connect");

$query = "SELECT * FROM Personal_info limit 10";

$key =md5 ($query);

if (! $memcache->get ($key))

{

$conn =mysql_connect ("192.168.1.121", "Test", "test");

mysql_select_db (Jiang);

$result =mysql_query ($query);

while ($row =mysql_fetch_assoc ($result))

{

$arr []= $row;

}

$f = ' MySQL ';

$memcache->add ($key, Serialize ($arr), 0,30); After MySQL query, insert memcached

$data = $arr;

}

else{

$f = ' memcache ';

$data _mem= $memcache->get ($key);

$data = unserialize ($data _mem);

}

Echo $f;

echo "<br>";

echo "Key:";

Print_r ($key);

echo "<br>";

foreach ($data as $a)

{

echo "number is <b><font color= #FF0000 > $a [pi_id]</font></b>";

echo "<br>";

echo "Name is <b><font color= #FF0000 > $a [pi_name]</font></b>";

echo "<br>";

echo "Tel is <b><font color= #FF0000 > $a [pi_tel]</font></b>";

echo "<br>";

echo "QQ is <b><font color= #FF0000 > $a [pi_qq]</font></b>";

echo "<br>";

echo "Email is <b><font color= #FF0000 > $a [pi_email]</font></b>";

echo "<br>";


}

?>


6. Test results

Visit: http://192.168.1.120/memsql.php

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/72/DB/wKiom1Xuo_WQ_m3QAACJ75g2kUk342.jpg "title=" Memcached1.jpg "alt=" Wkiom1xuo_wq_m3qaacj75g2kuk342.jpg "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/72/DB/wKiom1XupAGTbBuGAAC0MTiKlKA074.jpg "title=" Memcached2.jpg "alt=" Wkiom1xupagtbbugaac0mtiklka074.jpg "/>

Telnet 192.168.1.120


Get 76fc3b222d217ac77d6797672fb09eb1 #执行get key to get the contents of the memcached cache

VALUE 76FC3B222D217AC77D6797672FB09EB1 0 161

A:1:{i:0;a:5:{s:5: "pi_id"; s:1: "1"; s:7: "Pi_name"; s:5: "Jiang"; s:6: "Pi_tel"; s:10: "1586544556"; s:5: "Pi_qq"; S:8: " 42423423 "; s:8:" Pi_email "; s:18:" [email protected] ";}}






memcached +mysql+php test Case

Related Article

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.