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:
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.
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>
Confirm Resolution:
MySQL should now function normally. Verify by running:
<code class="bash">mysql -u root -p</code>
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 "$dir/mysql" ] && echo "$dir/mysql" && 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!