PHP数据库连接池SQL Relay安装使用

DedMcIntyre 9年前

来自: http://blog.csdn.net/21aspnet/article/details/50774684


SQL Relay按照其官网http://sqlrelay.sourceforge.net/index.html上所说是:A powerful database connection management solution.

翻译为中文也就是说SQL Relay是一个开源的数据库池连接代理服务器。

 

目前SQL Relay支持的数据库很多:

SQL Relay supports Oracle, MySQL, PostgreSQL, SAP/Sybase, IBM DB2, Informix, Firebird and SQLite databases directly, using the native client API's for those databases.
Support is provided for Microsoft SQL Server via the FreeTDS API.

 

支持的API也是非常丰富的

Guides: C++CC#PerlPHPPythonRubyJavaTCLErlangnode.js 
References: C++CC#PerlPHPPythonRubyJavaTCLnode.js,

 

本人讲述怎么在Linux+PHP+FreeDTS环境使用连接池,其实PHP->SQL Relay->FreeDTS->数据库 这样的架构。

下面说安装使用

1.先安装rudiments

建议尽量安装新版,旧版可能会报错需要解决。

# wget http://sourceforge.net/projects/rudiments/files/rudiments/0.28.2/rudiments-0.28.2.tar.gz/download
# tar xvzf rudiments-0.54.tar.gz
# cd rudiments-0.54
# ./configure --prefix=/usr/local/rudiments
# make && make install

 

2.再安装sqlrelay

# wget https://sourceforge.net/projects/sqlrelay/files/sqlrelay/0.64/sqlrelay-0.64.tar.gz/download
# tar vxzf sqlrelay-0.64.tar.gz
# cd sqlrelay-0.64
# ./configure --prefix=/usr/local/sqlrelay --with-rudiments-prefix=/usr/local/rudiments  --with-freetds-prefix=/usr/local/freetds --with-php-prefix=/usr/local/php


# make && make install

 

3.修改php配置文件

# vim /usr/local/php/lib/php.ini
增加扩展

extension ="sql_relay.so"
注意:需要确认phpinfo

 

4.修改freeDTS配置

关于freeDTS的安装参考此文:http://blog.csdn.net/unix21/article/details/47449901

修改FreeTDS的配置文件freetds.conf
# vim /usr/local/freetds/etc/freetds.conf

加入以下内容

[msdetest]  host = 192.168.1.1  port =1433  tds version = 7.0  client charset = UTF-8
不然会中文乱码,需要对应的编码类型GB2312等等。

5.修改SQL Relay的配置文件

# cd /usr/local/sqlrelay/etc/
# cp sqlrelay.conf.example  sqlrelay.conf
# vim sqlrelay.conf

整个配置非常好理解

<?xml version="1.0"?>  <!DOCTYPE instances SYSTEM "sqlrelay.dtd">  <instances>  <instance id="msdetest" port="9001" socket="/tmp/msdetest.socket" dbase="freetds" connections="10" maxconnections="50" maxqueuelength="0" growby="1" ttl="60" endofsession="commit" sessiontimeout="5" runasuser="nobody" runasgroup="nobody" cursors="5" authtier="listener" handoff="pass">  <users>  <user user="admin" password="admin"/>  </users>  <connections>  <connection connectionid="msdetest" string="server=msde;db=test;user=admin;password=admin;" metric="1"/>  </connections>  </instance>  </instances>

6.启动SQL Replay

# export PATH=$PATH:/usr/local/sqlrelay/bin
启动:

# sqlr-start -id msde

上图是成功启动,如果配置错误会提示数据库连不上之类的。

 

SQL工具

# sqlrsh -id msde


7.php使用连接池

<?php  $con=sqlrcon_alloc("msdetest",9001,"/tmp/msdetest.socket","admin","admin",0,1);  $cur=sqlrcur_alloc($con);  sqlrcur_sendQuery($cur,"SELECT top 10 * FROM test order by id desc");  for ($row=0; $row<sqlrcur_rowCount($cur); $row++) {  for ($col=0; $col<sqlrcur_colCount($cur); $col++) {  echo sqlrcur_getField($cur,$row,$col);  echo ",";  }  echo "<br>\n";  }  sqlrcur_free($cur);  sqlrcon_free($con);  ?>

前端网页从连接池取出数据

 

SQL Relay的PHP函数API

http://sqlrelay.sourceforge.net/sqlrelay/programming/php.html

参考:http://www.cnblogs.com/zhangjun516/archive/2013/03/12/2955162.html