FreeTDS有效,但odbc无法连接到mssql服务器

时间:2017-02-09 12:03:14

标签: php sql-server pdo ubuntu-16.04

我在Ubuntu 16上安装了一个带有Apache2的VPS。现在我正在尝试使用UnixODBC驱动程序连接到我的业务数据库。我已经在这两天苦苦挣扎了,我似乎无法弄清楚它为什么不能连接到我的数据库。

我已经安装了FreeTDS的UnixODBC。我的php版本是7.0。在我的index.php中,我试图在__construct类中连接到我的数据库:

  

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

但无论我做什么,这样做都会让我回复这个错误:

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

但我无法弄清楚为什么它说无法连接到数据源。我的odbc.ini配置如下:

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

我的odbcinst.ini包含:

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

但它只是无法连接,IP也在MSSQL服务器上列入白名单。凭证是对的。它运行在Linux VPS上,在Ubuntu 16.04上

1 个答案:

答案 0 :(得分:1)

我建议您查看本文第2部分: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*||"`

此时将安装pdo_sqlsrv模块:https://github.com/Microsoft/msphpsql

希望这有帮助!