Home > Mobile >  Symfony 5 : SQLSTATE[HY000] [2002] refusing connection
Symfony 5 : SQLSTATE[HY000] [2002] refusing connection

Time:09-29

I aam trying to create a new doctrine database using Symfony 5.

Recently Symfony changed using mysql by default to using pstgreSQL.

As I want to use mysql I changed the comment in .env from :

# DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=13&charset=utf8"

to

# DATABASE_URL="mysql://root:@127.0.0.1:3306/mywebsite?serverVersion=5.7"
DATABASE_URL="mysql://root:[email protected]:5432/mywebsite?serverVersion=13&charset=utf8"

in other words I just changed postgresql to mysql and commented what needs to be commented.

However when I run

php bin/console doctrine:database:create

I get the following error :

In AbstractMySQLDriver.php line 112: An exception occurred in driver: SQLSTATE[HY000] [2002] No connection has been established as the target computer rejected it (translated from french)

I found the following posts : An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused

and

In AbstractMySQLDriver.php line 112:

An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddr
esses: getaddrinfo failed: Hte inconnu.

in short both tells me to change "127.0.0.1" by "mysql"

but when running as such I get this error

In AbstractMySQLDriver.php line 112:

An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddr esses: getaddrinfo failed: Hte inconnu.

What mistake did I do or what did I miss?

Thank you

CodePudding user response:

I found a very simple solutionthat will seems obvious for experienced dev but not for beginners that is never talked about.

just change the # at the beginning of the first line

# DATABASE_URL="mysql://root:@127.0.0.1:3306/mywebsite?serverVersion=5.7"
DATABASE_URL="mysql://root:[email protected]:5432/mywebsite?serverVersion=13&charset=utf8"

to the second line

DATABASE_URL="mysql://root:@127.0.0.1:3306/mywebsite?serverVersion=5.7"
 # DATABASE_URL="mysql://root:[email protected]:5432/mywebsite?serverVersion=13&charset=utf8"

CodePudding user response:

You can found documentation here https://www.doctrine-project.org/projects/doctrine1/en/latest/manual/introduction-to-connections.html

In your config the port 5432 is not default port of Mysql

Try

  DATABASE_URL="mysql://root:[email protected]:3306/mywebsite?serverVersion=13&charset=utf8"
  • Related