duanlin1933 2017-09-27 16:48
浏览 146
已采纳

无法通过PHP连接到MS SQL Server

I have a MSSql Db I am trying to connect to from PHP7.1 hosted on my RedHat box running apache. I have installed the sqlsrv && pdo_sqlsrv extensions and verified they are present via 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. Any clues as to why I would not be able to connect to the db?

  • 写回答

2条回答 默认 最新

  • douhuanbai6729 2017-09-27 18:29
    关注

    RedHat Linux comes with SELinux protection enabled by default. 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. You'll need to tweak a couple of settings to allow your web server this kind of access. Try this one first, which allows your web server to make its own network connections:

    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:

    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. That is likely a question for Server Fault though.


    You may read suggestions to just disable SELinux. Don't do it. It's there for a reason, and provides a lot of protection for your server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题