PDO error "Adaptive server unavailable" when connecting to MS SQL database

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).

I am using the following pdo connection string to connect to the database.

$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 not available or does not exist (severity 9)"

I tried to test the connection using tsql and I can connect to the database without any error. The following code gave me a list of all tables in TestDB. It wouldn't work if I was using the TestDB type.

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

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

      

I cannot figure out how I can connect using tsql but cannot do the same when connecting from php.

The dblib driver is definitely installed.

print_r(PDO::getAvailableDrivers()); 

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

      

Answer

Found the cause of the problem. It turned out to be SELinux. The following issues fixed the issue.

setsebool -P httpd_can_network_connect 1

setsebool -P httpd_can_network_connect_db 1

      

+3


source to share


4 answers


You have a data source name, you must use it:

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

      



Are you using linux correctly? I recommend giving odbc a shot.

+2


source


Three things to check for you.

First, try your connection using a particular port.

Instead:

$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 need to be sure that your SQL Server is configured to listen on port 1433. This can be verified using the SQL Server Configuration Manager.

The third (if you run it on windows) thing you can check is one that I found in the PHP docs . Inside a comment, another one mentioned the same error. Here's an answer that seems to work:

For PDO MSSQL connection problems, make sure you have an updated version of ntwdblib.dll (currently 8.00.194 from 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 seems to be a well-known issue, but I find it very difficult to find information on this issue until I was able to block the filename. I hope this helps someone.

Another possible problem could be SELinux if enabled. I got some errors that are familiar with this while installing Ruby on Rails. You can try by disabling SELinux and try again.

0


source


When connecting using PDO, if you are not using it host=

, it uses Unix domain sockets which can cause problems. The input host=

will connect to the database over TCP / IP.

So, try changing your line of code to:

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

      

Or with a port:

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

      

0


source


I saw this problem the same. I solved it very simply. Open MSSQLsverver port 1433 and then you can connect to db success.

-1


source







All Articles