繁体   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