PDO错误"Adaptive Server不可用".连接到MS SQL数据库时 [英] PDO Error "Adaptive Server is unavailable" when connecting to MS SQL Database
问题描述
我正在尝试连接到Windows服务器上运行的SQL Server数据库.我正在Linux服务器(centos 7)上运行此PHP代码.
I am trying to connect to a SQL server database that is running on a windows server. I am running this PHP code on a linux server (centos 7).
我正在使用以下pdo连接字符串连接到数据库.
I am using the following pdo connection string to connect to the database.
$db = new PDO ("dblib:192.168.2.1;dbname=TestDB","username",'pass');
当我运行代码时,出现以下异常.消息为"SQLSTATE [HY000]"的"PDOException"无法连接:Adaptive Server不可用或不存在(严重性9)"
When i run the code i get the following exception. 'PDOException' with message 'SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)'
我尝试使用tsql测试连接,并且能够无任何错误地连接到数据库.以下代码为我提供了TestDB中所有表的列表.如果我没有类型先使用TestDB,那将行不通.
I have tried to test the connection using tsql and i am able to connect to the database without any error. The following code gave me a list of all the tables in TestDB. It wouldnt work if i didng type use TestDB first.
tsql -S 192.168.2.1 -U username -P 'pass' -L TestDB
use TestDB
GO
select * FROM sys.Tables
GO
我的freetds.conf文件包含以下内容
My freetds.conf file contains the following
[Server1]
host = 192.168.2.1
port = 1433
tds version = 8.0
我无法弄清楚如何使用tsql进行连接,但是在使用php连接时却无法做到这一点.
I cannot figure out how i am able to connect using tsql, but cannot do the same when connecting with php.
肯定安装了dblib驱动程序.
The dblib driver is definitely installed.
print_r(PDO::getAvailableDrivers());
Array ( [0] => dblib [1] => mysql [2] => sqlite )
答案
找到了问题的原因.原来是SELinux.以下命令解决了该问题.
Found the cause of the problem. Turned out to be SELinux. The following commands fixed the issue.
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect_db 1
推荐答案
您拥有数据源名称,您应该使用它:
You have the datasource name, you should make use of it:
$db = new PDO ("dblib:host=Server1;dbname=TestDB","username",'pass');
您正在运行linux吗?我建议给odbc一下.
You are running linux right? I recommend giving odbc a shot.
这篇关于PDO错误"Adaptive Server不可用".连接到MS SQL数据库时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!