简体   繁体   中英

Problem with sqlsrv_connect on php, linux server

I can connect to sql server remotely. 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:

$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:

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.

How can connect to database using sqlsrv_connect ?

Its either the firewall or any additional security features running on your system like selinux (if you are on 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 . 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

And don't forget to restart your server (apache or nginx)

more info about this command here:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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