Symfony 2 SQLSTATE[HY000] [2002] Connection refuse

2019-01-23 05:49发布

I get an error like database operations using Symfony2.

SQLSTATE[HY000] [2002] Connection refused

parameters.yml

parameters:
  database_driver: pdo_mysql
  database_host: 127.0.0.1
  database_port: '8889'
  database_name: symfony
  database_user: root
  database_password: root
  mailer_transport: smtp
  mailer_host: 127.0.0.1
  mailer_user: null
  mailer_password: null
  locale: tr
  secret: ef9b4381fe75208f060b7c786951242bebcfb3c2
  database_path: /private/var/mysql/mysql.sock

And console:

Kemal-Karakass-MacBook-Pro:~ kemalkarakas$ locate mysql.sock
/private/var/mysql/mysql.sock

How do I resolve the error?

11条回答
smile是对你的礼貌
2楼-- · 2019-01-23 06:30

On Mac OSX using Mamp Pro, what solved the problem for me was going to the MySQL tab on the UI and checking the option Allow network access to MySQL

Don't forget to click Save to update the settings

enter image description here

查看更多
Summer. ? 凉城
3楼-- · 2019-01-23 06:31

Running on MAMP, this is what configuration of /app/config/parameters.yml that worked for me:

parameters:
    database_host: '127.0.0.1'
    database_port: '8889'
    database_name: test
    database_user: root
    database_password: root

I lost quite some time with database_host: localhost, which didn't work.

Beginners Tips:

Don't forget to remove the semicolon ; to enable extension=php_pdo_mysql.dll in php.ini (in application/MAMP/conf/phpX.X.X - check your current version in MAMP settings) and/or in php.ini and/or php.ini.default in Macintosh HD/private/etc (use spotlight search to find this hidden file).

查看更多
We Are One
4楼-- · 2019-01-23 06:36

Had this issue "SQLSTATE[HY000] [2002] No such file or directory" on symfony 3.2.9 on deployment

I fixed it by changing the database_host value from "localhost" to my server IP on my parameters.yml file

but I had this another error message when trying to run commandline by SSH:

[Doctrine\DBAL\Exception\ConnectionException]
  An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused

I finaly fix it by adding these 2 lines on my config.yml file in Doctrine configuration section :

unix_socket: /var/lib/mysql/mysql.sock
server_version: '5.5'

all my final doctrine configuration is like this:

doctrine:
    dbal:
        driver: pdo_mysql
        host: '%database_host%'
        port: '%database_port%'
        dbname: '%database_name%'
        user: '%database_user%'
        password: '%database_password%'
        unix_socket: /var/lib/mysql/mysql.sock
        server_version: '5.5'
        charset: UTF8

hopefully this helps

查看更多
爷的心禁止访问
5楼-- · 2019-01-23 06:37

After seeing the answers and playing around with this myself I figured out the problem:

The default installation of LAMPP, MAMPP has the following in the my.cnf file:

skip-networking

This means that mysql will not listen on TCP/IP so you can tell doctrine to try any port in the 64K and it won't do any good until you comment out the directive above and get mysql to listen to TCP/IP ports.

Please ensure you restart LAMPP, MAMPP or reload mysql after commenting out the skip-networking directive.

查看更多
干净又极端
6楼-- · 2019-01-23 06:43

Today on a fresh installation of Symfony3 I had the same error:

SQLSTATE[HY000] [2002] Permission denied

Installation specs:

  • CentOS Linux 7.2.1511
  • Symfony 3.1.2
  • MariaDB 5.5.47
  • PHP 7.0.8

An earlier answer by izus did resolve the problem on my server. He suggests to change the parameter database_host from 127.0.0.1 to localhost in the Symfony configuration file app/config/parameters.yml

The real origin of the problem was not inside Symfony configuration, but authorization inside the database server. I've created the database and authorization for this database by executing the following SQL queries:

CREATE DATABASE user CHARACTER SET utf8 COLLATE utf8_bin;
GRANT ALL PRIVILEGES ON database.* TO user@localhost IDENTIFIED BY 'password';

This query implies only the host 'localhost' is authorized. This doesn't match '127.0.0.1' exactly. The database server rejects the incomming connection, and Symfony is throwing an exception and showing this error message.

Possible solutions to this specific situation:

  1. Change the database_host parameter
  2. Change the authorization inside the database server
查看更多
登录 后发表回答