ade leye
ade leye

Reputation: 79

“Connection refused” error when connecting to local MySQL server?

I am having this error:

Warning: mysql_connect(): [2002] Connection refused (trying to connect via tcp://127.0.0.1:3306)

I saw a similar question and I tried the solution by changing localhost to 127.0.0.1, but it is still not working. I am using Mac OS X Lion. Please any help would be appreciated

This is the PHP code I use to connect:

<?php
    require("constants.php");
    // open database
    $connect = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die('error connecting: ' . mysql_error());
    // select database
    mysql_select_db(DB_NAME) or die('error selecting db: ' . mysql_error());
?>

Upvotes: 6

Views: 9388

Answers (2)

Vijfhoek
Vijfhoek

Reputation: 37

I've had that problem before. You can fix it by commenting out "skip-networking" in the configuration. (my.ini or my.cnf, it's /etc/mysql/my.cnf on Linux, not sure about Mac)

-- edit
Also, you can try typing "netstat" in the Terminal, to make sure that mysql is listed there (should be something like 127.0.0.1:mysql or 127.0.0.1:3306). If it is not listed, MySQL either hasn't started, or hasn't been able to bind.

Upvotes: 0

Lee Avital
Lee Avital

Reputation: 542

Try making sure your firewall if open to connections on port 3306.

The command should be ipfw set enable 3306, but if you want you can read more about actually setting the firewall permissions.

Another thing could be to make sure MySQL is listening on port 3306, edit the my.ini file and set

[client]
port=3306

And then restart MySQL.

Upvotes: 1

Related Questions