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
709 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 82

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
 
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 82

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 82

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now