【问题标题】:FreeTDS mssql_connect not workingFreeTDS mssql_connect 不工作
【发布时间】:2015-06-09 07:11:51
【问题描述】:

我有一个 UBUNTU LAMP 设置。安装了所有必要的扩展

php5-odbc php5-mssql FREETDS unixODBC

在 CLI 中,我使用 isqltsql 测试了我与数据库的连接,并且两者都可以正常连接和工作。

这是我的freetds.conf

    ....
    # A typical Microsoft server
[TS]
        host = 10.32.6.52
        port = 1433
        tds version = 8.0
        client charset = UTF-8

这是我的 odbc.ini

[TS]
Description = "XXXXXXX"
Driver = /usr/local/lib/libtdsodbc.so
Server = xxx.xxx.xxx.xxx
Port = 1433
Database = XX_X_XX_XXXXX
Trace = No
TDS_Version = 8.0
[Default]
Driver=/usr/local/lib/libtdsodbc.so

这是我的 odbcinst.ini

[FreeTDS]
Description = FreeTDS
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
FileUsage = 1
CPTimeout =
CPResuse  =
client charset = utf-8
UsageCount = 1

这是我的 php

<?php

putenv("TDSVER=80");
putenv("FREETDSCONF=/etc/freetds/freetds.conf");
putenv("ODBCSYSINI=/etc/odbcinst.ini");
putenv("ODBCINI=/etc/odbc.ini");


$myServer = "XXX.XXX.XXX.XXX";
$username = "XXXXX";
$password = "XXXXX";
$database = "XXXXX";

//connection to the database
$dbhandle = mssql_connect($myServer, $username, $password)
  or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($database, $dbhandle)
  or die("Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query = "SELECT title from project where id = 8 ";

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

//display the results
while($row = mssql_fetch_array($result))
{
  echo "<li>" . $row["title"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>

就像我在 CLI 中所说的那样,我可以连接到 SQL 服务器,但从 php 中它不起作用。我已经尝试了所有我能找到的建议,但没有一个对我有用。

我的 Apache 错误日志也可供参考:

[Tue Jun 09 08:32:41.931108 2015] [:error] [pid 21435] [client 10.32.6.65:54994] PHP Warning:  mssql_connect(): Unable to connect to server: XXXXXXXX in /var/www/html/test/sqltest.php on line 15
[Tue Jun 09 08:32:41.931124 2015] [:error] [pid 21435] [client 10.32.6.65:54994] PHP Stack trace:
[Tue Jun 09 08:32:41.931134 2015] [:error] [pid 21435] [client 10.32.6.65:54994] PHP   1. {main}() /var/www/html/test/sqltest.php:0
[Tue Jun 09 08:32:41.931142 2015] [:error] [pid 21435] [client 10.32.6.65:54994] PHP   2. mssql_connect() /var/www/html/test/sqltest.php:15

【问题讨论】:

    标签: php sql-server ubuntu freetds unixodbc


    【解决方案1】:

    发现问题并解决。我通过取消注释 /etc/freetds/freetds.conf 中的日志文件位置来启用 Freetds.log

    这里是日志文件提取

    config.c:303:[XXXXXXXXX] not found.
    config.c:367:... $FREETDS not set.  Trying $HOME.
    config.c:293:Could not open '/var/www/.freetds.conf' ((.freetds.conf)).
    config.c:297:Found conf file '/usr/local/etc/freetds.conf' (default).
    config.c:483:Looking for section global.
    config.c:542:   Found section global.
    config.c:545:Got a match.
    config.c:567:   text size = '64512'
    config.c:542:   Found section egserver50.
    config.c:542:   Found section egserver70.
    config.c:556:   Reached EOF
    config.c:483:Looking for section XXXXXXXXXXXX.
    config.c:542:   Found section global.
    config.c:542:   Found section egserver50.
    config.c:542:   Found section egserver70.
    config.c:556:   Reached EOF
    config.c:303:[XXXXXXXXXXXX] not found.
    config.c:208:Failed in reading conf file.  Trying interface files.
    config.c:1042:Looking for server XXXXXXXXXXX....
    

    所以基本上它是在 freetds.conf 文件中寻找我的服务器名。但我没有具体说明。连接信息在配置文件中列为[TS] 我将我的php信息更改为寻找[TS] 并且突然之间它起作用了

    $myServer = "TS";
    $username = "XXXXX";
    $password = "XXXXX";
    $database = "XXXXX";
    

    【讨论】:

      【解决方案2】:

      我只是想发布这篇文章,以期对可能遇到相同问题的其他人有所帮助:

      最终对我有用:将我的PHP 连接字符串中的$host 值更改为我尝试连接到的服务器的引用名称,正如我在freetds.conf.odbc.ini 中指定的那样,而不是实际的IP 地址。

      感谢@Stroes 为我指明了正确的方向。经过一周多的阅读文章和故障排除后,这个问题几乎让我流泪。

      【讨论】:

        猜你喜欢
        • 2014-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-18
        • 1970-01-01
        • 2012-08-09
        • 1970-01-01
        相关资源
        最近更新 更多