-1

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

  • Does this answer your question? [MySQL connection not working: 2002 No such file or directory](https://stackoverflow.com/questions/1676688/mysql-connection-not-working-2002-no-such-file-or-directory) – Nico Haase Nov 01 '21 at 08:57

3 Answers3

0

email has to be in apostrophe see When to use single quotes, double quotes, and backticks in MySQL

Like

select * from `users` where `email` = 'frankie@example.com' limit 1

also i hope you rest of the code use prepared statements with parameters to prevent sql injection

Your connection has to be changed by adding the port information as well

$mysqli = new mysqli( $db_host, $db_user, $db_password, $db_db, ,$db_port );

As you don't use the standard port 3306 Port but 8889

nbk
  • 45,398
  • 8
  • 30
  • 47
  • I thought the same and that's why I wrote my first question https://stackoverflow.com/questions/66938556/sql-laravel-is-not-quoting-text-fields-when-building-a-query. However, it is not about that. The issue is about the connection @nbk. It is a demo I downloaded, I did not write or altered any single line of code –  Apr 05 '21 at 08:05
  • the error message was clear , connction probolem a sysnax problem. For connecting see if you can connect vioa phpmyasdmn or mysql client. the change to 127.0.0.1 only forces the mysql co9nnctor to use tcpip instead of sockets, but i think still you have a problem that the server isnot running becvause sockets should work. so cheklc the error log of mysql – nbk Apr 05 '21 at 10:39
  • Please share more details. If connecting to to the server fails already, how could this resolve the problem? – Nico Haase Nov 01 '21 at 08:58
  • @NicoHaase please open your own qauestion, provide all data necessary, error message what system you are using and mysql configuration, there must be a lot of information, to help with such problems – nbk Nov 01 '21 at 11:07
  • I don't have any question. I'm just trying to understand your answer – Nico Haase Nov 01 '21 at 12:20
  • my answer initially took the error code provided and explained that the code is wrong without single quotes and that he should read about sql injection, then the user presented a second problem, a connection problem, which is because he had a different port from 3306, but i didn't see the edit, so i adjusted my answer – nbk Nov 01 '21 at 12:27
0

The issue there is that you are not passing the port as the fifth parameter to mysqli connection. If you are using localhost as your host then the port parameter is ignored. This is why in the MAMP snippet, they are not passing $db_port as a parameter, it simply is not needed. The reason this happens, is that localhost does not use TCP/IP, instead it makes use of unix sockets.

After you've followed other's suggestion, you've probably changed the code to the point where you had

$db_host = '127.0.0.1';

This is ok, but since you are no longer using unix sockets (localhost), the port parameter is needed in order to establish a connection. Mysqli uses the 3306 port as default one if you do not specify any other as fifth parameter. So that is the port you are trying to connect to by using that code. This is where the Error: Connection refused comes from.

To solve the issue you just have to pass the port as a fifth parameter like this:

$mysqli = new mysqli($db_host,$db_user,$db_password,$db_db,$db_port);

The snippet itself is great, it just did not fully suit your needs and you had to do a bit of tweaks to it. If you want to use snippets you have to understand what they do and in this case how mysqli works. Make sure to remember this for the future as it will for sure avoid you unpleasant situations and errors like this one.

Croluy
  • 9
  • 3
0

After switching to new macbook with m1 chip i got similar issue. In my particular case i was using nginx + php within docker containers but MySQL was installed on host machine (due to the fact that there is no official docker image for MySQL for M1 chip yet).

In my application configuration i had following settings:

host: localhost (or 127.0.0.1)
port: 3306
user: root
password: mypassword

And again, in my particular case the application couldn't reach mysql server since PHP was running inside docker container and since i used localhost as a host that was a localhost within PHP container where mysql server is not installed.

To solve my problem i changed configuration to this:

host: host.docker.internal
port: 3306
user: root
password: mypassword

In the situation of the author of the question, the solution would be to change configuration on:

host: db
port: 3306
database: db
user: db
password: db

so host/database/db user/db password - should be set to db as it is said in the documentation here https://ddev.readthedocs.io/en/stable/users/cli-usage/#laravel-quickstart

The similar problem could happen if you are using own docker environment with mysql + php + nginx via docker-compose. Then, if you let's say in your docker-compose.yml file have something like that:

mysql:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example

Then in you application configuration you should use the following setting to reach mysql server:

host: mysql
port: 3306
user: root
password: example

So, mysql as a host, since the "service" name of mysql in docker-compose.yml file is set to mysql

Denis Gorgul
  • 79
  • 1
  • 3