简体   繁体   English

连接到 MS SQL 数据库时出现 PDO 错误“Adaptive Server 不可用”

[英]PDO Error "Adaptive Server is unavailable" when connecting to MS SQL Database

I am trying to connect to a SQL server database that is running on a windows server.我正在尝试连接到在 Windows 服务器上运行的 SQL 服务器数据库。 I am running this PHP code on a linux server (centos 7).我在 linux 服务器(centos 7)上运行这个 PHP 代码。

I am using the following pdo connection string to connect to the database.我正在使用以下 pdo 连接字符串连接到数据库。

$db = new PDO ("dblib:192.168.2.1;dbname=TestDB","username",'pass');

When i run the code i get the following exception.当我运行代码时,出现以下异常。 'PDOException' with message 'SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)'带有消息“SQLSTATE[HY000] 无法连接:Adaptive Server 不可用或不存在(严重性 9)”的“PDOException”

I have tried to test the connection using tsql and i am able to connect to the database without any error.我尝试使用 tsql 测试连接,并且我能够连接到数据库而没有任何错误。 The following code gave me a list of all the tables in TestDB.以下代码为我提供了 TestDB 中所有表的列表。 It wouldnt work if i didng type use TestDB first.如果我没有先键入使用 TestDB,它就行不通。

tsql -S 192.168.2.1 -U username -P 'pass' -L TestDB

use TestDB 
GO 
select * FROM sys.Tables 
GO

My freetds.conf file contains the following我的 freetds.conf 文件包含以下内容

[Server1]
    host = 192.168.2.1
    port = 1433
    tds version = 8.0

I cannot figure out how i am able to connect using tsql, but cannot do the same when connecting with php.我无法弄清楚如何使用 tsql 进行连接,但在使用 php 连接时无法进行相同的操作。

The dblib driver is definitely installed. dblib 驱动肯定已经安装好了。

print_r(PDO::getAvailableDrivers()); 

Array ( [0] => dblib [1] => mysql [2] => sqlite )

Answer回答

Found the cause of the problem.找到了问题的原因。 Turned out to be SELinux.原来是SELinux。 The following commands fixed the issue.以下命令修复了该问题。

setsebool -P httpd_can_network_connect 1

setsebool -P httpd_can_network_connect_db 1

You have the datasource name, you should make use of it:您有数据源名称,您应该使用它:

$db = new PDO ("dblib:host=Server1;dbname=TestDB","username",'pass');

You are running linux right?你在运行 linux 对吗? I recommend giving odbc a shot.我建议试一试 odbc。

Three things to check for you.为您检查三件事。

First try your connection using your defined port.首先使用您定义的端口尝试连接。

Instead of:代替:

$db = new PDO ("dblib:192.168.2.1;dbname=TestDB","username",'pass');

try using this:尝试使用这个:

$db = new PDO("dblib:host=192.168.2.1:1433;dbname=TestDB","username",'pass');

Second , you should be sure if your SQL Server is configured to hear on port 1433. You can check this using the SQL Server Configuration Manager.其次,您应该确定您的 SQL Server 是否配置为在端口 1433 上侦听。您可以使用 SQL Server 配置管理器进行检查。

The third (if you run it on windows) thing you can check is one thing I find in the PHP docs .您可以检查的第三件事(如果您在 Windows 上运行它)是我在PHP 文档中找到的一件事。 Inside a comment, another one mentioned the same error.在评论中,另一个人提到了同样的错误。 Here is the answer which seems to work:这是似乎有效的答案:

For PDO MSSQL connection issues, ensure that you have the updated version of ntwdblib.dll (currently 8.00.194 as of this post).对于 PDO MSSQL 连接问题,请确保您拥有 ntwdblib.dll 的更新版本(截至本文,当前为 8.00.194)。 Overwrite the existing (old) file or place it in the Windows system32 folder.覆盖现有(旧)文件或将其放在 Windows system32 文件夹中。 The version that ships with PHP 5.2.X does not work. PHP 5.2.X 附带的版本不起作用。 This is a well known issue apparently, but I had a really hard time finding information on this issue until I was able to lock in a file name.这显然是一个众所周知的问题,但在我能够锁定文件名之前,我很难找到有关此问题的信息。 I hope that this helps someone.我希望这对某人有所帮助。

Another possible issue could be SELinux if it's enabled.如果启用了 SELinux,另一个可能的问题可能是 SELinux。 I've gotten some errors which are something familiar with this on my Ruby on Rails installation.我在 Ruby on Rails 安装中遇到了一些与此相似的错误。 You can give it a try by disabling SELinux and try it again.您可以通过禁用 SELinux 来尝试一下,然后再试一次。

When connecting using PDO, if you leave out the host= it uses the Unix domain sockets which could be causing your problem.使用 PDO 连接时,如果您省略host=它使用 Unix 域套接字,这可能会导致您的问题。 Putting in the host= will connect to the database via TCP/IP.输入host=将通过 TCP/IP 连接到数据库。

So try change your line of code to:因此,尝试将您的代码行更改为:

$db = new PDO ("dblib:host=192.168.2.1;dbname=TestDB","username",'pass');

Or with the port aswell:或者还有端口:

$db = new PDO ("dblib:host=192.168.2.1:1433;dbname=TestDB","username",'pass');

For me, the problem was using the IP address.对我来说,问题在于使用 IP 地址。 I changed it to "MyServerName/SQL2017DEV" and it connected我将其更改为“MyServerName/SQL2017DEV”并连接

I have seen this issue same .I solved it very simply. 我已经看到了同样的问题。我非常简单地解决了这个问题。 Open your MSSQLsrver 1433 port and then you can connect db success. 打开您的MSSQLsrver 1433端口,然后您可以连接数据库成功。

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

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