Thimo Franken
Thimo Franken

Reputation: 330

FreeTDS works but odbc can't connect to mssql server

I have a VPS with Apache2 installed on Ubuntu 16. Now i am trying to connect to my business database using UnixODBC driver. I've been struggling with this for two days now and i cant seem to figure out why it won't connect to my database.

I have got UnixODBC installed with FreeTDS. My php version is 7.0. In my index.php i am trying to connect to my database like this in the __construct class:

$this->DBConn = new PDO("odbc:Driver=FreeTDS;host=SPOOF;dbname=SPOOF", 'SPOOF', 'SPOOF');

But doing this will give me back this error no matter what i do:

Fatal error: Uncaught PDOException: SQLSTATE[08001] SQLDriverConnect: 0 [unixODBC][FreeTDS][SQL Server]Unable to connect to data source in /var/www/ttv-boomgaardshoek.nl/public_html/Sharelogistics/index.php:5 Stack trace: #0 /var/www/ttv-boomgaardshoek.nl/public_html/Sharelogistics/index.php(5): PDO->__construct('odbc:Driver=Fre...', 'SPOOF', 'SPOOF') #1 /var/www/ttv-boomgaardshoek.nl/public_html/Sharelogistics/index.php(8): CakeClass->__construct() #2 {main} thrown in /var/www/ttv-boomgaardshoek.nl/public_html/Sharelogistics/index.php on line 5

But i can't figure out why it says it cant connect to data source. I have got my odbc.ini configured like this:

[Share]
Driver = FreeTDS
TDS_Version=8.0
ServerName = Share
SERVER = SPOOF
Port = 1433
Database=SPOOF

My odbcinst.ini contains this:

[FreeTDS]
Description = FreeTDS
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so

But it just won't connect, also the IP is whitelisted on the MSSQL server. The credentials are right. It is runing on a Linux VPS, on Ubuntu 16.04

Upvotes: 2

Views: 1101

Answers (1)

meet-bhagdev
meet-bhagdev

Reputation: 2708

I would recommend checking out section 2 of this article: https://www.microsoft.com/en-us/sql-server/developer-get-started/php-ubuntu

sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql
sudo apt-get install unixodbc-dev gcc g++ build-essential
sudo pecl install sqlsrv pdo_sqlsrv
sudo echo "extension= pdo_sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
sudo echo "extension= sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

At this point would would have installed the pdo_sqlsrv module: https://github.com/Microsoft/msphpsql

Hope this helps!

Upvotes: 1

Related Questions