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
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:
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.
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.
Related
When I try to run the below program I am get error as MySQL server has gone away.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Google says that it is due to
2
Try to debug the problem. This can be caused by any number of things.
Commonly there are:
The MySQL server crashed
A comms problem between the client and server
Abusing the client library in a way not intended, perhaps sending commands out of order or sending junk through the socket.
I came to solution as to debug the problem. How to debug to find the correct reason as to why this error is happening?
First check version of php installed in your computer
The MySQLi extension is designed to work with MySQL version 4.1.13 or newer.
if you have required version
am suggesting you to unistall your server for example if is xampp then install it again
it will work
Actually I have try to connect SQL server by using sqlsrv_connect and using the DSN (Data Source Name) without Apache service then the both are perfectly working.
The problem is when I turn on the apache service then php cannot connect to the sql server using odbc DSN (Working with sqlsrcv_connect).
The condition is I need to turn on the apache with a application running using sql server which using DSN. I working with crystal report thats why really need this method. I have tried using system DSN instead user dsn, its also not working.
!
I wrote code below to test my scenario
// Connect to the data source
$conn=odbc_connect('DSNNAME','DBUSER','DBPASS');
if ($conn){
echo "Connection established DSN";
}
else {
echo "Connection using DSN Failed:" . odbc_errormsg();
}
// Connect through server name
$serverName = "WEBSERVER\SQLEXPRESS"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"DBNAME", "UID"=>"DBUSER", "PWD"=>"DBPASS");
$conn2 = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn2 ) {
echo "Connection established using server name";
}else{
echo "Connection could not be established by using server name";
die( print_r( sqlsrv_errors(), true));
}
The Result when Apache service is on.
Make sure the ODBC DSN matches the Crystal runtime in terms of 32-bit or 64-bit.
Ok so I created an ubuntu instance from AWS and downloaded LAMP on it as you would in a regular Ubuntu OS. When I try to connect to the db from my php script it's rejecting the connection. saying "Connection failed: Connection refused". So I guess my question is this: Does amazon not allow you to connect to a db without using their RDS database service or am I putting something wrong here? (I've hidden some of the data for security purposes, the ... are numbers of my instance). When I put "ec2-34-...-..-64.compute-1.amazonaws.com" into the browser the apache message comes up so I don't see why this is not working as a server name?
$servername = "ec2-34-...-..-64.compute-1.amazonaws.com";
$username = "root";
$password = “hidden";
$dbname = "questions87";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
You need to change your EC2 Security Group Inbound Rules related with 3306, you can find that on the AWS FAQ. Then, you need to be sure that your user#YOURIP have all permissions on your MySQL database. For your Inbound Rules it is recommended to use a Custom Rule to your IP, not All Traffic.
Reference and further reading:
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html
I have a PC that is running some FTP via PHP that I know used to work 1-2 months ago, but now I return to it I find that the PC is no longer working. I know I have been using the PC but I cannot think of what might have changed.
The PHP is throwing out error messages reading
Unable to build data connection: Connection refused
...when I use the ftp_put() function.
The cut down code I am using is:
<?php
$trackErrors = ini_get('track_errors');
ini_set('track_errors', 1);
$server="***.***.***.***";
$port=21;
echo "<LI>Connecting to $server:$port<BR>";
$conn_id = ftp_connect($server,$port,9999999) or die("<BR>Unable to connect to ".$server.":$port server.");
if ( !$conn_id ) {
$errmsg = $php_errormsg;
echo "<BR><LI>ERR:$errmsg";
}
else {
$passive=false;
echo "<LI>Setting Passive Mode=$passive";
ftp_pasv($conn_id, $passive);
$user="*********";
$pass="*********";
echo "<LI>Connecting as $user/*****";
if (!ftp_login($conn_id, $user, $pass)) {
$msg = "Failed to login to $selected_server as $user; <BR>check logincredentials in the Settings";
echo "<BR><LI>$msg";
$errmsg = $php_errormsg;
echo "<LI>ERR:$errmsg";
return $msg;
}
ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 10000);
if (!#ftp_put($conn_id, "test.txt", "C:......test.txt", FTP_BINARY)) {
echo "<BR><LI>ftp_put failed";
$errmsg = $php_errormsg;
echo "<LI>ERR:$errmsg";
}
echo "<HR>Done";
}
?>
the output when running this as a webpage is
Connecting to ***.***.***.***:21
Setting Passive Mode=
Connecting as *******/*****
ftp_put failed
ERR:ftp_put(): Unable to build data connection: Connection refused
Done
The result is that the ftp_put() gives the error message and leaves a zero (0) byte file with the right filename on the server.
The strange thing is is that
the same code/connection info works on another laptop ok
the same connection info works ok using FileZilla when pushing a file
the problem occurs on several servers (ie. it's not just one specific destination that has the problem)
Also, this doesn't seem to have anything to do with the passive mode (it fails with and without this enabled)
Does anyone have any suggestions?
Thanks
Abe
You are using the active FTP mode. In the active mode the server tries to connect to the client. In most network configurations, that's not possible as the client machine is usually behind a firewall.
That's why the server fails with:
Unable to build data connection: Connection refused
It's specifically ProFTPD error message for this situation.
See my article on the active and passive FTP connection modes for details.
The code can work on other machines, if they have firewall disabled or if they have rules that allow incoming traffic on unprivileged ports.
FileZilla works because it defaults to the passive mode (as most modern FTP clients do).
You have claimed to try the passive mode too, yet to get the same error message.
That's because you are using the ftp_pasv call incorrectly.
You have to move the ftp_pasv call after the ftp_login.
$user = "*********";
$pass = "*********";
echo "<LI>Connecting as $user/*****";
if (!ftp_login($conn_id, $user, $pass)) {
// ...
}
$passive = true;
echo "<LI>Setting Passive Mode=$passive";
ftp_pasv($conn_id, $passive);
The documentation clearly suggests it:
Please note that ftp_pasv() can only be called after a successful login or otherwise it will fail.
For a similar issue (just with Pure-FTPd), see PHP upload via FTP - ftp_put() I won't open a connection to x.x.x.x (only to y.y.y.y).
I'm attempting to connect to a MS SQL Server via PHP7 sqlsrv driver.
It seems like when my browser tries to execute:
$conn = new PDO( "sqlsrv:Server=$serverName;Database=mydb", $uname, $pass);
or
$conn = sqlsrv_connect( $serverName, $connectionInfo);
it just hangs. No success or failure echo/die messages are received. Instead, the wheel in my browser keeps spinning and the status message is "waiting for localhost". It also doesn't seem to ever timeout. I've waited 10+ minutes and the connection never fails/succeeds.
Full code (I've tried procedural and PDO methods):
<?php
//
$serverName = "myserver\myinstance"; //serverName\instanceName
$uname = "myuser";
$pass = "mypass";
$connectionInfo = array( "Database"=>"mydb", "UID"=>$uname, "PWD"=>$pass);
/*Procedural sqlsrv method
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
die("DONE");
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
*/
/*PDO sqlsrv method*/
try {
$conn = new PDO( "sqlsrv:Server=$serverName;Database=mydb", $uname, $pass);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
die("success!");
} catch(PDOException $e) {
die($e->getMessage());
}
?>
Setup details and other information:
I'm using XAMPP.
Windows 7 x64
PHP 7.0.9
I'm trying to connect to an instance of Microsoft SQL Server version 10.50.6220.0 on a Windows NT machine
I need to authorize via SQL Server Authentication -- not Windows Authorization
I'm able to successfully connect to the database using Microsoft SQL Server Management Studio and other tools (FME Workbench)
The server is remote, but is accepting remote connections (as verified with SSMS)
I previously had issues with "undefined function sqlsrv_connect()" until I installed
php_sqlsrv_7_ts_x86.dll
and
php_pdo_sqlsrv_7_ts_x86.dll
into my php/ext folder and updated the php.ini. When I open phpinfo I see sqlsrv in both the "Registered PHP Streams" and "PDO drivers" fields.
What should I do to get a successful connection or get more information about why my connection attempt does not succeed/fail?
EDIT:
I've switched to XAMPP (PHP 5.6.24). I am able to connect to the SQL Server via CLI now, but NOT able to connect via PHP-CGI (through the browser/served by Apache HTTPD). I've verified again through php_info() that the sqlsrv and PDO extensions have been successfully loaded. I'm going through the differences between PHP-CGI and PHP CLI now to see what might be causing the issue.
EDIT:
I'm using IIS now instead of Apache and am able to connect via sqlsrv without issues. Not really a solution, but a way around the problem.
try like that :
try {
$db = new PDO( 'sqlsrv:server=serwer_name;Database=database_name', 'user', 'password');
} catch (PDOException $e) {
echo $e->getMessage();
}