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

时间:2021-06-03 19:26:50

I am trying to connect to a SQL server database that is running on a windows server. I am running this PHP code on a linux server (centos 7).

我正在尝试连接到在Windows服务器上运行的SQL Server数据库。我在linux服务器上运行这个PHP代码(centos 7)。

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]的'PDOException'无法连接:Adaptive Server不可用或不存在(严重级9)'

I have tried to test the connection using tsql and i am able to connect to the database without any error. The following code gave me a list of all the tables in TestDB. It wouldnt work if i didng type use TestDB first.

我试图使用tsql测试连接,我能够连接到数据库,没有任何错误。以下代码为我提供了TestDB中所有表的列表。如果我首先使用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. The following commands fixed the issue.

找到问题的原因。原来是SELinux。以下命令解决了该问题。

setsebool -P httpd_can_network_connect 1

setsebool -P httpd_can_network_connect_db 1

4 个解决方案

#1


2  

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? I recommend giving odbc a shot.

你正在运行linux吗?我建议给odbc一个机会。

#2


0  

When connecting using PDO, if you leave out the host= it uses the Unix domain sockets which could be causing your problem. Putting in the host= will connect to the database via TCP/IP.

使用PDO进行连接时,如果省略主机=它使用可能导致问题的Unix域套接字。放入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');

#3


0  

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');

$ 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');

$ 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. Inside a comment, another one mentioned the same error. Here is the answer which seems to work:

第三个(如果你在Windows上运行它)你可以检查的东西是我在PHP文档中找到的一件事。在评论中,另一个提到了同样的错误。这是一个似乎有效的答案:

For PDO MSSQL connection issues, ensure that you have the updated version of ntwdblib.dll (currently 8.00.194 as of this post). Overwrite the existing (old) file or place it in the Windows system32 folder. The version that ships with PHP 5.2.X does not work. 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.

对于PDO MSSQL连接问题,请确保您具有ntwdblib.dll的更新版本(截至此帖子目前为8.00.194)。覆盖现有(旧)文件或将其放在Windows system32文件夹中。 PHP 5.2.X附带的版本不起作用。这显然是一个众所周知的问题,但在我能够锁定文件名之前,我很难找到有关此问题的信息。我希望这有助于某人。

Another possible issue could be SELinux if it's enabled. I've gotten some errors which are something familiar with this on my Ruby on Rails installation. You can give it a try by disabling SELinux and try it again.

另一个可能的问题可能是SELinux,如果它已启用。我在Ruby on Rails安装上遇到了一些熟悉的错误。您可以通过禁用SELinux尝试再试一次。

#4


-1  

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端口,然后您可以连接数据库成功。

#1


2  

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? I recommend giving odbc a shot.

你正在运行linux吗?我建议给odbc一个机会。

#2


0  

When connecting using PDO, if you leave out the host= it uses the Unix domain sockets which could be causing your problem. Putting in the host= will connect to the database via TCP/IP.

使用PDO进行连接时,如果省略主机=它使用可能导致问题的Unix域套接字。放入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');

#3


0  

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');

$ 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');

$ 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. Inside a comment, another one mentioned the same error. Here is the answer which seems to work:

第三个(如果你在Windows上运行它)你可以检查的东西是我在PHP文档中找到的一件事。在评论中,另一个提到了同样的错误。这是一个似乎有效的答案:

For PDO MSSQL connection issues, ensure that you have the updated version of ntwdblib.dll (currently 8.00.194 as of this post). Overwrite the existing (old) file or place it in the Windows system32 folder. The version that ships with PHP 5.2.X does not work. 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.

对于PDO MSSQL连接问题,请确保您具有ntwdblib.dll的更新版本(截至此帖子目前为8.00.194)。覆盖现有(旧)文件或将其放在Windows system32文件夹中。 PHP 5.2.X附带的版本不起作用。这显然是一个众所周知的问题,但在我能够锁定文件名之前,我很难找到有关此问题的信息。我希望这有助于某人。

Another possible issue could be SELinux if it's enabled. I've gotten some errors which are something familiar with this on my Ruby on Rails installation. You can give it a try by disabling SELinux and try it again.

另一个可能的问题可能是SELinux,如果它已启用。我在Ruby on Rails安装上遇到了一些熟悉的错误。您可以通过禁用SELinux尝试再试一次。

#4


-1  

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端口,然后您可以连接数据库成功。