我正在尝试通过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());
是否有某种方法可以获得更具体的错误消息?或者某种方式,我可以测试两台计算机是否正在进行通信,以便我可以尝试开始本地化问题?
答案 0 :(得分: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
答案 1 :(得分: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());