Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 776
  • Last Modified:

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

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
MYSQLuser
Asked:
MYSQLuser
  • 3
  • 3
  • 2
1 Solution
 
nemws1Commented:
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
 
MYSQLuserAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
nemws1Commented:
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
 
MYSQLuserAuthor Commented:
*** 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
 
Dave BaldwinFixer of ProblemsCommented:
"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
 
MYSQLuserAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now