简体   繁体   English

无法使用PDO连接到MS SQL Server

[英]Cannot connect to MS SQL Server using PDO

The last 2 days I've spent a lot of time trying to figure out how to connect to MS SQL server hosted on Azure using PHP from an Ubuntu 14.04 server. 最近两天,我花了很多时间试图弄清楚如何从Ubuntu 14.04服务器使用PHP连接到Azure托管的MS SQL服务器。

The code responsible to initiate the connection to the remote database is the following: 负责启动与远程数据库的连接的代码如下:

public function connect()
{
    try
    {
        $this->databaseConnection = new \PDO(
            'dblib:host='.$this->hostname.':'.$this->port.';dbname='.$this->databaseName.';',
            $this->username,
            $this->password
        );
        $this->databaseConnection->setAttribute(
            \PDO::ATTR_ERRMODE,
            \PDO::ERRMODE_EXCEPTION
        );
    }
    catch (\PDOException $e)
    {
        throw new \Exception('Failed to connect to the database: '.$e->getMessage());
    }
}

And every time I try to connect I receive: 每当我尝试连接时,我都会收到:

Failed to connect to the database: SQLSTATE[01002] Adaptive Server connection failed (severity 9) 无法连接到数据库:SQLSTATE [01002] Adaptive Server连接失败(严重性9)

The problem is that this is a very general error and there's no way for me to understand what's causing the issue. 问题在于这是一个非常普遍的错误,我无法理解导致问题的原因。

This is my /etc/freetds.conf configuration: 这是我的/etc/freetds.conf配置:

[placeholder.database.windows.net]
    host = placeholder.database.windows.net
    port = 1433
    tds version = 7.2
    client charset = UTF-8

And this is the tsql -C output: 这是tsql -C输出:

Compile-time settings (established with the "configure" script)
                        Version: freetds v0.91
         freetds.conf directory: /etc/freetds
 MS db-lib source compatibility: no
    Sybase binary compatibility: yes
                  Thread safety: yes
                  iconv library: yes
                    TDS version: 4.2
                          iODBC: no
                       unixodbc: yes
          SSPI "trusted" logins: no
                       Kerberos: yes

In addition to this, I tried making some test using mssql_* functions and even if I was able to successfully connect to the database most of the queries didn't work. 除此之外,我尝试使用mssql_*函数进行一些测试,即使我能够成功连接到数据库,大多数查询也无法正常工作。

For a simple solution for one sqlsrv server connection in your application, you can modify the tds version to 8.0 under the [global] section of the freetds.conf , eg 对于您的应用程序中一个sqlsrv服务器连接的简单解决方案,可以在freetds.conf[global]部分下将tds版本修改为8.0 ,例如

[global]
    tds version = 8.0

Then test in PHP script: 然后在PHP脚本中进行测试:

try {
$pdo = new PDO("dblib:host=<azure_sql_name>.database.windows.net;dbname=<dbname>", "<username>", "<password>");
} catch (PDOException $e) {
echo "Error: " . $e->getMessage() . "\n";
} 

It works fine on my side after the modification. 修改后,它对我而言效果很好。 Any further concern, please feel free to let me know. 如有任何其他疑问,请随时告诉我。

The problem was caused by 2 different issues. 该问题是由2个不同的问题引起的。

The first one was related to a wrong FreeTDS configuration. 第一个与错误的FreeTDS配置有关。 The freetds.con global section needs to be like this: freetds.con全局部分需要像这样:

[global]
    tds version = 8.0

The second one was caused by the fact that the username must be specified in the following way when connecting to a MSSQL server hosted on Azure: 第二个原因是由于在连接到Azure托管的MSSQL服务器时必须以以下方式指定用户名:

<username>@<freeTDSServerName>

Where the specific server configuration in freetds.conf looks something like this: freetds.conf中的特定服务器配置如下所示:

[<freeTDSServerName>]
    database = <databaseName>
    host = <serverName>.database.windows.net
    port = 1433
    tds version = 8.0

So the PHP DB connection will look like something like this: 因此,PHP DB连接将如下所示:

$this->databaseConnection = new \PDO(
    'dblib:host='.$this->hostname.':'.$this->port.';dbname='.$this->databaseName.';',
    $this->username.'@<freeTDSName>',
    $this->password
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM