Maxim U
Maxim U

Reputation: 29

Symfony SQLSTATE[HY000] [2002] No such file or directory database connection

I'm working on MacOS using MAMP and trying to connect my Symfony framework with database. But I'm always getting these errors.

console errors

Here is my .env file:

# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
#  * .env                contains default values for the environment variables needed by the app
#  * .env.local          uncommitted file with local overrides
#  * .env.$APP_ENV       committed environment-specific defaults
#  * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=dfa38e65f5a82a254945036ae326e50e
#TRUSTED_PROXIES=127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
#TRUSTED_HOSTS='^(localhost|example\.com)$'
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
 DATABASE_URL="mysql://root:root@localhost:3306/test_project?serverVersion=5.7"
# DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=13&charset=utf8"
###< doctrine/doctrine-bundle ###

###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###

And here is my connection parameters:

Host        localhost
Port        8889
Username    root
Password    root
Socket      /Applications/MAMP/tmp/mysql/mysql.sock

Upvotes: 1

Views: 298

Answers (2)

Imrickjames
Imrickjames

Reputation: 129

You need to change your "port" to "8889" and not use "3306". you can config your database url to "DATABASE_URL=mysql://root:root@localhost:8889/test_project"

Upvotes: 1

Eugenio Carocci
Eugenio Carocci

Reputation: 161

As you can see you're trying to connect to port 3306 whereas the MySQL instance is working on port 8889.

The correct environment variable in the .env file is

DATABASE_URL="mysql://root:root@localhost:8889/test_project?serverVersion=5.7"

In addition to that, you've to be sure that the test_project database exists.

Upvotes: 1

Related Questions