简体   繁体   English

无法通过PHP连接到MS SQL Server

[英]Can't connect to MS SQL Server through PHP

I have a MSSql Db I am trying to connect to from PHP7.1 hosted on my RedHat box running apache. 我有一个MSSql Db,我试图从运行Apache的RedHat框中托管的PHP7.1连接到。 I have installed the sqlsrv && pdo_sqlsrv extensions and verified they are present via php.ini. 我已经安装了sqlsrv && pdo_sqlsrv扩展名,并验证了它们是否通过php.ini存在。

This is the small script I am using to test my connection: 这是我用来测试连接的小脚本:

<?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));
   }
?>

This is the reponse I am getting fro this script: 这是我从此脚本得到的答复:

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.
        )
)

I have verified with telnet that I can connect to the MSSql server form my Redhat box. 我已经使用telnet进行了验证,可以从Redhat框中连接到MSSql服务器。 Any clues as to why I would not be able to connect to the db? 关于为什么我将无法连接到数据库的任何线索?

Thanks to miken32 I was finally able to find the solution. 多亏了miken32,我终于能够找到解决方案。 He pointed out that the connection might not be allowed by SELinux and he was right. 他指出SELinux可能不允许连接,他是对的。 I just had to allow db connections and it worked like a charm. 我只需要允许数据库连接就可以了。

Allow db connections: 允许数据库连接:

setsebool -P httpd_can_network_connect_db 1

RedHat Linux comes with SELinux protection enabled by default. RedHat Linux默认情况下启用SELinux保护。 The fact that you can execute this script from CLI as root suggests that they are preventing your script from running properly from the web server. 您可以以root用户身份从CLI执行此脚本,这一事实表明它们阻止了脚本从Web服务器正常运行。 You'll need to tweak a couple of settings to allow your web server this kind of access. 您需要调整一些设置,以允许您的Web服务器进行这种访问。 Try this one first, which allows your web server to make its own network connections: 首先尝试一下,这将使您的Web服务器建立自己的网络连接:

setsebool -P httpd_can_network_connect 1

If that doesn't work you can try allowing database access specifically, though I'm honestly not sure whether this would apply to a remote SQL Server: 如果那行不通,您可以尝试专门允许数据库访问,尽管老实说我不确定这是否适用于远程SQL Server:

setsetbool -P httpd_can_network_connect_db 1

If neither of those work, you'll want to use the audit2why tool to read your audit log and find out why it's getting rejected. 如果上述两种方法都不起作用,则需要使用audit2why工具读取审核日志,并找出拒绝日志的原因。 That is likely a question for Server Fault though. 不过,这可能是服务器故障的问题。


You may read suggestions to just disable SELinux. 您可能会阅读有关禁用SELinux的建议。 Don't do it. 不要这样 It's there for a reason, and provides a lot of protection for your server. 它的存在是有原因的,并且为您的服务器提供了很多保护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM