PHP/PDO Error: SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)

16,126

Solution 1

Try removing the port from $hostname and adding it to freetds.conf.

[192.168.5.57]
    host = 192.168.5.57
    port = 1433

Solution 2

We got this error when trying to connect to a database that we weren't authorized to. Once we requested to be added to the whitelist (not sure whether it was at the firewall or db level), then we could connect okay.

Check that there's no restrictions by IP address.

Share:
16,126
Jeremy H.
Author by

Jeremy H.

Updated on June 27, 2022

Comments

  • Jeremy H.
    Jeremy H. almost 2 years

    I'm running PHP 5.5 on CentOS 6 trying to connect to a MS SQL Server database via PDO. I have searched through about 20 different answers to the same error message and tried every one of them here on SO. When I attempt to connect from PDO I get the following error:

    SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)

    I am able to connect through FreeTDS on the command line with no issue at all:

    TDSVER=7.0 tsql -H 192.168.5.57 -p 1433 -U testuser

    I tried setting tds version = 7.0 in freetds.conf and still get the same error.

    I've tried connecting to different MS SQL Servers and get the same thing every time. I've tried this same code on a Rackspace Cloud server with no issues. So something between PDO/PHP and FreeTDS doesn't seem to be talking correctly, but I am at a loss of how to track this down.

    try {
      $hostname = '192.168.5.57:1433';
      $dbname = 'Test';
      $username = 'testuser';
      $password = 'thisismypassword';
      $db = new PDO("dblib:host=$hostname;dbname=$dbname","$username","$password");
    } catch (PDOException $e) {
      echo 'Failed to get DB handle: ' . $e->getMessage();
    }