2013-01-24 2 views
2

PHP를 통해 아파치 리눅스 서버에서 Windows 컴퓨터에 SQL Server 2008에 대한 연결을 열려고합니다. 나는 방화벽에서 해당 포트를 열었습니다,하지만 난아파치가 PHP와 SQL Server에 연결하지 않습니다

이 경고 vaguest 오류가 점점 오전 : mssql_connect은() [function.mssql이-연결] : 서버에 연결할 수 없습니다 : xxx.xxx.xx.를

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

자세한 오류 메시지를 얻을 수있는 몇 가지 방법이 있나요 : XXX,

와 XXXX? 아니면 두 컴퓨터가 전혀 통신하지 않는지 확인하기 위해 테스트 할 수있는 방법으로 문제를 현지화하기 시작할 수 있습니까?

+0

이됩니까? 해당 서버에서 주어진 포트를 확인하십시오. 해당 포트에 텔넷하여 연결을 수락하는지 확인하십시오. – 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

우분투 컴퓨터에서 Windows SQL Server로 PHP를 MSSQL에 연결하는 데 사용하는 코드는 다음과 같습니다.이 코드가 도움이되는지는 모르겠지만이 코드는 정상적으로 작동하므로 지금 작동합니다. 우리 환경 ...

보시다시피 mssql_ * 함수 대신 PDO를 사용합니다. 우분투에서 dblib 드라이버를 얻으려면 php5-sybase 패키지를 설치해야했습니다.

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

감사합니다. 나는 우분투를 사용할 수 있었으면 좋겠다. 나는 PHP를 위해 ms sql PDO를 설치하고 아무 쓸모가 없도록 여러 번 시도했다. 나는 당신이 평범하지 않은 사람이라는 것을 알고 있지만 어쩌면 당신은 현재 사용 가능한 라이브러리가 어디에 있는지 알 수 있습니다. – 1252748

+0

@thomas - 어떤 리눅스 배포판을 사용하고 있습니까? –

+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()); 
관련 문제