Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli_connect(): (HY000/2002): Can't connect to local MySQL server through socket

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);
?>
like image 288
Henry Lee Avatar asked Oct 19 '22 16:10

Henry Lee


1 Answers

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.

like image 117
Henry Lee Avatar answered Oct 22 '22 18:10

Henry Lee