[참고자료] 공개커넥션풀 프로그램

문태준의 이미지

공개커넥션 풀 프로그램 설명
http://tunelinux.pe.kr
http://database.sarang.net
2004.12.24
문태준

커넥션 풀은 프로그램(java, c, php 등등)과 db사이에서 db연결을 pool로 만들어 제어해주는 것입니다.
PHP에서 사용할 수 있는 공개 커넥션 풀 프로그램을 찾은 것이 있어서 여기 올립니다.
여기서는 간단한 사용법만 설명합니다. 자세한건 설명서와 옵션을 열심히 보고 고민해야 할듯.

구조는 다음과 같습니다.
db접속요청 -> 커넥션 풀 대몬 -> db 접속
db접속요청이 늘어나도 db 프로세스 갯수는 일정합니다. 커넥션 풀을 이용하여 자원관리를 하는 것입니다.

SQL Relay
http://sqlrelay.sourceforge.net/

SQL Relay is a persistent database connection pooling, proxying and load balancing system for Unix and Linux.

지원 DB는 다음과 같습니다.
* Oracle
* MySQL
* mSQL
* PostgreSQL

* Sybase
* MS SQL Server
* IBM DB2
* Interbase

* Sybase
* SQLite
* Lago
* ODBC

지원 api
* C
* C++
* Perl

* Python
* PHP
* Ruby

* Java
* TCL
* Zope

설치방법
http://sqlrelay.sourceforge.net/download.html 에서 SRPM을 이용하여 새로 RPM을 만들었습니다.
설치설명서를 참고하면 여러가지 방법을 이용할 수 있습니다. http://sqlrelay.sourceforge.net/sqlrelay/installing.html

SRPM을 설치하고 RPM을 만들때 여러가지 RPM파일을 요구합니다. 나오는 내용을 보고 설치해주면 됩니다.
gtk-devel 등등.

PHP를 사용하는데 소스로 설치했더니 php-config (php 확장모듈 만들때 쓰지요?) 를 못 찾더군요. 아마도 경로를 지정해주면 될듯한데 그냥 귀찮아서 php rpm을 설치하였습니다. 임시테스팅이 목적이었으므로.

설치시 불필요한 db, api를 명시해주지 않으면 rpm을 만들다가 에러가 납니다.

rpm -ta -without oracle -without zope sqlrelay-xxx.tar.gz

위와 같은 식입니다.

rpmbuild -without db2 -without freetds --without oracle -without zope sqlrelay.spec

나온 파일을 필요에 따라 설치합니다.

[root@kdu i386]# ls -1
sqlrelay-0.35-1.i386.rpm
sqlrelay-client-devel-c-0.35-1.i386.rpm
sqlrelay-client-devel-c++-0.35-1.i386.rpm
sqlrelay-client-mysql-0.35-1.i386.rpm
sqlrelay-client-postgresql-0.35-1.i386.rpm
sqlrelay-client-runtime-c-0.35-1.i386.rpm
sqlrelay-client-runtime-c++-0.35-1.i386.rpm
sqlrelay-clients-0.35-1.i386.rpm
sqlrelay-config-gtk-0.35-1.i386.rpm
sqlrelay-doc-0.35-1.i386.rpm
sqlrelay-man-0.35-1.i386.rpm
sqlrelay-mysql-0.35-1.i386.rpm
sqlrelay-perl-0.35-1.i386.rpm
sqlrelay-php-0.35-1.i386.rpm

# rpm -ivh *.rpm

# rpm -ql sqlrelay | grep bin
/usr/bin/sqlr-cachemanager
/usr/bin/sqlr-listener
/usr/bin/sqlr-listener-debug
/usr/bin/sqlr-scaler
/usr/bin/sqlr-start
/usr/bin/sqlr-stop

# rpm -ql sqlrelay-php
/usr/lib/php4/sql_relay.so -> 이게 php모듈이지요.
/usr/share/pear/DB/sqlrelay.php

sqlrelay rpm으로 설치를 하면 /etc/sqlrelay.conf 파일을 만들어 설정합니다.
샘플은 /etc/sqlrelay.conf.example 입니다.

# ls /etc/sqlrelay.*
/etc/sqlrelay.conf /etc/sqlrelay.conf.example /etc/sqlrelay.dtd

아래와 같이 임시로 세팅했습니다.

mysql 의 경우 최소 수정할것
id : instance id. sqlr-start 할때 필요함
dbase : mysql
connections : 초기 연결할 갯수
maxconnections : 최대 접속자

users 는 해당 api에서 접속할때 필요한 사용자, 비밀번호입니다. 나중 php소스코드보면 이해감.

connections는 커넥션 풀을 만들때 사용하는 db, user 등의 정보임.

