ºìÁªLinuxÃÅ»§
Linux°ïÖú

LinuxʹÓÃperlͨ¹ýunixODBCÁ¬½ÓSQLServer2000

·¢²¼Ê±¼ä:2006-03-16 00:40:55À´Ô´:ºìÁª×÷Õß:yo
Ò»¡¢¹ØÓÚ²âÊÔ»·¾³¼°LinuxÁ¬½ÓSQL Server µÄ˵Ã÷£»

²âÊÔ»·¾³£º
gcc 3.3.3 , kernel 2.6.5-1 , glibc 2.3.3-27 , perl 5.8.3
¹ØÓÚSQL Server˵Ã÷:
MS´ÓÀ´Ã»ÓÐÌṩ¹ýSQLServer for Linux,ËùÒÔ´ó¼ÒÒ²²»ÒªÈ¥³¢ÊÔÔÚLinuxϵͳ°²×°SQLServer,µ«ÊÇ¿ÉÒÔͨ¹ýODBCÁ¬½ÓWindowsϵͳµÄSQLServerÊý¾Ý¿â£»
Windows Server 2003Óû§×¢Ò⣺
ÔÚWindows Server 2003ϵͳÉÏ°²×°µÄSQLServerÊDz»»á´ò¿ª1433¶Ë¿ÚµÄ,ËùÒÔÇëWindows2003Óû§ÔÚ°²×°ÍêSQLServerÖ®ºóÒ»¶¨×°ÉÏSQLServer SP3²¼¶¡

¶þ¡¢ÏÂÔØÏà¹ØÈí¼þ unixODBC¡¢freetdsºÍDBD-ODBC

