4
votes

I need help bad...I got this error trying to display a database using php:

mysqli_connect(): (HY000/2002): Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2) in /Applications/MAMP/htdocs/databases.php on line 7 Database connection failed: Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2) (2002)

I've been trying to fix it for days, reading through this forum from people with the same issues, and applying the fixes (like this one http://tinyurl.com/q4cpxzj) with absolutely no luck.

Here's some steps I've tried: Going into php.ini file and changing the socket to /tmp/mysql.sock (I also tried changing it to /Applications/MAMP/tmp/mysql/mysql.sock to see what happens, no good).

Tried doing the same to /etc/my.cnf file. No good.

Tried a bunch of other things I can't even remember anymore.

Also, I'm using Mac 10.10.2 and I have MAMP installed. According to MAMP, only Apache is working (green light by Apache and nothing on MySQL) but I've downloaded MySQL and according to System Pref it is running, although it won't let me stop MySQL server for some reason (when I click stop it would stop then restart again on its own).

Please help...

If it matters, here's the php:

<?php
  // 1. Create a database connection
  $dbhost = "localhost";
  $dbuser = "widget_cms";
  $dbpass = "*********";
  $dbname = "widget_corp";
  $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  // Test if connection succeeded
  if(mysqli_connect_errno()) {
    die("Database connection failed: " . 
         mysqli_connect_error() . 
         " (" . mysqli_connect_errno() . ")"
    );
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
    <head>
        <title>Databases</title>
    </head>
    <body>

    </body>
</html>

<?php
  // 5. Close database connection
  mysqli_close($connection);
?>
1
Did you install MySQL from some other package than MAMP?tsnorri
I installed the Mysql community server from the mysql website.Henry Lee
After installing it did you try it without editing php.ini first...maybe Mac is different but on Windows, I've never edited php.ini to specify the socket.developerwjk
If you don't edit the file I think the default config is that address. But it didn't work so I tried putting it in anyway.Henry Lee

1 Answers

3
votes

JUST FOUND A SOLUTION!

I just replaced localhost with 127.0.0.1.

Oddly enough, I think I've tried this before. Maybe messing around with the config and retrying it got it working now.