Total Pageviews

Wednesday, 8 May 2013

sqlrelay 的安装配置

一.什么是sqlrelay?
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
安装rudiments会报configure: error: C++ compiler cannot create executables错误,
说明没装gcc或者装了gcc,没装g++。
1
apt-get install g++
2.安装sqlrelay:
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
3.配置sqlrelay
配置文件路径: /usr/local/sqlrelay/etc/sqlrelay.conf。内容如下:
1
2
3
<!--?xml version="1.0"?-->
 
<!-- Regular SQL Relay Instance -->
4.启动sqlrelay
1
2
# export PATH=$PATH:/usr/local/sqlrelay/bin
# sqlr-start -id mypool
5.服务测试
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>
从返回的信息可以看出mysql的有3个连接到my数据库的活动连接,正是sqlrelay配置文件中默认活动连接数量.
四.利用PHP API来测试连接池
1.查看php安装目录的扩展目录:
1
2
# ls /usr/local/php-fcgi/lib/php/extensions/no-debug-non-zts-20090626/
memcache.so      sql_relay.so
2.将sql_relay.so扩展添加到php.ini配置文件中
1
extension=sql_relay.so
3.测试php是否加载sql_relay.so模块
1
2
3
4
5
6
/usr/local/php-fcgi/bin/php -m
[PHP Modules]
Bcmath
......
sql_relay
........
4.到此说明php可以通过SQL RELAY连接池来访问mysql数据库了
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);
执行php_sqlrelay.php文件
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的安装
  1. [root@fwphp-cn data]# wget http://sourceforge.net/projects/rudiments/files/rudiments
  2. /0.28.2/rudiments-0.28.2.tar.gz/download

  3. [root@fwphp-cn data]#tar xvzf rudiments-0.28.2.tar.gz
  4. [root@fwphp-cn data]#cd rudiments-0.28.2
  5. [root@fwphp-cn data]#./configure && make && make install
复制代码
SQL RELAY的安装:
  1. [root@fwphp-cn data]#wget http://sourceforge.net/projects/sqlrelay/files/sqlrelay/0.36.4/sqlrelay-0.36.4.tar.gz/download

  2. [root@fwphp-cn data]#tar xvzf sqlrelay-0.36.4.tar.gz
  3. [root@fwphp-cn data]#cd sqlrelay-0.36.4
  4. [root@fwphp-cn data]#./configure  \
  5. --with-mysql-prefix=/home/falcon/mysql \
  6. --with-php-prefix=/usr/local/php-fcgi \
  7. --disable-python \
  8. --disable-perl
  9. [root@fwphp-cn data]#make && make install

  10. [root@fwphp-cn data]#cd /usr/local/firstworks/
  11. [root@fwphp-cn firstworks]# cp etc/sqlrelay.conf.example etc/sqlrelay.conf
复制代码
配置sqlreplay.conf配置文件如下:
  1. [root@fwphp-cn firstworks]# vi etc/sqlrelay.conf
  2. <?xml version="1.0"?>
  3. <!DOCTYPE instances SYSTEM "sqlrelay.dtd">
  4. <instances>
  5.     <instance id="mysqlpool" port="9002" socket="/tmp/sqlrelay.socket" dbase="mysql" connections="5" maxconnections="15" maxqueuelength="5" growby="1" ttl="6
  6. 0" maxsessioncount="10" endofsession="commit" sessiontimeout="600" runasuser="nobody" runasgroup="nobody" cursors="5" authtier="listener" handoff="pass" deni
  7. edips="" allowedips="" debug="listener" maxquerysize="65536" maxstringbindvaluelength="4000" maxlobbindvaluelength="71680" idleclienttimeout="-1" maxlistener
  8. s="-1" listenertimeout="0" reloginatstart="no" timequeriessec="-1" timequeriesusec="-1">
  9.         <users>
  10.             <user user="userpool" password="pwpool"/>
  11.         </users>
  12.         <connections>
  13.             <connection connectionid="my" string="user=falcon;password=falcon;db=my;host=127.0.0.1;port=10000;" metric="1" behindloadbalancer="no"/>
  14.         </connections>
  15.     </instance>
  16. </instances>
复制代码
注:蓝色文字为需要修改的参数

