CentOS 6.7 和 Windows MSSQL 连接 - FreeTDS 和 PHP



我搜索了与我的问题相关的答案,但没有找到我要找的东西。

我有装有 Linux CentOS 6.7 的服务器。我需要连接到 Windows Small Business Server 2003 上的 MS SQL Server 2005。在 CentOS 中,我安装了 FreeTDS,并设法使用以下命令从终端连接到 SQL Server:

# TDSVER=7.0 tsql -H ServerIPAdress -p 1433 -U username -P password

据我所知,这个命令绕过freetds.conf中的设置。现在我需要从 PHP 脚本连接。我正在使用PDO扩展,我尝试了这个DSN字符串:

$db = new PDO("dblib:version=7.0;host=ServerIPAdress;dbname=Database;","username","password");

这导致我遇到第一个错误:

找不到驱动程序

好的,我明白了 - 我的 PHP 5.3.3 安装没有安装 PDO 驱动程序 dblib。我的問題:如何在 CentOS 安裝pdo_dblib驅動程式?许多教程和答案建议通过以下命令进行安装:

yum install php5-sybase

但是该包不存在:

没有可用的软件包 php-sybase。

如果我使用命令检查 tsql 配置

tsql -C

我得到这些输出:

                       Version: freetds v0.95
        freetds.conf directory: /usr/local/etc
MS db-lib source compatibility: no
   Sybase binary compatibility: no
                 Thread safety: yes
                 iconv library: yes
                   TDS version: 5.0
                         iODBC: no
                      unixodbc: yes

据我了解,我需要配置 FreeTDS:

./configure --enable-msdblib --with-pdo-dblib=/usr/local

但是,如何在安装后配置 FreeTDS?我试图删除当前版本,但我找不到办法做到这一点。我也尝试安装另一个版本,但配置没有改变。

任何建议不胜感激。

当我运行命令# odbcinst -j时,我可以看到以下输出:

unixODBC 2.3.0
DRIVERS ...........: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLSETPOSIROW Size.: 8

我的freetds.conf的内容:

#   $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same 
# name is found in the installation directory.  
#
# For information about the layout of this file and its settings, 
# see the freetds.conf manpage "man freetds.conf".  
# Global settings are overridden by those in a database
# server specific section
[global]
    # TDS protocol version
    tds version = 7.0
    # Whether to write a TDSDUMP file for diagnostic purposes
    # (setting this to /tmp is insecure on a multi-user system)
;   dump file = /tmp/freetds.log
;   debug flags = 0xffff
    # Command and connection timeouts
;   timeout = 10
;   connect timeout = 10
    # If you get out-of-memory errors, it may mean that your client
    # is trying to allocate a huge buffer for a TEXT field.  
    # Try setting 'text size' to a more reasonable limit 
    text size = 64512
# A typical Sybase server
[egServer50]
    host = symachine.domain.com
    port = 5000
    tds version = 7.0
# A typical Microsoft server
[MSServer]
    host = 192.168.1.55
    port = 1433
    tds version = 7.0

odbcinst.ini

[FreeTDS]
Description = FreeTDS v7.0
Driver = /usr/local/lib/libtdsodbc.so
UsageCount = 1

ODBC.ini

[MSSQL]
Driver = FreeTDS
Description = MSSQL Server Connection
TDS_Version = 7.0
Trace = No
Server = MSServer
Port = 1433
Database = MyDatabase
Username = DOMAINMyUsername
Password = MyPassword

当我指定用户名和密码时,我可以与isql命令建立连接:

# isql -v MSSQL "DOMAINMyUsername" MyPassword

但是如果我省略用户名和密码是ql生成错误:

[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Adaptive Server connection failed

我怀疑我的用户名有问题,因为我在连接到 SQL Server 2005 时必须在用户名之前使用域名后跟反斜杠。

更新:我成功地在 PHP 中建立了连接,现在我可以运行查询了。我在脚本顶部添加了这一行:putenv("FREETDSCONF=/usr/local/etc/freetds.conf")为什么有必要这样做?

最新更新