0
votes

I've found myself in a Job where I have to work with a windows server (2012) - I've never had problems with establishing DB connection, but now I dont seem to find any right solution.

I'll show you my connecting php code:


    error_reporting(-1);
    ini_set('display_errors', 1);
    $DB = array ('dbname'=>"test" , 'user'=> '***' , 'passwort'=> '***', 'host'=>'somelocalnetwork ip 192.**');
    $connect = "mysql:dbname=".$DB['dbname']."; host=".$DB['host'];
    try
    {
        $dbh = new PDO($connect,$DB['user'], $DB['passwort']);
        echo "Connection established.
"; $dbh = null; } catch (PDOException $e) { echo $e -> getMessage(); }

This is the result, that i get in my browser:

SQLSTATE[HY000] [2002] Es konnte keine Verbindung hergestellt werden, da der Zielcomputer die Verbindung verweigerte.

translated into english :

No connection could be made because the target machine actively refused it .

NOTE: I downloaded mssql drivers and sqlsrv drivers and extracted them to the /ext/ direcoty , included them in the php ini file. But when checking the php_info() i dont see any mssql nor sqlsrv parts. I don't know if thats relevant

The Windows Server is set as WebServer and as normal Microsoft SQL Server

2
I see mysql in your $connect, is this intended ? - t-clausen.dk
I use it because it doesn't allow me to use sqlsrv / mssql because of the reasons i wrote in the "NOTE" part :) - noa-dev

2 Answers

2
votes

To connect to sql sever using sqlsrv:

<?php

error_reporting(E_ALL);
$serverName = "SuperComputer-PC";

$connectionInfo = array('Database'=>'RiverDatabase', "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect($serverName, $connectionInfo);


if($conn) {
     "Connection established.<br />";
}else {
     "Connection could not be established.<br />";
    die(print_r(sqlsrv_errors(), true));
}


?>

Download php SQL Server from: http://www.microsoft.com/en-za/download/details.aspx?id=20098

and copy the relevant drivers to your php ext folder. add the extensions to the php.ini file, for example: enter image description here

I am using 5.4, but this should work for 5.5, I am not sure if a driver exists for 5.6 yet, but if it does, that's great.

-1
votes

it might be 1) Authorization of pc needed Or 2)port number(lesser chances).

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Or Try

try
{
if ($db = mysqli_connect($hostname_db, $username_db, $password_db))
{
    //do something
}
else
{
    throw new Exception('Unable to connect');
}
}
catch(Exception $e)
{
echo $e->getMessage();
}

For Connection Problem:

If this happens always, it literally means that the machine exists but that it has no services listening on the specified port, or there is a firewall stopping you.

If it happens occasionally - you used the word "sometimes" - and retrying succeeds, it is likely because the server has a full 'backlog'.

When you are waiting to be accepted on a listening socket, you are placed in a backlog. This backlog is finite and quite short - values of 1, 2 or 3 are not unusual - and so the OS might be unable to queue your request for the 'accept' to consume.

The backlog is a parameter on the listen function - all languages and platforms have basically the same API in this regard, even the C# one. This parameter is often configurable if you control the server, and is likely read from some settings file or the registry. Investigate how to configure your server.

If you wrote the server, you might have heavy processing in the accept of your socket, and this can be better moved to a separate worker-thread so your accept is always ready to receive connections. There are various architecture choices you can explore that mitigate queuing up clients and processing them sequentially.

Regardless of whether you can increase the server backlog, you do need retry logic in your client code to cope with this issue - as even with a long backlog the server might be receiving lots of other requests on that port at that time.

There is a rare possibility where a NAT router would give this error should it's ports for mappings be exhausted. I think we can discard this possibility as too much of a long shot though, since the router has 64K simultaneous connections to the same destination address/port before exhaustion.