CentOS 6.7和Windows MSSQL连接 - FreeTDS和PHP

时间:2016-04-23 11:03:49

标签: tsql centos freetds unixodbc

我搜索了与我的问题相关的答案,但未找到我正在寻找的内容。

我有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?我试图删除当前版本,但我找不到这样做的方法。此外,我尝试安装另一个版本,但配置不会更改。

感谢任何建议。

1 个答案:

答案 0 :(得分:0)

当我运行命令# 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 = DOMAIN\MyUsername
Password = MyPassword

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

# isql -v MSSQL "DOMAIN\MyUsername" MyPassword

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

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

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

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