簡體   English   中英

Apache無法使用PHP連接到SQL Server

[英]Apache will not connect to SQL Server with PHP

我試圖通過apache linux服務器從Windows計算機上打開到SQL Server 2008的連接。 我已經在防火牆上打開了適當的端口,但出現了最模糊的錯誤

警告:mssql_connect()[function.mssql-connect]:無法連接到服務器: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()); 

有什么方法可以獲取更具體的錯誤消息? 還是可以通過某種方式測試以查看兩台計算機是否完全通信,以便嘗試定位問題?

這是我用來將PHP從Ubuntu機器連接到Windows SQL Server並將PHP連接到MSSQL的代碼,我不知道它是否對您有幫助,但是此代碼現在已經成功啟動並運行,因此我知道它可以在我們的環境中工作。 。

如您所見,我使用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

我今天遇到了同樣的問題。 這對我有用:

代替這個

$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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM