2016-04-23 210 views
0

我搜索了與我的問題相關的答案,但未找到我要找的內容。CentOS 6.7和Windows MSSQL連接 - FreeTDS和PHP

我有Linux CentOS 6.7的服務器。我需要到Windows Small Business Server 2003上連接到MS SQL Server 2005的在我安裝了freetds的CentOS的,我設法使用此命令從終端連接到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"); 

這使我第一個錯誤:

Could not find driver

好吧,我明白了 - 我的PHP 5.3.3安裝沒有安裝了PDO驅動程序dblib。 我的問題:如何在CentOS中安裝pdo_dblib驅動程序?許多教程和答案建議通過這個命令來安裝:

yum install php5-sybase 

但是,這包不存在:

No package php-sybase available.

如果我請與命令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的?我試圖刪除當前版本,但我無法找到辦法做到這一點。此外,我試圖安裝另一個版本,但配置不會改變。

任何意見表示讚賞。

+0

要從PHP連接,您需要FreeTDS和unixODBC。 PHP與unixODBC對話,FreeTDS允許您在unixODBC層和SQL Server本身之間進行連接。你用'isql'來測試連接是否和unixODBC一起工作?這裏的錯誤也不是很有用,你可以在任何地方打開日誌記錄。 – FlipperPA

+0

嗨,謝謝你的回覆。是的,我使用'isql'進行了測試,並且可以看到SQL提示符,以便按照它應該的方式工作。我只需要找到如何使用PDO從PHP連接到SQL Server。 – ManOfHonor

+0

請分享您的freetds.conf,odbcinst.ini和odbc.ini。從終端運行odbcinst -j來獲取配置文件。 – chapskev

回答

0

當我運行命令# odbcinst -j我可以看到這樣的輸出:我freetds.conf

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 

內容:

# $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 

我懷疑我的用戶名會產生問題,因爲我必須在連接到SQ時使用用戶名前面的域名和反斜槓L Server 2005.

更新: 我成功地在PHP中建立連接,現在我可以運行查詢。我在腳本的基礎上增加了這一行:putenv("FREETDSCONF=/usr/local/etc/freetds.conf") 爲什麼這是必需的?