我有一个MSSql Db我试图从运行apache的RedHat盒上托管的PHP7.1连接。我安装了sqlsrv&& pdo_sqlsrv扩展并验证它们是通过php.ini存在的。
这是我用来测试连接的小脚本:
<?php
$serverName = "MY_IP_ADDRESS"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array("Database"=>"MY_DB", "UID"=>"MY_UID", "PWD"=>"MY_PWD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
这是我对这个剧本的反应:
Connection could not be established.
Array
(
[0] => Array
(
[0] => HYT00
[SQLSTATE] => HYT00
[1] => 0
[code] => 0
[2] => [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired
[message] => [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired
)
[1] => Array
(
[0] => 08001
[SQLSTATE] => 08001
[1] => 10013
[code] => 10013
[2] => [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]TCP Provider: Error code 0x271D
[message] => [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]TCP Provider: Error code 0x271D
)
[2] => Array
(
[0] => 08001
[SQLSTATE] => 08001
[1] => 10013
[code] => 10013
[2] => [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
[message] => [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
)
)
我已经通过telnet验证我可以从我的Redhat框连接到MSSql服务器。关于为什么我无法连接数据库的任何线索?
答案 0 :(得分:0)
感谢miken32,我终于找到了解决方案。他指出SELinux可能不允许连接,他是对的。我只需要允许数据库连接,它就像一个魅力。
允许数据库连接:
setsebool -P httpd_can_network_connect_db 1
答案 1 :(得分:0)
RedHat Linux默认启用SELinux保护。您可以从CLI以root身份执行此脚本,这表明它们阻止您的脚本从Web服务器正常运行。您需要调整一些设置以允许您的Web服务器进行此类访问。首先尝试这个,这允许您的Web服务器建立自己的网络连接:
setsebool -P httpd_can_network_connect 1
如果这不起作用,您可以尝试专门允许数据库访问,但我真的不确定这是否适用于远程SQL Server:
setsetbool -P httpd_can_network_connect_db 1
如果这些都不起作用,您将需要使用audit2why
工具来阅读审核日志并找出被拒绝的原因。这可能是服务器故障的问题。
您可以阅读仅禁用SELinux的建议。不要这样做。这是有原因的,并为您的服务器提供了很多保护。