title: mysqli_connect(): (HY000/2002): Can't connect to local MySQL server through socket
body: <p>I need help bad...I got this error trying to display a database using php:</p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <p>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 <a href="http://tinyurl.com/q4cpxzj" rel="noreferrer">http://tinyurl.com/q4cpxzj</a>) with absolutely no luck. </p>
  
  <p>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).</p>
  
  <p>Tried doing the same to /etc/my.cnf file. No good.</p>
  
  <p>Tried a bunch of other things I can't even remember anymore.</p>
  
  <p>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).</p>
  
  <p>Please help...</p>
  
  <p>If it matters, here's the php: </p>
  
  <pre><code>&lt;?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() . ")"
      );
    }
  ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     "http://www.w3.org/TR/html4/loose.dtd"&gt;
  
  &lt;html lang="en"&gt;
      &lt;head&gt;
          &lt;title&gt;Databases&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;?php
    // 5. Close database connection
    mysqli_close($connection);
  ?&gt;
  </code></pre>