2

I have followed this solution for what seems to be the exact same problem, however I am not having as much success as I had hoped. I set up the required config files for PDO_ODBC, unixODBC and FreeTDS packages:

  1. First by specifiying the host in /etc/freetds.conf :

    [mssql]
    host = DBHost
    port = 1433
    tds version = 7.3
    
  2. Then by specifying the FreeTDS driver location in /etc/odbcinst.ini :

    [freetds]
    Description = Ms SQL database access with Free TDS
    Driver64 = /usr/lib64/libtdsodbc.so.0
    Setup64 = /usr/lib64/libtdsS.so.2
    FileUsage = 1
    UsageCount = 1
    
  3. Then by specifying the DSN in /etc/odbc.ini :

    [mssql]
    Description = mssql server
    Driver = FreeTDS
    Database = CET_PhonesDB
    ServerName = mssql
    TDS_Version = 7.3
    

Finally here is my PHP:

 try {
        $pdo = new PDO('odbc:mssql', 'dbuser', 'dbpass');
 }
 catch(Exception $e){
        echo $e->getMessage();
 }

which returns: "SQLSTATE[08S01] SQLConnect: 20009 [unixODBC][FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist".

I've tried troubleshooting using $tsql -S mssql -U dbuser -P dbpass (which tests the FreeTDS) and $isql mssql dbuser dbpass (Which tests the unixODBC) both of which connect successfully. This leads me to believe that the problem is with PDO_ODBC or something else in the PHP configuration. Any help is greatly appreciated :)

walley
  • 135
  • 2
  • 11

2 Answers2

2

So I narrowed the problem down to the Apache server, by running my PHP script through the command line: $php /var/www/html/repo/index.php and the connection was made succesfully.

A coworker suggested that SELinux could be interfering with httpd, and sure enough running $setenforce 0 allowed my PHP to run properly when requesting them through a web browser.

Since disabling SELinux permenantly is a VERY BAD idea, I did some fiddling and digging and thanks to this manual I found that $sudo setsebool -P httpd_can_network_connect 1 allows scripts that the Apache runs to create network connections of their own. And now SELinux should let any PHP script executed by httpd create network connections.

walley
  • 135
  • 2
  • 11
0

I had the same problem and solved it by using a custom port: port = XXXXX in freetds.conf. This port should be configured in SQL Server Configuration Mgnt SQL Server Network Configuration -> TCP/IP (enabled) -> IP Addresses -> IPAll -> TCP Dynamic Ports https://msdn.microsoft.com/en-us/library/ms177440.aspx

James Z
  • 12,209
  • 10
  • 24
  • 44
Pupim1o
  • 5
  • 3