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
718 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
  • 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

825 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