<?xml version="1.0"?>
<!DOCTYPE instances SYSTEM "sqlrelay.dtd">

<instances>

<instance id="example" port="9000" socket="/tmp/example.socket" dbase="mysql" connections="20" maxconnections="500" maxqueuelength="5" growby="1" ttl="60" endofsession="commit" sessiontimeout="600" runasuser="nobody" runasgroup="nobody" cursors="5" authtier="listener" handoff="pass" deniedips="" allowedips="" debug="none">
<users>
<user user="user1" password="password1"/>
<user user="user2" password="password2"/>
<user user="user3" password="password3"/>
</users>
<connections>
<connection connectionid="db1" string="user=aaaat;password=aaaa;db=test;host=localhost;port=3306;socket=/var/lib/mysql/mysql.sock;" metric="1"/>
<connection connectionid="db2" string="user=aaaa;password=aaaa;db=test;host=localhost;port=3306;socket=/var/lib/mysql/mysql.sock;" metric="1"/>
<connection connectionid="db3" string="user=aaaa;password=aaaa;db=test;host=localhost;port=3306;socket=/var/lib/mysql/mysql.sock;" metric="1"/>

</connections>
</instance>

</instances>

이제 커넥션풀을 시작합니다.
# sqlr-start -id example -config /etc/sqlrelay.conf

# ps auxw | grep sqlr
nobody 20725 0.0 0.2 3600 1408 ? S 16:11 0:00 sqlr-listener -id example -config /etc/sqlrelay.conf
nobody 20728 0.0 0.3 4288 1720 ? S 16:11 0:00 sqlr-connection-mysql -id example -connectionid db1 -config /etc/sqlr
nobody 20731 0.0 0.3 4292 1720 ? S 16:11 0:00 sqlr-connection-mysql -id example -connectionid db1 -config /etc/sqlr
nobody 20734 0.0 0.3 4292 1720 ? S 16:11 0:00 sqlr-connection-mysql -id example -connectionid db1 -config /etc/sqlr
nobody 20785 0.0 0.3 4288 1720 ? S 16:11 0:00 sqlr-connection-mysql -id example -connectionid db5 -config /etc/sqlr
nobody 20787 0.0 0.2 3572 1368 ? S 16:11 0:00 sqlr-scaler -id example -config /etc/sqlrelay.conf
root 20793 0.0 0.2 3488 1072 ? S 16:11 0:00 sqlr-cachemanager

위에서 보듯이 몇가지 프로세서로 이루어집니다.

끝내기
# sqlr-stop
id를 지정하지 않으면 몽땅 죽입니다.

mysqladmin pro 에서 보면 connections 만큼 mysql 과 연결되어있습니다.

샘플코드 php

# cat sql.php
<?
dl("sql_relay.so");

$con=sqlrcon_alloc("localhost",9000,"","user1","password1",0,1) or die ("connect error");
$cur=sqlrcur_alloc($con);

echo "sendquery";
sqlrcur_sendQuery($cur,"select * from test");

#sqlrcur_sendFileQuery($cur,"/usr/local/myprogram/sql","myquery.sql");
echo "endsession";
sqlrcon_endSession($con);

sqlrcur_sendQuery($cur,"select * from test");
sqlrcon_endSession($con);

sqlrcur_free($cur);
sqlrcon_free($con);
?>

사이트에 나온 소스코드에서는 아래와 같이 나와있는데 " 를 넣어주지 않으면 에러가 납니다. ; 도 빠져있구요.
dl(sql_relay.so)

** 기타참고사이트
http://sqlb.sourceforge.net/frameset.html
The SQLB project is used to improve SQL requests to a database. It supports MySQL, PostgreSQL and Oracle(tm).

It is a set of deamon programs that make multiple permanent connection to one or more database when they start. Then an external program, previously linked with the sqlb client library, can send SQL requests to these deamons and get the result without any need to make a connection/disconnection. The SQLB PHP and Perl modules provided here with SQLB are some examples of such programs.

** phpschool 참고자료
http://www.phpschool.com/bbs2/inc_view.html?id=6888&code=tnt2 : Too many connections 에러와 DB pool 사용
http://www.phpschool.com/bbs2/inc_view.html?id=8132&code=tnt2 : 새로운 요청 처리 방법
http://www.phpschool.com/bbs2/inc_view.html?id=10093&code=tnt2 : 데이터베이스 풀링에 대한 sqlrelay 설치 문서

Forums: 
익명 사용자의 이미지

taejun I agree with yor argument.

문태준의 이미지

who are you?

---------------------------
문태준
http://groups.google.co.kr/group/sysadminstudy 시스템어드민 공부모임
http://tunelinux.pe.kr
http://database.sarang.net

chronon의 이미지

taejun wrote:
who are you?

요즘 한창 도는 봇인가 봅니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.