Solved

MYSQL issue - Failed to connect to MySQL: (2002) No connection could be made because the target machine actively refused it.

Posted on 2013-05-13
8
730 Views
Last Modified: 2013-05-20
I am trying to test a simple php application to connect to mysql . I give the hostnamem user and password, and database name. But it always fails.

But i am able to do it with the command line. ( same username,passowrd, db).



<?php
$mysqli = new mysqli("hostname", "username", "password", "mysql");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";

$mysqli = new mysqli("hostname", "username", "password", "mysql", 3306);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

echo $mysqli->host_info . "\n";
?>


and also when I did  telnet ipaddressof the remote server 3306 -- Could not connect error,
But when I do telnet complete url address(sample.com) 3306 - It works fine


Please let me know what might be the issue.

Thanks.
0
Comment
Question by:MYSQLuser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 39162674
When you connect on the command line are you on the same server as your MySQL database?  Is your PHP/Web on the same server or on a different server?

MySQL does *3* step authentication:  username, password, and *originating host*.  I'm guessing the last one is the issue.
0
 

Author Comment

by:MYSQLuser
ID: 39162690
Yes I tried the command line on the same server wherethe MYSQL is installed.

PHP/web is on the different server,

-- Originating host is the one I mentioned the mysql server name.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39162696
If the name works and the IP address doesn't, it's almost certain you have the wrong IP address cause MySQL doesn't care.

Your code looks exactly the same as my working code so the problem must be in the details.  Are you connecting on the command line on the same machine that the PHP code is on?  If you are not, then you need to make sure that that user has privileges to make a remote '%' anyhost connection.  That is a separate privilege from 'localhost'.


http://us2.php.net/manual/en/mysqli.construct.php
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 23

Expert Comment

by:nemws1
ID: 39162715
If you look in the 'mysql' database, at the 'user' table, you have an entry for the username/password combo you are trying with 'localhost' in the 'host' column.  You want to *leave this alone* and issue a new grant statement:

GRANT *.* ON your_database_name to 'username'@'php_host_name' identified by 'password';

Replace the underlined parts.
0
 

Author Comment

by:MYSQLuser
ID: 39162719
*** When I ping the name of the db server where mysql is installed I get the Ip address. Now I am trying to telnet the ip address but this fails but when doing with the name telneting is fine. So i am not sure why this is not accepting?

**** Yes I am using the command line from other server and not from the php application server.


>>>If you are not, then you need to make sure that that user has privileges to make a remote '%' anyhost connection.  That is a separate privilege from 'localhost'.

*** How can I check or do this?  But just wantto let you know that the user root has allprivileges for my ip adrresss, % and :: also
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39162841
"But just want to let you know that the user root has allprivileges for my ip adrresss, % and :: also "

You should Never enable 'root' for remote access.  That is practically inviting someone to break into your database server.

http://dev.mysql.com/doc/refman/5.1/en/adding-users.html

I believe the error message is from the PHP driver, not from the server.  Check the firewall on the MySQL server machine and make sure it allows incoming connections on port 3306.
0
 

Author Comment

by:MYSQLuser
ID: 39163480
Thanks for the responses so far.. i appreciate it..

The firewall in mysqlsserver is switched off and the port 3306 is enabled.

I wantto let u know last week my sys admin made the changes from local host to my mysql server (complete name) -- then I edited to change the local host to myswl server name in the Mysql server instance. After this change I restarted and then the telnet worked both with the Mysql server ip address and with the full name. My php sample application was also working fin .But all of a sudden today it has this issue.

Is there something might be changed? I checke dthat the host is still the same mysql srver name and not the local host.


Please let me know of any input .
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39163743
The default 'bind-address' is 0.0.0.0 so the sever can accept connections from any 'interface' on the machine.  Setting it to something else restricts connections to that one IP address.  Not recommended unless you really have a need for it.

http://dev.mysql.com/doc/refman/5.1/en/server-options.html
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question