PHP數據庫連接池SQL Relay安裝使用
來自: 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++, C, C#, Perl, PHP, Python, Ruby, Java, TCL, Erlang, node.js
References: C++, C, C#, Perl, PHP, Python, Ruby, Java, TCL, node.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