linux下如何配置SQLSERVER ODBC

2019-12-23,,

一 测试环境

操作系统 Red Hat As 5.5

数据库   

SQLServer2000 installed in windows xp professional,我假定数据库有用户autotest 密码是autotest,数据库文件是atsdb

注意:   

1 MS从来没有提供过SQLServer for Linux,所以大家也不要去尝试在Linux系统安装SQLServer,但是可以通过ODBC连接

Windows系统的SQLServer数据库;

2 安装的SQLServer2000是不会打开1433端口的,所以请Windows xp 用户在安装完SQLServer之后一定装上SQLServer SP3布丁,可以使用命令netstat -a 来查看是否打开1433端口

3 对于windows xp,缺省防火墙是不让1433端口来通讯的,所以必须关掉windows的防火墙,这个问题折腾我一天,老是连接不成功,我还以为是odbc软件的配置 问题,教训呀。最可气的是在别的windows机器使用sqlserver的命令isql能够访问数据库,(不用关闭防火墙)。通过抓包工具发现根本就不 通过1433端口通讯,而是通过445或139内部端口通讯呀,真要命呀,看来windowns东西是好,但是也太霸道了呀

4 在xp sp2安装sqlserver2000时必须要打chs_sql2ksp3.exe补丁,如果没有这个补丁,telnet ip 1433会连接不了,当然从远程客户端连接不上,就这个问题折腾一个下午,晕呀

Linux系统的ODBC   

      unixODBC-2.2.8.tar.gz ( http://www.unixodbc.org)

连接SQLServer或Sybase的驱动           

     freetds-0.62.4.tar.gz ( http://www.freetds.org)

二 安装配置(必须在root下用户安装)

1、安装unixODBC

# tar vxzf unixODBC-2.2.8.tar.gz
# cd unixODBC-2.2.8
# ./configure --prefix=/usr/local/unixODBC --enable-gui=no
# 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、配置freetds

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

修改以下的一段,并把;去掉

改好之后的内容:

[global]
        # TDS protocol version
        tds version = 8.0
[SQLServer2000]
        host = 192.168.1.110
        port = 1433
        tds version = 8.0
        client charset = ISO-8859-1
[SQLServerMDM]
        host =SQLCNBJ015.corp.novocorp.net
        port =3888
#       instance = MDM_StagingDB
        tds version = 8.0
        client charset = UTF-8

其中的[SQLServer2000]代表在客户端使 用的服务器名称(在tsql命令中要加入 -S SQLServer2000),host代表SQLServer服务器的IP地址,port代表端口.client charset 是客户端使用的字符集,我的机器使用utf-8会报连接失败的错误,发现tsql使用ISO-8859-1字符集可以连接成功,所以这儿也改成这样

测试连接:

$ tsql -S SQLServer2000 -U autotest -P autotest
./tsql -S SQLServerDEV -U SELASReader -P Only4SELAS
locale is "en_US"
locale charset is "ISO-8859-1"
Msg 2403, Level 16, State 0, Server OpenClient, Line 0
WARNING! Some character(s) could not be converted into client"s character set. Unconverted bytes were changed to question marks ("?").
1> use atsdb
2> go
1> select * from auth_function
2> go
FUNC_ID NAME    ENGLISH_NAME    PARENT_ID       IS_LOG NOTES
1>
[siebel@appcnbj035 bin]$ ./tsql -S SQLServerMDM -U SELASReader -P Only4SELAS
locale is "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=zh_CN.GB18030;LC_TIME=zh_CN.GB18030;LC_COLLATE=zh_CN.GB18030;LC_MONETARY=zh_CN.GB18030;LC_MESSAGES=zh_CN.GB18030;LC_PAPER=zh_CN.GB18030;LC_NAME=zh_CN.GB18030;LC_ADDRESS=zh_CN.GB18030;LC_TELEPHONE=zh_CN.GB18030;LC_MEASUREMENT=zh_CN.GB18030;LC_IDENTIFICATION=zh_CN.GB18030"
locale charset is "UTF-8"
Msg 5703, Level 0, State 1, Server SQLCNBJ015\I01, Line 1
Changed language setting to us_english.
1> select count(*) from V_Standard_STAFF ;
2> go;

这说明安装freetds成功

4、配置unixODBC

# cd /usr/local/unixODBC/etc

向ODBC添加SQLServer驱动

# vi /usr/local/unixODBC/etc odbcinst.ini

写入如下内容:

[ODBC]
Trace           = Yes
TraceFile       = /tmp/sql.log
ForceTrace      = Yes
Pooling         = No
[SQLSERVER]
Description     = SQLSERVER
Driver          = /usr/local/freetds/lib/libtdsodbc.so
Setup           = /usr/local/freetds/lib/libtds.so
UsageCount      = 1
CPTimeout       = 5
CPReuse         = 5
FileUsage       = 1

保存退出

添加DSN

# vi /usr/local/unixODBC/etc/odbc.ini

写入如下内容

[ATSDB]
Driver          = SQLSERVER  
Description     = SQLSERVER
Server          = 192.168.1.110
Database        = atsdb
Port            = 1433
[ATSDB]         = SQLSERVER
Description     = SQLSERVER
Server          = SQLCNBJ015.corp.novocorp.net
Database        = MDM_StagingDB
Port            = 3888

注意Driver驱动要写odbcinst.ini中的驱动名称,Database是数据库的名称

保存并退出。

测试ODBC的连接

# bin/isql -v ATSDB autotest autotest
Cd /usr/local/unixODBC/bin
./isql -v ATSDB SELASReader Only4SELAS
SQL>
SQL> quit

《linux下如何配置SQLSERVER ODBC.doc》

下载本文的Word格式文档,以方便收藏与打印。