0

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

noa-dev
  • 3,561
  • 9
  • 34
  • 72

2 Answers2

2

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.

kya
  • 1,798
  • 8
  • 35
  • 61
  • Hello Kya, thanks for your answer. sqlsrv extension doesnt seem to be enabled, do you have any hints for that? When I loook into the ext/ direcoty I see pdo sql srv extensions for php 5.6 (which i am using) but i dont see sqlsrv in the php info – noa-dev Mar 12 '15 at 08:45
  • Hi, I have updated the system variable to the directory of my PHP yes – noa-dev Mar 13 '15 at 08:59
  • I still can't do it :( – noa-dev Mar 18 '15 at 06:58
  • I've already seen this video. But I am neither using xampp nor wamp. Should work without a simulated server since I its connected to our network as a server and installed as a webserver – noa-dev Mar 20 '15 at 07:17
-1

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.

Pranav Shah
  • 89
  • 11
  • I am actually connecting with the Serveradmin User, so the permissions should be alright, shouldnt they ? I've tried your code as well, I get the same error message. – noa-dev Mar 11 '15 at 11:08
  • add at the end--> mysqli_close($conn); – Pranav Shah Mar 11 '15 at 11:15
  • http://stackoverflow.com/questions/24096657/no-connection-could-be-made-because-the-target-machine-actively-refused-it-127-0 – Pranav Shah Mar 11 '15 at 11:16
  • the mysqli_close function call only terminates the sql connection if there was one , this sadly wont solve my problem Thank you for your help tho. The topic you ve linked doesnt really link with mine or does it ? The mashine I am trying to connect to is in the same network, but not on my localhost – noa-dev Mar 11 '15 at 11:20
  • thanks for your fast replies, i really appreciate it still same old problem :Warning: mysqli_connect(): (HY000/2002): Es konnte keine Verbindung hergestellt werden, da der Zielcomputer die Verbindung verweigerte. in C:\inetpub\wwwroot\db.php on line 14 Unable to connect – noa-dev Mar 11 '15 at 11:31
  • English please so can help you – Pranav Shah Mar 11 '15 at 11:36
  • translated into english : No connection could be made because the target machine actively refused it . Same as before – noa-dev Mar 11 '15 at 11:38
  • I see. Pardon my mispelling. The connectivity issue happens always. I have disabled the firewall for test purposes to eliminate any confilicts that might occur because of it. Maybe the serveraddress which im entering is wrong ? I am using the 192.... IP in the $servername variable which should actually work ... – noa-dev Mar 11 '15 at 11:53
  • $servername = "localhost"; $username = "root"; $password = ""; see is this work – Pranav Shah Mar 11 '15 at 11:59
  • the server isnt hosted on my Computer, its just in the same network - so i can't write localhost. Thats why I write the IP in $servername - > if I enter the same IP in my browser I get access to the root web directory of my Server – noa-dev Mar 11 '15 at 13:10
  • mysqli is strictly for mysql database and cannot be used to connect to the sql server. The user must either use pdo or sqlsrv – kya Mar 12 '15 at 08:24