2013-01-24 121 views
2

我想打開一個連接到SQL Server 2008在Windows計算機上從Apache Linux服務器通過PHP。我已經打開了防火牆上相應的端口,但我得到最模糊的錯誤Apache不會連接到SQL Server與PHP

警告:mssql_connect()[function.mssql-連接]:無法連接到服務器:xxx.xxx.xx. XXX,XXXX

有:

$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon 
$myUser = "un"; 
$myPass = "pw"; 
$myDB = "db"; 

//connection to the database 
$dbhandle = mssql_connect($myServer, $myUser, $myPass) 
    or die(mssql_get_last_message()); 

有一些方法,以獲得更詳細的錯誤訊息?或者我可以測試兩種計算機是否都在進行通信,以便我可以嘗試開始本地化問題?

+0

他們有防火牆嗎?檢查該服務器上給定的端口。 Telnet到該端口以檢查它是否接受連接。 – PoX

+0

也是,是否接受遠程連接的mssql服務器設置? http://blogs.msdn.com/b/walzenbach/archive/2010/04/14/how-to-enable-remote-connections-in-sql-server-2008.aspx –

+0

'sql'或' apache'標籤適合於這個問題。 –

回答

2

下面是我用來連接PHP到MSSQL從Ubuntu機器到Windows SQL Server的代碼,我不知道它是否會幫助你,但這段代碼現在成功運行,所以我知道它的工作原理我們的環境...

正如你所看到的,我使用PDO而不是mssql_ *函數。在Ubuntu上,我需要安裝php5-sybase軟件包來獲取dblib驅動程序。

PHP:

<?php 
try{ 
    $con = new PDO("dblib:dbname=$dbname;host=$servername", $username, $password); 
}catch(PDOException $e){ 
    echo 'Failed to connect to database: ' . $e->getMessage() . "\n"; 
    exit; 
} 
?> 

/etc/odbc.ini

# Define a connection to the MSSQL server. 
# The Description can be whatever we want it to be. 
# The Driver value must match what we have defined in /etc/odbcinst.ini 
# The Database name must be the name of the database this connection will connect to. 
# The ServerName is the name we defined in /etc/freetds/freetds.conf 
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf 
[mssqldb] 
Description    = MSSQL Server 
Driver     = freetds 
Database    = MyDB 
ServerName    = mssqldb 
TDS_Version    = 8.0 

/etc/odbcinst.ini

# Define where to find the driver for the Free TDS connections. 
[freetds] 
Description  = MS SQL database access with Free TDS 
Driver   = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so 
Setup   = /usr/lib/i386-linux-gnu/odbc/libtdsS.so 
UsageCount  = 1 

/etc/freetds/freetds.conf

[global] 
     # 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 

# Define a connection to the MSSQL server. 
[mssqldb] 
     host = mssqldb 
     port = 1433 
     tds version = 8.0 
+0

謝謝你。我希望我可以使用Ubuntu。我已經嘗試了很多次,爲PHP安裝ms sql PDO並無濟於事。我知道你是一個unbuntu的人,但也許你知道哪裏有一個可用的庫.. – 1252748

+0

@thomas - 你使用的是什麼Linux發行版? –

+0

不得不檢查...對不起,它花了一段時間:CentOS 6,和它的64位 – 1252748

2

今天我遇到了同樣的問題。這個工作對我來說:

取而代之的是

$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon 

試試這個:

$myServer = "mssqldb"; //must correspond to [entry] in freetds.conf file 

在你的情況,我會重命名該條目,並使用完全合格的主機名的條目只是爲了清楚起見

# Define a connection to the MSSQL server. 
[mssqldbAlias] 
     host = mssqldb.mydomain.com 
     port = 1433 
     tds version = 8.0 

在調用的第一個參數配置文件mssql_connect()必須對應於freetds.conf文件中的[entry]。 所以你的電話變成

$myServer = "mssqldbAlias"; 
//connection to the database 
$dbhandle = mssql_connect($myServer, $myUser, $myPass) 
    or die(mssql_get_last_message());