Not only SQL: memcache与MySQL5.6

Not only SQL: memcache与MySQL5.6

http://schlueters.de/blog/archives/152-Not-only-SQL-memcache-and-MySQL-5.6.html

MySQL 5.6 milestone releases给memcache提供了一个访问InnodDB表的接口!!
这就意味着除了SQL语句,还可以使用noSQL协议来访问mysql中的数据!

原理简述:
mysql server中加载一个插件,这个插件会运行memcache daemon。
memecached既有传统的功能:在memory hash table中读取/保存数据,还有二个新功能:
1. 直接读写innodb table.
2. 如果在memory hash table中找不到所需数据,可以去innodb table中寻找。

这样就把memcache和mysql的优点组合到一起了:
1. memcache连接速度快(因为没有身份认证这些东西),数据读写速度快(直接在内存中操作,不必parse sql语句。);
2. mysql(innodb) 支持SQL语句,可以进行复杂的查询,可以持久化存储。

下面讲讲在php中怎样使用这个新玩意:
首先下载mysql 5.6 编译安装。 在我印象中,从5.5开始就没办法./configure了,是另外一种autoconf/automake之类的东西。

然后配置PHP。可以使用老的memcache 模块或者新的memcached模块。
用PECL安装吧:

# pecl install memcache
or
# pecl install memcached

也可以下载源码自己编译。
然后把extension=memcache[d].so添加到php.ini中。

在命令行中测试一下看:

$ php -r '$m = memcache_connect("localhost", 11211); ' \
         '$m->add("key", "value"); var_dump($m->get("key"));'
string(5) "value"

memcache的读写功能都正常。
在mysql中找找看有没有对应的数据:

mysql> SELECT * FROM demo_test WHERE c1 = 'key';
Empty SET (0.00 sec)

为啥会没有呢?

If you would like to take a look at what’s in the “demo_test” table,
 please remember we had batched the commits (32 ops by default) by default.
So you will need to do “read uncommitted” select to find the just inserted rows

原来是事务未被提交呢。
这样就行了:

mysql> SET SESSION TRANSACTION ISOLATION LEVEL READ uncommitted;
Query OK, 0 ROWS affected (0.00 sec)
 
mysql> SELECT * FROM demo_test WHERE c1 = 'key';
+------+------+------+------+-------+------+------+------+------+------+------+
| cx   | cy   | c1   | cz   | c2    | ca   | CB   | c3   | cu   | c4   | C5   |
+------+------+------+------+-------+------+------+------+------+------+------+
| NULL | NULL | KEY  | NULL | VALUE | NULL | NULL |    0 | NULL |    1 | NULL |
+------+------+------+------+-------+------+------+------+------+------+------+
1 ROW IN SET (0.00 sec)

有木有? value有木有!

下面说session相关的东西。
php默认是把session信息保存在/tmp/目录中的。其实它也支持保存到memcache中。

修改php.ini

; when using the "memcache" extension:
session.save_handler=memcache
; when using the "memcached" extension:
; session.save_handler=memcached
 
session.save_path="tcp://localhost:11211"

重启web服务。
测试一下看:

<?php
session_start();
echo "<pre>Session ID: ".session_id()."\n";
var_dump($_SESSION);
$_SESSION['foo'] = 'bar';
?>

输出:

m1h4iqmp6hc7e4l85qlld0gtd
array(0) {
}

刷新页面之后输出:

m1h4iqmp6hc7e4l85qlld0gtd1
array(1) {
  ["foo"]=>
  string(3) "bar"
}

说明session功能正常。
看看数据库中有没有数据:

mysql> SELECT * FROM demo_test WHERE c1 = 'm1h4iqmp6hc7e4l85qlld0gtd1';
+------+------+----------------------------+------+----------------+------+------+------+------+------+------+
| cx   | cy   | c1                         | cz   | c2             | ca   | CB   | c3   | cu   | c4   | C5   |
+------+------+----------------------------+------+----------------+------+------+------+------+------+------+
| NULL | NULL | m1h4iqmp6hc7e4l85qlld0gtd1 | NULL | foo|s:3:"bar"; | NULL | NULL |    0 | NULL |    4 | NULL |
+------+------+----------------------------+------+----------------+------+------+------+------+------+------+
1 ROW IN SET (0.00 sec)

很不错。

阅读更多:

http://dev.mysql.com/

Be Sociable, Share!

Related posts:

  1. mysql LAST_INSERT_ID() 的几个问题
  2. mysql中的old_passwords
  3. select *,colname 与 select colname,*
  4. mysql使用中常犯的八个错误
  5. MySQL5.6 中memcache插件的安装

Please Buy me a beer. 支付宝帐户: zkyaoo@163.com 百威10元,青岛7元,雪花4元。

About 花荣

He is the founder, designer, and managing editor of zhaokunyao.com, and he is perpetually behind schedule.
This entry was posted in mysql, 花荣在写PHP代码 and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">