简体   繁体   English

php,linux服务器上的sqlsrv_connect问题

[英]Problem with sqlsrv_connect on php, linux server

I can connect to sql server remotely. 我可以远程连接到sql server。 The connection string is: 连接字符串是:

Provider=SQLOLEDB.1; Persist Security Info=True; Data Source=3.120.12.12;
Initial Catalog=StoreDB; User ID=mohan; PASSWORD=123456

I used of sqlsrv_connect to connect: 我使用sqlsrv_connect连接:

$serverName = "3.120.12.12";
$connectionOptions = array
(
   "Database" => "StoreDB",
   "Uid" => "mohan",
   "PWD" => "123456"
);
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn) echo "Connected!"; else die( print_r( sqlsrv_errors(), true));

But this doesn't connect to the database and show's the following error: 但是这没有连接到数据库并显示以下错误:

Array
(
[0] => Array
    (
        [0] => HYT00
        [SQLSTATE] => HYT00
        [1] => 0
        [code] => 0
        [2] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
        [message] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
    )

[1] => Array
    (
        [0] => 08001
        [SQLSTATE] => 08001
        [1] => 258
        [code] => 258
        [2] => [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x102
        [message] => [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x102
    )

[2] => Array
    (
        [0] => 08001
        [SQLSTATE] => 08001
        [1] => 258
        [code] => 258
        [2] => [Microsoft][ODBC Driver 17 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] => [Microsoft][ODBC Driver 17 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.
    ))

After this error, I change $serverName to "3.120.12.12:1433" and it then shows this error: 出现此错误后,我将$ serverName更改为“3.120.12.12:1433”,然后显示此错误:

Array ( [0] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired [message] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired ) [1] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 87 [code] => 87 [2] => [Microsoft][ODBC Driver 17 for SQL Server]MAX_PROVS: Connection string is not valid [87]. [message] => [Microsoft][ODBC Driver 17 for SQL Server]MAX_PROVS: Connection string is not valid [87]. ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 87 [code] => 87 [2] => [Microsoft][ODBC Driver 17 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] => [Microsoft][ODBC Driver 17 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. ) )

However, I can connect to the database by "Microsoft sql server management studio" without problem. 但是,我可以通过“Microsoft sql server management studio”连接到数据库而没有问题。

How can connect to database using sqlsrv_connect ? 如何使用sqlsrv_connect连接数据库?

Its either the firewall or any additional security features running on your system like selinux (if you are on Red Hat). 它是防火墙或系统上运行的任何其他安全功能,如selinux(如果您使用的是Red Hat)。

If you do have the selinux kernel check for the booleans and see if the httpd_can_network_connect_db is off with getsebool -a . 如果你确实有selinux内核检查布尔值,看看httpd_can_network_connect_db是否已关闭getsebool -a If it is of you need to turn it on. 如果是你需要打开它。 You can do that using this command: setsebool -P httpd_can_network_connect_db 1 您可以使用以下命令执行此操作: setsebool -P httpd_can_network_connect_db 1

And don't forget to restart your server (apache or nginx) 并且不要忘记重新启动服务器(apache或nginx)

more info about this command here: 这里有关于此命令的更多信息:

https://linux.die.net/man/8/setsebool https://linux.die.net/man/8/setsebool

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

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