`
ezerg
  • 浏览: 273463 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

MySQL 使用 HandlerSocket 实现 NOSQL 功能

阅读更多


一、安装
1、下载 mysql-5.1.54.tar.gz 解压至 /root/mysql/mysql-5.1.54 目录
2、下载 ahiguti-HandlerSocket-Plugin-for-MySQL-1.0.6-94-g98b14c3.tar.gz 解压至 /root/mysql/ahiguti-HandlerSocket-Plugin-for-MySQL-98b14c3 目录
3、以动态编译的形式编译安装 mysql 到 /usr/local/mysql5.1
4、编译 HandlerSocket 插件,在 /root/mysql/ahiguti-HandlerSocket-Plugin-for-MySQL-98b14c3 目录
# ./autogen.sh
# ./configure --with-mysql-source=/root/mysql/mysql-5.1.54 --with-mysql-bindir=/usr/local/mysql5.1/bin --with-mysql-plugindir=/usr/local/mysql5.1/lib/mysql/plugin
with-mysql-source 表示MySQL源代码目录,with-mysql-bindir 表示MySQL二进制可执行文件目录(也就是 mysql_config 所在目录),with-mysql-plugindir 表示MySQL插件目录
如果不清楚这个目录在哪,可以按如下方法查询:
mysql> SHOW VARIABLES LIKE 'plugin%';
+---------------+-----------------------+
| Variable_name | Value                 |
+---------------+-----------------------+
| plugin_dir    | /usr/lib/mysql/plugin |
+---------------+-----------------------+
编译和安装
# make && make install

二、配置 MySQL
# vi /etc/my.cnf
[mysqld]
loose_handlersocket_port = 9998
# the port number to bind to (for read requests)
loose_handlersocket_port_wr = 9999
# the port number to bind to (for write requests)
loose_handlersocket_threads = 16
# the number of worker threads (for read requests)
loose_handlersocket_threads_wr = 1
# the number of worker threads (for write requests)
open_files_limit = 65535
# to allow handlersocket accept many concurren connections, make open_files_limit as large as possible.

激活HandlerSocket插件,在客户端命令执行
mysql> install plugin handlersocket soname 'handlersocket.so';
查看 handlersocket 进程
mysql>  show processlist
+----+-------------+-----------------+---------------+---------+------+-------------------------------------------+------------------+
| Id | User        | Host            | db            | Command | Time | State                                     | Info             |
+----+-------------+-----------------+---------------+---------+------+-------------------------------------------+------------------+
|  1 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
|  2 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
|  3 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
|  4 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
|  5 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
|  6 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
|  7 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
|  8 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
|  9 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
| 10 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
| 11 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
| 12 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
| 13 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
| 14 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
| 15 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
| 16 | system user | connecting host | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL             |
| 17 | system user | connecting host | handlersocket | Connect | NULL | handlersocket: mode=wr, 0 conns, 0 active | NULL             |

查看系统进程占用的端口
# lsof -i :9998
COMMAND  PID  USER   FD   TYPE DEVICE SIZE NODE NAME
mysqld  2731 mysql   11u  IPv4 571386       TCP *:9998 (LISTEN)
# lsof -i :9999
COMMAND  PID  USER   FD   TYPE DEVICE SIZE NODE NAME
mysqld  2731 mysql   29u  IPv4 571403       TCP *:9999 (LISTEN)


三、测试使用

在数据库 test 创建测试表
CREATE TABLE `user` ( 
`user_id` INT(10) UNSIGNED NOT NULL, 
`user_name` VARCHAR(50) DEFAULT NULL, 
`user_email` VARCHAR(255) DEFAULT NULL, 
`created` DATETIME DEFAULT NULL, 
PRIMARY KEY (`user_id`), 
KEY `INDEX_01` (`user_name`) 
) ENGINE=INNODB 

插入测试数据
INSERT INTO USER VALUES(1, "John", "john@test.com", CURRENT_TIMESTAMP); 
INSERT INTO USER VALUES(2, "Kevin", "Kevin@test.com", CURRENT_TIMESTAMP); 
INSERT INTO USER VALUES(3, "Dino", "Dino@test.com", CURRENT_TIMESTAMP); 

编译 Perl 客户端,在 /root/mysql/ahiguti-HandlerSocket-Plugin-for-MySQL-98b14c3 目录执行
# ./autogen.sh
# ./configure --disable-handlersocket-server
# make && make install
# cd perl-Net-HandlerSocket
# perl Makefile.PL
# make && make install

perl 程序文件:
#!/usr/bin/perl  
  
use strict;  
use warnings;  
use Net::HandlerSocket;  
  
#1. establishing a connection  
my $args = { host => 'localhost', port => 9998 };  
my $hs = new Net::HandlerSocket($args);  
  
#2. initializing an index so that we can use in main logics.  
# MySQL tables will be opened here (if not opened)  
my $res = $hs->open_index(0, 'test', 'user', 'INDEX_01', 'user_name,user_email,created');  
die $hs->get_error() if $res != 0;  
  
#3. main logic  
#fetching rows by id  
#execute_single (index id, cond, cond value, max rows, offset)  
$res = $hs->execute_single(0, '=', [ 'kevin' ], 1, 0);  
die $hs->get_error() if $res->[0] != 0;  
shift(@$res);  
for (my $row = 0; $row < 1; ++$row) {  
  my $user_name= $res->[$row + 0];  
  my $user_email= $res->[$row + 1];  
  my $created= $res->[$row + 2];  
  print "$user_name\t$user_email\t$created\n";  
}  
  
#4. closing the connection  
$hs->close();  

下载 hs4j-0.1 软件包。
Java 程序文件:
import java.sql.ResultSet;

import com.google.code.hs4j.HSClient;
import com.google.code.hs4j.HSClientBuilder;
import com.google.code.hs4j.IndexSession;
import com.google.code.hs4j.impl.HSClientBuilderImpl;

public class TestNoSQL {
	public static void main(String[] args) throws Exception {
		HSClientBuilder hscb = new HSClientBuilderImpl();
		hscb.setServerAddress("10.10.10.2", 9999);
		HSClient hsc = hscb.build();
		IndexSession is = hsc.openIndexSession(1,"test", "user", "INDEX_01", new String[]{"user_name","user_email","created"});
		// 插入
		is.insert(new String[]{"ezerg", "ezerg@126.com", "2011-10-12 13:04:33"});
		final String[] keys = { "ezerg"};
		// 查询
		ResultSet rs = is.find(keys);
		while(rs.next()) {
			System.out.println("name="+rs.getString(1));
			System.out.println("email="+rs.getString(2));
		}
		// 删除
		// is.delete(keys);
		// 更新
		// is.update(keys, new String[] { "ezerg", "ezerg@163.com", "2011-10-12 13:04:33" }, FindOperator.EQ); 
		// 关闭连接
		hsc.shutdown();
	}
}


0
1
分享到:
评论

相关推荐

    php-handlersocket:一个使用HandlerSocket NoSQL MySQL插件的PECL扩展

    HandlerSocket会覆盖与SQL解析相关的所有CPU使用率。 相反,它直接向InnoDB使用文本协议来创建/读取/更新/删除(CRUD)数据。 它可以在服务器端结合多种操作。 支持SQL命令: SELECT, UPDATE, INSERT, DELETE 支持...

    MySQL HandlerSocket插件安装配置教程

    MySQL HandlerSocket插件是一款由akira higuchi开发的MySQL扩展,它以Daemon Plugin的形式运行,为MySQL提供了类似NoSQL的访问方式。该插件允许用户直接与存储引擎进行键值对交互,绕过了SQL解析、打开关闭表、创建...

    基于大数据下的NOSQL和Mysql融合的数据存储模型研究.pdf

    例如,MySQL 5.6.2版本引入了对Memcached的支持,允许开发者在InnoDB之上使用NOSQL功能,同时保持与MySQL的共存。HandlerSocket的出现进一步提升了性能,它的性能是Memcached的两倍,且支持大量并发连接,能够执行...

    HandlerSocket

    HandlerSocket的应用场景:MySQL自身的局限性,很多站点都采用了MySQL+Memcached的经典架构,甚至一些网站放弃MySQL而采用NoSQL产品,比如Redis/MongoDB等。不可否认,在做一些简单查询(尤其是PK查询)的时候,很多...

    MySQL新技术在淘宝的使用

    HandlerSocket是一种基于MySQL实现的NoSQL插件,它提供了类似于NoSQL数据库的非结构化数据存储能力。通过直接与MySQL服务器通信,HandlerSocket可以绕过SQL解析器,直接读取和写入数据,从而实现更高的读写性能。 #...

    php-handlersocket

    HandlerSocket是MySQL提供的一种高性能、低延迟的接口,旨在为InnoDB存储引擎提供NoSQL风格的访问方式。通过绕过SQL解析和优化步骤,HandlerSocket能显著提高数据读取和写入的速度,尤其适用于大数据量的实时应用。 ...

    php-handelrsocket_lo9k7t_V2_php_

    结合以上信息,我们可以推测这个压缩包可能包含了一个使用PHP实现的HandlerSocket客户端,允许开发者在PHP应用程序中直接与MySQL的HandlerSocket插件通信,以提高数据库操作的性能。使用这个库的开发者需要了解PHP...

    详解MySQL下InnoDB引擎中的Memcached插件

    总算是在MySQL中延续了NoSQL的香火,以前单独架设Memcached服务器不仅浪费了内存,而且还必须自己维护数据的不一致问题,有了Memcached插件,这些问题都不存在了,而且借助MySQL本身的复制功能,我们可以说是变相的...

    up_683643_php-handelrsocket_vtmyk.rar

    HandlerSocket是一个特殊的MySQL客户端库,它绕过了MySQL的标准查询解析器,直接通过套接字与MySQL服务器进行交互,从而实现了极高的数据读取性能。这种技术常用于大数据量的实时查询场景,如日志分析或在线分析处理...

    php-handelrsocket_vero24_V2_php_

    2. **HandlerSocket介绍**:HandlerSocket是由Nobuhiro Iwamatsu开发的一个MySQL插件,它提供了一个NoSQL风格的接口,绕过MySQL的查询解析、优化和执行过程,直接操作InnoDB存储引擎的数据,极大地提高了读写性能,...

    php-handelrsocket_npw4x2_npw569_V2_php_

    标题中的"php-handelrsocket_npw4x2_npw569_V2_php_"似乎是一个项目或库的名称,其中包含了"php",表明它与PHP编程语言有关,"handlersocket"则可能指的是HandlerSocket扩展...

    php-handelrsocket_xwy9zm_V2_php_

    HandlerSocket是MySQL数据库的一个插件,它提供了直接访问存储引擎层的能力,允许PHP代码以类似NoSQL的方式快速读写数据库,跳过SQL解析和优化过程,从而提高性能。 描述中提到的"这是一个用php脚本语言写的其它...

    php并发对MYSQL造成压力的解决方法

    再者,可以考虑使用非SQL接口,如MySQL的HandlerSocket插件或者InnoDB NoSQL Plugin,它们可以直接操作存储引擎,绕过SQL解析,极大地提高了处理速度。特别是InnoDB NoSQL Plugin,它支持memcached协议,并共享...

    handler socket erlang client

    handlersocket是基于mysql的nosql解决方案,与普通的nosql方案比较,具有更大的灵活性,可以使用mysql的索引。性能相比于mysql的批量操作方式,具有5倍左右的提升(我测试的,可能是内存设置的不多)。 详细内容见此...

Global site tag (gtag.js) - Google Analytics