一.什么是sqlrelay?
Sqlrelay是一个开源的数据库连接池软件。它可以预建立数据库连接,同时可作为数据库连接代理和负载均衡器使用。
二.Sqlrelay原理示意:
1连接池示意图:
三.安装和配置
开始安装SQL Relay,到 http://sqlrelay.sourceforge.net/ 去下载相应的软件
安装SQL Relay需要先安装Rudiments, 在 http://sqlrelay.sourceforge.net/download.html 可以找到
1.安装rudiments
安装rudiments会报configure: error: C++ compiler cannot create executables错误,
说明没装gcc或者装了gcc,没装g++。
2.安装sqlrelay:
3.配置sqlrelay
配置文件路径: /usr/local/sqlrelay/etc/sqlrelay.conf。内容如下:
4.启动sqlrelay
5.服务测试
从返回的信息可以看出mysql的有3个连接到my数据库的活动连接,正是sqlrelay配置文件中默认活动连接数量.
四.利用PHP API来测试连接池
1.查看php安装目录的扩展目录:
2.将sql_relay.so扩展添加到php.ini配置文件中
3.测试php是否加载sql_relay.so模块
4.到此说明php可以通过SQL RELAY连接池来访问mysql数据库了
执行php_sqlrelay.php文件
--------------------------------------------------------------
本文以SQL RELAY在MYSQL上的应用,应用测试以PHP程序为主.
SQL Relay是一个开源的数据库池连接代理服务器
SQL Relay 的主要特点:
SQL Relay 支持的数据库类型:
SQL Relay支持客户端接口:
相关软件:
Rudiments http://sourceforge.net/projects/ ... 8.2.tar.gz/download
SQL Relay http://sourceforge.net/projects/ ... 6.4.tar.gz/download
SQL RELAY的安装依赖Rudiments 库文件,因此在安装sql relay时,首先安装Rudiments 库
Rudiments的安装
复制代码
SQL RELAY的安装:
复制代码
配置sqlreplay.conf配置文件如下:
复制代码
注:蓝色文字为需要修改的参数
到此sqlrelay.conf文件配置完成,现在来运行sqlrelay:
复制代码
看到有sqlr-*的进程 和 sqlr-listener的9002端口存在,则说明配置成功了。到此就可以测试sql relay了
//mysqlpool为配置文件中定义的连接池实例
复制代码
从返回的信息可以看出mysql的有5个连接到my数据库的活动连接,正是sqlrelay配置文件中默认活动连接数量
利用PHP API来测试连接池:
查看php安装目录的扩展目录:
复制代码
将sql_relay.so扩展添加到php.ini配置文件中
复制代码
测试php是否加载sql_relay.so模块
[falcon@fwphp-cn ~/phpcode]$ /usr/local/php-fcgi/bin/php -m
[PHP Modules]
Bcmath
......
sockets
SPL
sql_relay
SQLite
sqlite3
........
........
到此说明php可以通过SQL RELAY连接池来访问mysql数据库了
复制代码
执行php_sqlrelay.php文件
[falcon@fwphp-cn ~/phpcode]$ /usr/local/php-fcgi/bin/php php_sqlrelay.php
1,admin,84383e908a8fb8b5dec1c82ca5a710d2,admin@your.com,,,192.168.234.1,1255863260,0,0,cdedef,,
到处SQL RELAY数据库连接池测试全部结束
参考链接:
SQL RELAY :http://sqlrelay.sourceforge.net/index.html
PHP API :http://sqlrelay.sourceforge.net/sqlrelay/api/php/doc.html
C API :http://sqlrelay.sourceforge.net/ ... lientwrapper.h.html
注:
如果sqlrelay 采用0.41 Rudiments采用0.32 测试时 会报错,估计是Bug
http://blog.csdn.net/linkor/article/details/6591103
Sqlrelay是一个开源的数据库连接池软件。它可以预建立数据库连接,同时可作为数据库连接代理和负载均衡器使用。
二.Sqlrelay原理示意:
1连接池示意图:
三.安装和配置
开始安装SQL Relay,到 http://sqlrelay.sourceforge.net/ 去下载相应的软件
安装SQL Relay需要先安装Rudiments, 在 http://sqlrelay.sourceforge.net/download.html 可以找到
1.安装rudiments
1
2
3
4
5
| # tar vxzf rudiments-0.33.tar.gz # cd rudiments-0.33 # ./configure --prefix=/usr/local/rudiments # make # make install |
说明没装gcc或者装了gcc,没装g++。
1
| apt-get install g++ |
1
2
3
4
5
6
| # tar zxvf sqlrelay-0.42.tar.gz # cd sqlrelay-0.42 # ./configure --prefix=/usr/local/sqlrelay --with-rudiments-prefix=/usr/local/rudiments --with-mysql-prefix=/usr/local/mysql --with-php-prefix==/usr/local/php # make # make install |
配置文件路径: /usr/local/sqlrelay/etc/sqlrelay.conf。内容如下:
1
2
3
| <!--?xml version= "1.0" ?--> <!-- Regular SQL Relay Instance --> |
1
2
| # export PATH= $PATH :/usr/local/sqlrelay/bin # sqlr-start -id mypool |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| # sqlrsh -id mypool SQLRShell - Version 0.22 Connected to: localhost:9000 as userpool type help; for a help. 0> show processlist; Debugging to: /usr/local/firstworks/ var /sqlrelay/debug/sqlr-listener.15838 Id User Host db Command Time State Info ================================================================ 55 falcon localhost:50257 my Sleep 429 56 falcon localhost:50258 my Sleep 419 57 falcon localhost:50259 my Sleep 13 Rows Returned : 6 Fields Returned : 48 System time : 0 0> |
四.利用PHP API来测试连接池
1.查看php安装目录的扩展目录:
1
2
| # ls /usr/local/php-fcgi/lib/php/extensions/no-debug-non-zts-20090626/ memcache.so sql_relay.so |
1
| extension=sql_relay.so |
1
2
3
4
5
6
| /usr/local/php-fcgi/bin/php -m [PHP Modules] Bcmath ...... sql_relay ........ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| vi php_sqlrelay.php <?php $conn = sqlrcon_alloc( "localhost" ,9000, "" , "userpool" , "pwpool" ,0,1); $cur = sqlrcur_alloc( $conn ); //输出fw_members表中的所有信息 sqlrcur_sendquery( $cur , "select * from fw_members" ); for ( $row =0; $row <sqlrcur_rowcount( $cur ); $row ++){ for ( $col =0; $col <sqlrcur_colcount( $cur ); $col ++){ echo sqlrcur_getfield( $cur , $row , $col ); echo "," ; } echo "\n\n" ; } sqlrcur_free( $cur ); sqlrcon_free( $conn ); |
1
2
| /usr/local/php-fcgi/bin/php php_sqlrelay.php |
本文以SQL RELAY在MYSQL上的应用,应用测试以PHP程序为主.
SQL Relay是一个开源的数据库池连接代理服务器
SQL Relay 的主要特点:
基于web应用的高效、可扩展数据库驱动
分布式的数据库访问
数据库访问的限制
跨平台数据库访问支持
从不同的数据库之间迁移数据
SQL Relay 支持的数据库类型:
* Oracle
* MySQL
* mSQL
* PostgreSQL
* Sybase
* MS SQL Server
* IBM DB2
* Interbase
* Sybase
* SQLite
* Lago
* ODBC
* MS Access
SQL Relay支持客户端接口:
C
C++
Perl
Python
PHP
Ruby
Java
TC
Zope
相关软件:
Rudiments http://sourceforge.net/projects/ ... 8.2.tar.gz/download
SQL Relay http://sourceforge.net/projects/ ... 6.4.tar.gz/download
SQL RELAY的安装依赖Rudiments 库文件,因此在安装sql relay时,首先安装Rudiments 库
Rudiments的安装
- [root@fwphp-cn data]# wget http://sourceforge.net/projects/rudiments/files/rudiments
- /0.28.2/rudiments-0.28.2.tar.gz/download
- [root@fwphp-cn data]#tar xvzf rudiments-0.28.2.tar.gz
- [root@fwphp-cn data]#cd rudiments-0.28.2
- [root@fwphp-cn data]#./configure && make && make install
- [root@fwphp-cn data]#wget http://sourceforge.net/projects/sqlrelay/files/sqlrelay/0.36.4/sqlrelay-0.36.4.tar.gz/download
- [root@fwphp-cn data]#tar xvzf sqlrelay-0.36.4.tar.gz
- [root@fwphp-cn data]#cd sqlrelay-0.36.4
- [root@fwphp-cn data]#./configure \
- --with-mysql-prefix=/home/falcon/mysql \
- --with-php-prefix=/usr/local/php-fcgi \
- --disable-python \
- --disable-perl
- [root@fwphp-cn data]#make && make install
- [root@fwphp-cn data]#cd /usr/local/firstworks/
- [root@fwphp-cn firstworks]# cp etc/sqlrelay.conf.example etc/sqlrelay.conf
- [root@fwphp-cn firstworks]# vi etc/sqlrelay.conf
- <?xml version="1.0"?>
- <!DOCTYPE instances SYSTEM "sqlrelay.dtd">
- <instances>
- <instance id="mysqlpool" port="9002"
socket="/tmp/sqlrelay.socket" dbase="mysql" connections="5"
maxconnections="15" maxqueuelength="5" growby="1" ttl="6
- 0" maxsessioncount="10" endofsession="commit"
sessiontimeout="600" runasuser="nobody" runasgroup="nobody" cursors="5"
authtier="listener" handoff="pass" deni
- edips="" allowedips="" debug="listener" maxquerysize="65536"
maxstringbindvaluelength="4000" maxlobbindvaluelength="71680"
idleclienttimeout="-1" maxlistener
- s="-1" listenertimeout="0" reloginatstart="no" timequeriessec="-1" timequeriesusec="-1">
- <users>
- <user user="userpool" password="pwpool"/>
- </users>
- <connections>
- <connection connectionid="my"
string="user=falcon;password=falcon;db=my;host=127.0.0.1;port=10000;"
metric="1" behindloadbalancer="no"/>
- </connections>
- </instance>
- </instances>
到此sqlrelay.conf文件配置完成,现在来运行sqlrelay:
- [root@fwphp-cn firstworks]# export PATH=$PATH:/usr/local/firstworks/bin
- [root@fwphp-cn firstworks]# sqlr-start -id mysqlpool
- [root@fwphp-cn firstworks]# ps -ef|grep sqlr
- nobody 15601 1 0 14:52 pts/1 00:00:00
sqlr-listener-debug -id mysqlpool -config
/usr/local/firstworks/etc/sqlrelay.conf
- nobody 15603 1 0 14:52 ? 00:00:00
sqlr-connection-mysql -id mysqlpool -connectionid my -config
/usr/local/firstworks/etc/sqlrelay.conf
- nobody 15605 1 0 14:52 ? 00:00:00
sqlr-connection-mysql -id mysqlpool -connectionid my -config
/usr/local/firstworks/etc/sqlrelay.conf
- nobody 15607 1 0 14:52 ? 00:00:00
sqlr-connection-mysql -id mysqlpool -connectionid my -config
/usr/local/firstworks/etc/sqlrelay.conf
- nobody 15609 1 0 14:52 ? 00:00:00
sqlr-connection-mysql -id mysqlpool -connectionid my -config
/usr/local/firstworks/etc/sqlrelay.conf
- nobody 15611 1 0 14:52 ? 00:00:00
sqlr-connection-mysql -id mysqlpool -connectionid my -config
/usr/local/firstworks/etc/sqlrelay.conf
- nobody 15613 1 0 14:52 ? 00:00:00 sqlr-scaler -id mysqlpool -config /usr/local/firstworks/etc/sqlrelay.conf
- root 15619 1 0 14:52 ? 00:00:00 sqlr-cachemanager
- falcon 15749 15645 0 14:55 pts/2 00:00:01 vim php_sqlrelay.php
- root 15831 10721 0 15:06 pts/1 00:00:00 grep sqlr
- [root@fwphp-cn firstworks]# netstat -an -tp|grep 9002
- tcp 0 0 0.0.0.0:9002 0.0.0.0:* LISTEN 15601/sqlr-listener
//mysqlpool为配置文件中定义的连接池实例
- [root@fwphp-cn firstworks]# sqlrsh -id mysqlpool
- SQLRShell - Version 0.22
- Connected to: localhost:9002 as userpool
- type help; for a help.
- 0> show processlist;
- Debugging to: /usr/local/firstworks/var/sqlrelay/debug/sqlr-listener.15838
- Id User Host db Command Time State Info
- ================================================================
- 55 falcon localhost:50257 my Sleep 429
- 56 falcon localhost:50258 my Sleep 419
- 57 falcon localhost:50259 my Sleep 13
- 58 falcon localhost:50260 my Query 0 show processlist
- 59 falcon localhost:50261 my Sleep 433
- 60 root localhost my Sleep 470
- Rows Returned : 6
- Fields Returned : 48
- System time : 0
- 0>
从返回的信息可以看出mysql的有5个连接到my数据库的活动连接,正是sqlrelay配置文件中默认活动连接数量
利用PHP API来测试连接池:
查看php安装目录的扩展目录:
- [falcon@fwphp-cn ~/phpcode]$ ls /usr/local/php-fcgi/lib/php/extensions/no-debug-non-zts-20090626/
- memcache.so qqwry.so sql_relay.so
将sql_relay.so扩展添加到php.ini配置文件中
- [falcon@fwphp-cn ~/phpcode]$ vi /usr/local/php-fcgi/etc/php.ini
- ......
- 989 ;extension=php_xmlrpc.dll
- 990 ;extension=php_xsl.dll
- 991 ;extension=php_zip.dll
- 992
- 993 extension=memcache.so
- 994 extension=qqwry.so
- 995
- 996 extension=sql_relay.so
- ........
测试php是否加载sql_relay.so模块
[falcon@fwphp-cn ~/phpcode]$ /usr/local/php-fcgi/bin/php -m
[PHP Modules]
Bcmath
......
sockets
SPL
sql_relay
SQLite
sqlite3
........
........
到此说明php可以通过SQL RELAY连接池来访问mysql数据库了
- [falcon@fwphp-cn ~/phpcode]$ vi php_sqlrelay.php
- <?
- $conn = sqlrcon_alloc("localhost",9002,"","userpool","pwpool",0,1);
- $cur = sqlrcur_alloc($conn);
- //输出fw_members表中的所有信息
- sqlrcur_sendquery($cur,"select * from fw_members");
- for($row=0;$row<sqlrcur_rowcount($cur);$row++){
- for($col=0;$col<sqlrcur_colcount($cur);$col++){
- echo sqlrcur_getfield($cur,$row,$col);
- echo ",";
- }
- echo "\n\n";
- }
- sqlrcur_free($cur);
- sqlrcon_free($conn);
- ?>
[falcon@fwphp-cn ~/phpcode]$ /usr/local/php-fcgi/bin/php php_sqlrelay.php
1,admin,84383e908a8fb8b5dec1c82ca5a710d2,admin@your.com,,,192.168.234.1,1255863260,0,0,cdedef,,
到处SQL RELAY数据库连接池测试全部结束
参考链接:
SQL RELAY :http://sqlrelay.sourceforge.net/index.html
PHP API :http://sqlrelay.sourceforge.net/sqlrelay/api/php/doc.html
C API :http://sqlrelay.sourceforge.net/ ... lientwrapper.h.html
注:
如果sqlrelay 采用0.41 Rudiments采用0.32 测试时 会报错,估计是Bug
- [falcon@localhost 10:44:57 ~/sqlrelay]$ bin/sqlr-start -id mysqlpool
- Starting listener:
- sqlr-listener -id mysqlpool -config /home/falcon/sqlrelay/etc/sqlrelay.conf
- sqlr-listener error:
- The pid file /home/falcon/sqlrelay/var/sqlrelay/tmp/pids/sqlr-listener-mysqlpool exists.
- This usually means that the sqlr-listener is already running for the
- mysqlpool instance.
- If it is not running, please remove the file and restart.
- sqlr-listener failed to start.
- Thanks to MP3.com for sponsoring:
- Clustered/Replicated database support.
- Perl API.
- Thanks to FeedLounge for sponsoring:
- Query routing and filtering
http://blog.csdn.net/linkor/article/details/6591103