I just installed Laravel on my local Mac after installing ddev, Docker and MAMP. I used https://ddev.readthedocs.io/en/stable/users/cli-usage/#laravel-quickstart guide and everything looked ok. However, when I tried to run a sample code downloaded from the Internet, I started getting the error message
Illuminate \ Database \ QueryException (2002)
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from `users` where `email` = frankie@example.com limit 1)
In the beginning, I thought it was something related to an internal Lavavel error as I saw the email address did not have any quotes. However, I tried to connect to MySQL from this PHP script I took from MAMP (section Connect via Network to MySQL)
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_password = 'root';
$db_db = 'information_schema';
$db_port = 8889;
$mysqli = new mysqli(
$db_host,
$db_user,
$db_password,
$db_db
);
if ($mysqli->connect_error) {
echo 'Errno: '.$mysqli->connect_errno;
echo '<br>';
echo 'Error: '.$mysqli->connect_error;
exit();
}
echo 'Success: A proper connection to MySQL was made.';
echo '<br>';
echo 'Host information: '.$mysqli->host_info;
echo '<br>';
echo 'Protocol version: '.$mysqli->protocol_version;
$mysqli->close();
?>
And it was when I realised I cannot connect to MySQL from any PHP script/code run on MAMP (MySQL 5.7.32 ). This is the error I got:
Errno: 2002
Error: No such file or directory
Then after reading other similar questions on here, I changed localhost for 127.0.0.1 and got this new message
Errno: 2002
Error: Connection refused
Notes: MySQL is listening on port 8889
The file /Applications/MAMP/tmp/mysql/mysql.sock exists
The user and passwords are correct
The database also exists
My first thought was about lack of quotation for the email address, that's why I wrote my initial question here SQL/Laravel is not quoting text fields when building a query. However, the real issue is about trying to connect to MySQL server