0

I'm using OpenVPN to connect in my work database, if I connect in VPN and ping in the IP of the database, it looks like everything is right. But when I try to connect via PHP sqlsrv_connect, doesn't work (return false).

Ping in database IP

$serverName = "server-ip";
$connectionOptions = [
    "Database" => $databaseName,
    "Uid" => $uid,
    "PWD" => $pwd
];

$conn = sqlsrv_connect($serverName, $connectionOptions);
    if($conn) {
    $getResults= sqlsrv_query($conn, "SELECT @@Version as SQL_VERSION");
    var_dump(sqlsrv_fetch_object($getResults));
}

PHP Code The same code works in Windows

  • 2
    Please add code and data as text [using code formatting](https://stackoverflow.com/editing-help#code). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – Jason K Mar 11 '21 at 16:51
  • More detail on your env is needed. What testing other than ping? Just because you have network connectivity does not mean your allowed to connect. Have you talked to the administrator of the DB? Have you checked the web server error log? You done have any error checking, – Jason K Mar 11 '21 at 16:55
  • The server allows remote conections, when I ping without being on the VPN, it doesn't work, so when I connected to the VPN and the ping responded correctly, so I thought everything was fine with the VPN. I spoke with the DB admin, and he never used WSL2, so he doesn't know how to help me in this case, he just raised a few points "maybe it is filtering some specific package, with a firewall or something like" – Pedro Paulo Mar 11 '21 at 17:16
  • Have you checked the web server error log? – Jason K Mar 11 '21 at 17:19
  • I didn't understand this part, how do I check it? – Pedro Paulo Mar 11 '21 at 17:21
  • Whats your OS and web server type? Are you using wamp? – Jason K Mar 11 '21 at 17:30
  • Add this to the top of you script to display errors. ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); – Jason K Mar 11 '21 at 17:35
  • I'm using Win 10, with WSL2, not using wamp or something like. Just PHP and Apache. – Pedro Paulo Mar 11 '21 at 17:36
  • I add `die( print_r( sqlsrv_errors(), true));` in else condition. Return: ``` [0] => 08001 [SQLSTATE] => 08001 [1] => 10054 [code] => 10054 [2] => [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2746 [message] => [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2746 ``` – Pedro Paulo Mar 11 '21 at 17:38
  • And: [0] => 08001 [SQLSTATE] => 08001 [1] => 10054 [code] => 10054 [2] => [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection [message] => [Microsoft][ODBC Driver 17 for SQL Server]Client unable to establish connection – Pedro Paulo Mar 11 '21 at 17:40
  • I've havent used wsl2. In a linux os I would look under /var/log/ . My guess is has to do with routing. the ws2l env does not know how to get to the vpn interface. It's sounds like routing problem. I would first shutdown the vpn. Then check to see if the ws2l env can ping the host ip. If you cant get to that you need routing for that. Then see if you can ping the vpn ip. My guess is you need to add some routing. – Jason K Mar 11 '21 at 17:49
  • The VPN assigned IP 10.0.0.10 to me. I do a ping in that ip on windows and it worked, when I did it on WSL2 doesn't. – Pedro Paulo Mar 11 '21 at 18:01
  • Since you're not using an instance name can we assume that the target SQL Server is meant to be listening on port 1433? If you can't `telnet 172.31.12.132 1433` then the database drivers aren't going to connect either. – AlwaysLearning Mar 12 '21 at 06:25

0 Answers0