Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLSTATE[01002] Adaptive Server connection failed (severity 9)

I have the following script to connect to my microsoft azure server.

<?php

try {
    $hostname = "secrets.database.windows.net";
    $dbname = "secrets";
    $username = "secrets";
    $pw = "secrets";
    $dbh = new PDO ("dblib:host=$hostname;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}

echo "Passed!";

The script above passes on my old server, but gives me the following error message when executed from new server.

SQLSTATE[01002] Adaptive Server connection failed (severity 9)

My new server PHP setup is as follows:

sudo apt-get install -y php5.6-fpm php5.6-ldap php5.6-curl php5.6-cli   php5.6-mcrypt php5.6-intl php5.6-json php5.6-pdo-dblib php5.6-mysqlnd php5.6-memcached php5.6-mbstring php5.6-imap php5.6-xml php5.6-sybase

My checks so far:

1) Both have same public facing IP address.

2) Both have identical PHP PDO/ODBC setup.

$ php -i | grep PDO
DO
PDO support => enabled
PDO drivers => dblib, mysql, odbc
PDO Driver for FreeTDS/Sybase DB-lib => enabled
PDO Driver for MySQL => enabled
PDO_ODBC
PDO Driver for ODBC (unixODBC) => enabled

3) I am able to ping my server using telnet from both servers using:

telnet secrets.database.windows.net 1433

Any suggestions would be appreciated.

like image 954
hawx Avatar asked May 13 '16 09:05

hawx


3 Answers

After some further googling I came across this answer.

Connect PHP to MSSQL via PDO ODBC

Turned out I just needed to update my /etc/freetds/freetds.conf

My changes:

Uncommented TDS protocol version and updated.

tds version = 8.0

Added mssql below examples.

[mssql]
host =
Port = 1433
tds version = 8.0
like image 153
hawx Avatar answered Sep 30 '22 06:09

hawx


I got the same issue here, but it is fixed by adding version of FreeTDS 8.0 to the connection directly in PHP code:

<?php

try {
    $hostname = "secrets.database.windows.net";
    $dbname = "secrets";
    $username = "secrets";
    $pw = "secrets";
    $dbh = new PDO ("dblib:version=8.0;charset=UTF-8;host={$hostname};dbname={$dbname}", $username, $pwd);
} catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}

echo "Passed!";
like image 32
Kannika Avatar answered Sep 30 '22 05:09

Kannika


In my case I had a typo in $dbname, correcting it solved the issue.

like image 35
redacted Avatar answered Sep 30 '22 06:09

redacted