Home >Database >Mysql Tutorial >How to Fix MySQL Error #2002: \'Can\'t Connect to MySQL Server\'?

How to Fix MySQL Error #2002: \'Can\'t Connect to MySQL Server\'?

Susan Sarandon
Susan SarandonOriginal
2024-10-23 23:09:301096browse

How to Fix MySQL Error #2002:

Troubleshooting Error #2002: Can't Connect to MySQL Server

The "Error #2002 Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2)" encountered when running MAMP typically signifies a missing or inaccessible mysql.sock file. To resolve this issue, follow the steps outlined below:

  1. Test the Connection:

    Attempt to start MySQL using the full path to the binary:

    <code class="bash">/Applications/MAMP/Library/bin/mysql -u root -p</code>

    If this works, proceed to the fix step.

  2. Create a Symlink:

    If the test is successful, execute the following command to create a symlink to the mysql.sock file:

    <code class="bash">sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock</code>
  3. Confirm Resolution:

    MySQL should now function normally. Verify by running:

    <code class="bash">mysql -u root -p</code>
  4. Dynamic Path Finding (Alternative):

    If the previous method fails, you can dynamically find the MySQL path:

    <code class="bash">$($(for dir in /usr/local/mysql/bin /usr/bin /usr/local/bin /Applications/MAMP/Library/bin /Applications/XAMPP/xamppfiles/bin; do [ -x &quot;$dir/mysql&quot; ] &amp;&amp; echo &quot;$dir/mysql&quot; &amp;&amp; break; done) -u root -p)</code>

The above is the detailed content of How to Fix MySQL Error #2002: \'Can\'t Connect to MySQL Server\'?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn