首页 > 代码库 > 数据库连接池SQL Relay简介

数据库连接池SQL Relay简介

作者:zhanhailiang 日期:2014-12-27

简介

SQLRelay 是一个持久化的数据库连接池,用来为 Unix 或者 Linux 提供数据库连接池、代理以及负载均衡的功能。

曾在2004淘宝架构迁移充当管理Oracle数据库链接的中间件,请见:

  1. 《淘宝业务发展及技术架构》分享 文字版
  2. 《淘宝业务发展及技术架构》分享 PPT版

连接池示意图:

技术分享

负载均衡示意图:

技术分享

功能特性

  1. 加速数据库驱动的基于 Web 的应用程序
  2. 增加 Web 应用程序的可扩展性
  3. 分布式的访问复制的数据库系统
  4. 对数据库访问进行节流
  5. 从不被支持的平台上访问某个数据库
  6. 可以轻松的对数据库系统进行移植

支持的数据库

  1. Getting Started With Oracle
  2. Getting Started With MySQL
  3. Getting Started With PostgreSQL
  4. Getting Started With Sybase
  5. Getting Started With Microsoft SQL Server 2000 Desktop Engine
  6. Getting Started With IBM DB2
  7. Getting Started With Firebird
  8. Getting Started With SQLite
  9. Getting Started With Microsoft Access
  10. Getting Started With Blackray
  11. Getting Started With ODBC

支持的语言

  1. C++
  2. C
  3. C#
  4. Perl
  5. PHP
  6. Python
  7. Ruby
  8. Java
  9. TCL
  10. Erlang

请见:http://sqlrelay.sourceforge.net/sqlrelay/

本文将讲解如何基于SQL Relay,使用PHP进行Mysql操作。

安装

安装rudiments-0.48:

./configure
make && make install

安装sqlrelay-0.57:

./configure --with-mysql-prefix=/usr/local/mysql --with-php-prefix=/usr/local/php
make && make install

配置php.ini:

extension=sql_relay.so

配置SQL Relay:

[root@~]# cd /usr/local/firstworks/etc/
[root@/usr/local/firstworks/etc]# cp sqlrelay.conf.example sqlrelay.conf

其连接Mysql配置如下:

<?xml version="1.0"?>
<!DOCTYPE instances SYSTEM "sqlrelay.dtd">

<instances>

    <!-- Regular SQL Relay Instance -->
    <instance id="mysqlpool" port="12000" socket="/tmp/mysqlpool.socket" dbase="mysql" connections="3" maxconnections="5" maxqueuelength="0" growby="1" ttl="60" endofsession="commit" sessiontimeout="600" runasuser="nobody" runasgroup="nobody" cursors="5">
        <users>
            <user user="root" password="*****"/>
        </users>
        <connections>
            <connection connectionid="mysqlpool" string="user=root;password=*****;db=test" metric="1" behindloadbalancer="no"/>
        </connections>
    </instance>

</instances>

启动和停止SQL Relay

sqlr-start -id mysqlpool
sqlr-start  --trace -id mysqlpool // 跟踪调用详情
sqlr-stop

注:

1. 关于启动命令的使用,请见:http://sqlrelay.sourceforge.net/sqlrelay/admin/running.html
2. 安装SQL Relay后需要将firstworks/bin/目录添加到环境变量PATH中:

    PATH="/usr/local/firstworks/bin/:$PATH"

测试:

启动SQL Relay后可以看到如下进程:

[root@/usr/local/firstworks/etc]# ps -ef|grep sqlrelay
nobody   26512     1  0 22:26 ?        00:00:00 sqlr-listener -id mysqlpool -config /usr/local/firstworks/etc/sqlrelay.conf
nobody   26519     1  0 22:26 ?        00:00:00 sqlr-scaler -id mysqlpool -config /usr/local/firstworks/etc/sqlrelay.conf
nobody   26520     1  0 22:26 ?        00:00:00 sqlr-connection -id mysqlpool -connectionid mysqlpool -config /usr/local/firstworks/etc/sqlrelay.conf
nobody   26521     1  0 22:26 ?        00:00:00 sqlr-connection -id mysqlpool -connectionid mysqlpool -config /usr/local/firstworks/etc/sqlrelay.conf
nobody   26522     1  0 22:26 ?        00:00:00 sqlr-connection -id mysqlpool -connectionid mysqlpool -config /usr/local/firstworks/etc/sqlrelay.conf
root     26579 18985  0 23:07 pts/3    00:00:00 grep sqlrelay

PHP测试脚本:

<?php
$con = sqlrcon_alloc(‘mysqlpool‘, 12000, ‘/tmp/mysqlpool.socket‘, ‘root‘, ‘******‘, 0, 1);

var_dump(sqlrcon_errorNumber ($con));
var_dump(sqlrcon_dbHostName ($con));
var_dump(sqlrcon_dbIpAddress ($con));

$cur = sqlrcur_alloc($con);

sqlrcur_sendQuery($cur, ‘select * from test‘);

var_dump(sqlrcur_totalRows ($cur));

for ($row=0; $row<sqlrcur_rowCount($cur); $row++) {
    for ($col=0; $col<sqlrcur_colCount($cur); $col++) {
        echo sqlrcur_getField($cur,$row,$col);
    }   
    echo PHP_EOL;
}

sqlrcur_free($cur);
sqlrcon_free($con);

执行输出如下:

int(0)
string(9) "Localhost"
string(9) "127.0.0.1"
int(2)
1
2

参考文章

  1. php+sqlrelay+mysql实现连接池及读写负载均衡
  2. 开源数据库连接池 SQL Relay 的安装配置和应用
  3. SQL Relay官网
  4. Running SQL Relay
  5. Getting Started With MySQL
  6. Programming with SQL Relay using the PHP API
  7. SQL Relay PHP References

数据库连接池SQL Relay简介