作者:zhanhailiang 日期:2014-12-27
简介
SQLRelay 是一个持久化的数据库连接池,用来为 Unix 或者 Linux 提供数据库连接池、代理以及负载均衡的功能。
曾在2004淘宝架构迁移充当管理Oracle数据库链接的中间件,请见:
连接池示意图:
负载均衡示意图:
功能特性
- 加速数据库驱动的基于 Web 的应用程序
- 增加 Web 应用程序的可扩展性
- 分布式的访问复制的数据库系统
- 对数据库访问进行节流
- 从不被支持的平台上访问某个数据库
- 可以轻松的对数据库系统进行移植
支持的数据库
- Getting Started With Oracle
- Getting Started With MySQL
- Getting Started With PostgreSQL
- Getting Started With Sybase
- Getting Started With Microsoft SQL Server 2000 Desktop Engine
- Getting Started With IBM DB2
- Getting Started With Firebird
- Getting Started With SQLite
- Getting Started With Microsoft Access
- Getting Started With Blackray
- Getting Started With ODBC
支持的语言
- C++
- C
- C#
- Perl
- PHP
- Python
- Ruby
- Java
- TCL
- 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:
[[email protected]~]# cd /usr/local/firstworks/etc/
[[email protected]/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后可以看到如下进程:
[[email protected]/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
参考文章
- php+sqlrelay+mysql实现连接池及读写负载均衡
- 开源数据库连接池 SQL Relay 的安装配置和应用
- SQL Relay官网
- Running SQL Relay
- Getting Started With MySQL
- Programming with SQL Relay using the PHP API
- SQL Relay PHP References
时间: 2024-11-25 12:20:28