Ê×ÏÈÐèÒªÏÂÔØ3¸öÎļþ£º
LinuxϵͳµÄODBC
unixODBC-2.2.8.tar.gz ( http://www.unixodbc.org)
Á¬½ÓSQLServer»òSybaseµÄÇý¶¯
freetds-0.62.4.tar.gz ( http://www.freetds.org)
perlµÄODBCÄ£¿é
DBD-ODBC-1.12.tar.gz ( http://cpan.perl.org)
°ÑÏÂÔص½µÄÈý¸öÎļþ·Åµ½Í¬Ò»¸öĿ¼

Èý¡¢°²×°ºÍÅäÖã»

1¡¢°²×°unixODBC

ÒýÓÃ:
# tar vxzf unixODBC-2.2.8.tar.gz
# cd unixODBC-2.2.8
# ./configure --prefix=/usr/local/unixODBC
# make
# make install



2¡¢°²×°freetds

ÒýÓÃ:
# tar vxzf freetds-0.62.4.tar.gz
# cd freetds-0.62.4
# ./configure --prefix=/usr/local/freetds --with-unixodbc=/usr/local/unixODBC --with-tdsver=8.0
# make
# make install


3¡¢°²×°DBD-ODBC

ÔÚ°²×°Ö®Ç°ÒªÏÈÉèÖÃһϻ·¾³±äÁ¿

# export ODBCHOME=/usr/local/unixODBC

Ö®ºó¿ªÊ¼±àÒë°²×°

# tar vxzf DBD-ODBC-1.12.tar.gz
# cd DBD-ODBC-1.12
# perl Makefile.PL
# make
# make install
ËùÓеÄÈí¼þµ½ÏÖÔÚ¶¼°²×°Íê³ÉÁË£¬½ÓÏÂÀ´¾ÍÊÇÅäÖÃÁË

4¡¢ÅäÖÃfreetds

# cd /usr/local/freetds
# vi etc/freetds.conf

ÐÞ¸ÄÒÔϵÄÒ»¶Î£¬²¢°Ñ;È¥µô
¸ÄºÃÖ®ºóµÄÄÚÈÝ£º

[MyServer2k]
host=192.168.0.32
port=1433
tds version=8.0
ÆäÖеÄ[MyServer2k]´ú±íÔÚ¿Í»§¶ËʹÓõķþÎñÆ÷Ãû³Æ£¬host´ú±íSQLServer·þÎñÆ÷µÄIPµØÖ·£¬port´ú±í¶Ë¿Ú£¬²âÊÔÁ¬½Ó£»
# bin/tsql -S MyServer2k -H 192.168.0.32 -p 1433 -U sa -P password
1> use gameDB
2> go
1> select count(*) from t_ip
4
1> quit

Ò»Çж¼ºÜ˳Àû
ÏÖÔÚÀ´ËµÃ÷ÒÔÏÂÕâЩ²ÎÊýµÄ×÷Óã¬-S MyServer2kÊÇÔÚfreetds.confÖж¨ÒåºÃµÄ£¬-H 192.168.0.32·þÎñÆ÷IPµØÖ·£¬ºÍfreetds.confÖеÄÒ»Ö£¬Èç¹ûʹÓÃÁË-S²ÎÊý£¬Õâ¸ö²ÎÊý¿ÉÒÔ²»Óã¬-p 1433ÓÃÀ´Ö¸¶¨¶Ë¿ÚÊÇ1433 £¬-U saÓÃÀ´Ö¸¶¨Á¬½ÓÊý¾Ý¿âµÄÓû§Ãû£¬-P passwordÓÃÀ´Ö¸¶¨Á¬½ÓÊý¾Ý¿âµÄÃÜÂë¡£

5¡¢ÅäÖÃunixODBC

# cd /usr/local/unixODBC

ÏòODBCÌí¼ÓSQLServerÇý¶¯

# vi etc/odbcinst.ini

дÈëÈçÏÂÄÚÈÝ£º

[TDS] ;Çý¶¯Ãû³Æ
Description = MS-SQLServer ;ÃèÊö
Driver = /usr/local/freetds/lib/libtdsodbc.so ;Çý¶¯³ÌÐò
Setup = /usr/local/freetds/lib/libtds.so ;²»ÒªÎÊÎÒ£¬ÎÒÒ²²»ÖªµÀÕâÊÇʲô
FileUsage = 1

±£´æÍ˳ö
Ìí¼ÓDSN

# vi etc/odbc.ini

дÈëÈçÏÂÄÚÈÝ

[123] ;DSNÃû³Æ
Driver = TDS ;ODBCÇý¶¯
Server = 192.168.0.32 ;·þÎñÆ÷IP
Database = gameDB ;ҪʹÓõÄÊý¾Ý¿â
Port = 1433 ;¶Ë¿Ú
Socket =
Option =
Stmt =

±£´æ²¢Í˳ö£¬²âÊÔODBCµÄÁ¬½Ó

# bin/isql -v 123 sa password
SQL> select count(*) from t_ip
+----------+
| |
+----------+
| 4 |
+----------+
SQL> quit
OK£¬²âÊÔͨ¹ý£¬¿ªÊ¼²âÊÔperlÄÜ·ñÕýȷʹÓÃODBC
ÔÚʹÓÃperlÁ¬½ÓODBC֮ǰҪ×öЩ׼±¸¹¤×÷
°ÑODBCµÄ¹²Ïí¿â¶¼¸´ÖƵ½/usr/libĿ¼

# cp /usr/local/unixODBC/lib/libodbc.* /usr/lib

6¡¢perl½Å±¾²âÊÔÄÜ·ñ³É¹¦£»

# vi test.pl

ÄÚÈÝÈçÏÂ

#!/usr/bin/perl
use DBI;
$dbh=DBI->connect(¡¯dbi:ODBC:123¡¯,¡¯sa¡¯,¡¯password¡¯);
my $sth=$dbh->prepare("select * from t_ip";
$sth->execute();
while (@data=$sth->fetchrow_array()){
print "$data[0] $data[1] $data[2]\n";
}

±£´æÍ˳ö

# perl test.pl

Èç¹û¿ÉÒÔ¿´µ½Êý¾Ý¿âÖеļǼ£¬¾Í˵Ã÷¿ÉÒÔÕý³£Ê¹ÓÃODBC perlÁË¡£
¼ÈÈ»ÒѾ­¿ÉÒÔʵÏÖperlͨ¹ýODBCÀ´Á¬½ÓSQLServerÁË£¬ÄÇôͬÑù¿ÉÒÔʹÓÃPHP+ODBC+SQLServer£¬¾ßÌå·½·¨Çë²éѯPHP¹Ù·½ÍøÕ¾¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2 ÌõÆÀÂÛ

  1. ÁúÓî ÓÚ 2009-03-01 09:37:04·¢±í:

    ÎÒŪºÃÁËΪʲô²»Ö§³ÖÖÐÎÄ?ÓÃtsql Á¬½Ó ÖÐÎÄÏÔʾÕý³£, ¿ÉÊÇÓÃisqlÁ¬½ÓÖÐÎÄÂÒÂ룡¶¼ÊÇ?? Îʺţ¡

  2. ÏèÏè ÓÚ 2006-07-31 10:04:05·¢±í:

    ѧϰÁË