PDO DBLIB访问SQL Server 2008和2012 [英] PDO DBLIB accessing SQL Server 2008 and 2012
问题描述
经过大量的阅读和搜索,我能够使PDO DBLIB在Centos 6.4 32位环境下工作(有很多关于64位而非32位的文档).令我惊讶的是,以下代码在 shell命令($ PHP test.php)
After a lot of reading and searching I was able to get PDO DBLIB working in Centos 6.4 32bits (there is a lot of documentation for 64 bits and not for 32 bits). To my surprise the following code work from the shell command ($ PHP test.php )
<?php
try {
$conn = new PDO('dblib:host='.$host.':1433;dbname='.$db, $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
但是当从Web浏览器访问文件test.php时,出现以下错误:
But when visiting the file test.php from a web browser I get the following error:
错误:SQLSTATE [HY000]无法连接:Adaptive Server是 不可用或不存在(严重性9)
ERROR: SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)
我还可以使用以下命令从外壳连接到SQL Server:
I am also able to connect to SQL Servers from the shell using the command:
tsql -H 192.168.1.120 -p 1433 -U sa
我想念什么?
推荐答案
在我的情况下,当从Linux框中的Apache Web服务器到Windows框中的sql服务器完成时,SELinux(我正在使用CentOS)正在阻止连接.要允许连接,您有两个选择:
In my case SELinux (I am using CentOS) was blocking the connection when done from Apache web server in the linux box to the sql server in the windows box. To allow the connection you have two options:
-
禁用SELinux
Disable SELinux
以root或sudo身份运行以下两个命令:
As root or sudo run the two commands:
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect_db 1
setsebool -P httpd_can_network_connect_db 1
要检查SELinux是否阻止了您的网络和数据库连接,请运行以下命令:
To check if SELinux is blocking your network and DB connections run this command:
getsebool -a | grep httpd_can_network_connect
getsebool -a | grep httpd_can_network_connect
如果结果是:
httpd_can_network_connect->上
httpd_can_network_connect --> on
httpd_can_network_connect_db->上
httpd_can_network_connect_db --> on
您很好,如果关闭,则运行此答案中的第一个命令.
You are good, if the are off then run the first commands in this answer.
这篇关于PDO DBLIB访问SQL Server 2008和2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!