到此sqlrelay.conf文件配置完成,现在来运行sqlrelay:
  1. [root@fwphp-cn firstworks]# export PATH=$PATH:/usr/local/firstworks/bin
  2. [root@fwphp-cn firstworks]# sqlr-start -id mysqlpool 

  3. [root@fwphp-cn firstworks]# ps -ef|grep sqlr
  4. nobody   15601     1  0 14:52 pts/1    00:00:00 sqlr-listener-debug -id mysqlpool -config /usr/local/firstworks/etc/sqlrelay.conf
  5. nobody   15603     1  0 14:52 ?        00:00:00 sqlr-connection-mysql -id mysqlpool -connectionid my -config /usr/local/firstworks/etc/sqlrelay.conf
  6. nobody   15605     1  0 14:52 ?        00:00:00 sqlr-connection-mysql -id mysqlpool -connectionid my -config /usr/local/firstworks/etc/sqlrelay.conf
  7. nobody   15607     1  0 14:52 ?        00:00:00 sqlr-connection-mysql -id mysqlpool -connectionid my -config /usr/local/firstworks/etc/sqlrelay.conf
  8. nobody   15609     1  0 14:52 ?        00:00:00 sqlr-connection-mysql -id mysqlpool -connectionid my -config /usr/local/firstworks/etc/sqlrelay.conf
  9. nobody   15611     1  0 14:52 ?        00:00:00 sqlr-connection-mysql -id mysqlpool -connectionid my -config /usr/local/firstworks/etc/sqlrelay.conf
  10. nobody   15613     1  0 14:52 ?        00:00:00 sqlr-scaler -id mysqlpool -config /usr/local/firstworks/etc/sqlrelay.conf
  11. root     15619     1  0 14:52 ?        00:00:00 sqlr-cachemanager
  12. falcon   15749 15645  0 14:55 pts/2    00:00:01 vim php_sqlrelay.php
  13. root     15831 10721  0 15:06 pts/1    00:00:00 grep sqlr

  14. [root@fwphp-cn firstworks]# netstat -an -tp|grep 9002
  15. tcp  0  0 0.0.0.0:9002      0.0.0.0:*        LISTEN      15601/sqlr-listener
复制代码
看到有sqlr-*的进程 和 sqlr-listener的9002端口存在,则说明配置成功了。到此就可以测试sql relay了

//mysqlpool为配置文件中定义的连接池实例
  1. [root@fwphp-cn firstworks]# sqlrsh -id mysqlpool
  2. SQLRShell - Version 0.22
  3.         Connected to: localhost:9002 as userpool

  4.         type help; for a help.

  5. 0> show processlist;
  6. Debugging to: /usr/local/firstworks/var/sqlrelay/debug/sqlr-listener.15838
  7. Id User   Host            db Command Time State Info          
  8. ================================================================
  9. 55 falcon localhost:50257 my Sleep   429                      
  10. 56 falcon localhost:50258 my Sleep   419                      
  11. 57 falcon localhost:50259 my Sleep   13                        
  12. 58 falcon localhost:50260 my Query   0          show processlist
  13. 59 falcon localhost:50261 my Sleep   433                      
  14. 60 root   localhost       my Sleep   470                      
  15.         Rows Returned   : 6
  16.         Fields Returned : 48
  17.         System time     : 0

  18. 0>
复制代码

从返回的信息可以看出mysql的有5个连接到my数据库的活动连接,正是sqlrelay配置文件中默认活动连接数量


利用PHP API来测试连接池:

查看php安装目录的扩展目录:
  1. [falcon@fwphp-cn ~/phpcode]$ ls /usr/local/php-fcgi/lib/php/extensions/no-debug-non-zts-20090626/
  2. memcache.so   qqwry.so      sql_relay.so
复制代码


将sql_relay.so扩展添加到php.ini配置文件中

  1. [falcon@fwphp-cn ~/phpcode]$ vi /usr/local/php-fcgi/etc/php.ini
  2. ......
  3. 989 ;extension=php_xmlrpc.dll
  4. 990 ;extension=php_xsl.dll
  5. 991 ;extension=php_zip.dll
  6. 992
  7. 993 extension=memcache.so
  8. 994 extension=qqwry.so
  9. 995
  10. 996 extension=sql_relay.so
  11. ........
复制代码



测试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数据库了
  1. [falcon@fwphp-cn ~/phpcode]$ vi php_sqlrelay.php
  2.   <?
  3. $conn = sqlrcon_alloc("localhost",9002,"","userpool","pwpool",0,1);

  4. $cur = sqlrcur_alloc($conn);
  5. //输出fw_members表中的所有信息
  6. sqlrcur_sendquery($cur,"select * from fw_members");

  7. for($row=0;$row<sqlrcur_rowcount($cur);$row++){
  8.         for($col=0;$col<sqlrcur_colcount($cur);$col++){
  9.                 echo sqlrcur_getfield($cur,$row,$col);
  10.                 echo ",";
  11.         } 
  12.         echo "\n\n";
  13. }
  14. sqlrcur_free($cur);
  15. sqlrcon_free($conn);
  16. ?>
复制代码
执行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
  1. [falcon@localhost 10:44:57 ~/sqlrelay]$ bin/sqlr-start -id mysqlpool

  2. Starting listener:
  3.   sqlr-listener -id mysqlpool -config /home/falcon/sqlrelay/etc/sqlrelay.conf

  4. sqlr-listener error:
  5.         The pid file /home/falcon/sqlrelay/var/sqlrelay/tmp/pids/sqlr-listener-mysqlpool exists.
  6.         This usually means that the sqlr-listener is already running for the
  7.         mysqlpool instance.
  8.         If it is not running, please remove the file and restart.

  9. sqlr-listener failed to start.


  10. Thanks to MP3.com for sponsoring:
  11.         Clustered/Replicated database support.
  12.         Perl API.
  13. Thanks to FeedLounge for sponsoring:
  14.         Query routing and filtering
from http://bbs.linuxtone.org/thread-4538-1-1.html
http://blog.csdn.net/linkor/article/details/6591103