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条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-23 06:17

This is a bit stupid but I arrived on this post googling the same error, so maybe this can help someone.

In case you have this Connection refused Error double check that your database is up and running, in my case I simply forgot to power on MAMP...

查看更多
beautiful°
3楼-- · 2019-01-23 06:19

I had the same problem using MAMP because the default port is not 3306 but 8889.

You can find that information in the MAMP starting web page.

My parameters

parameters: database_driver: pdo_mysql database_host: 127.0.0.1 database_port: '8889' database_name: symfony_blog database_user: root database_password: root mailer_transport: smtp mailer_host: 127.0.0.1 mailer_user: null mailer_password: null locale: en secret: 7f03537e81f981683bc773b9ec7113ab5861adec database_path: null

查看更多
Melony?
4楼-- · 2019-01-23 06:20

There is a parameter unix_socket you can use within your config.yml.

See full configuration example:

# Doctrine Configuration
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   %database_driver%
                dbname:   %database_name%
                user:     %database_user%
                host:     %database_host%
                password: %database_password%
                unix_socket: /tmp/mysql.sock
查看更多
姐就是有狂的资本
5楼-- · 2019-01-23 06:21

If anyone is still stuck on this issue when using a virtual environment, I found if you are attempting to run database commands on the host machine and running into the "connection refused" error, try ssh-ing into your guest machine and running the commands directly so the path to mysql.sock is correct.

(I guess it makes sense that the relative path needs to be on the virtual guest machine, not the host machine).

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-23 06:24

Configuring symfony2/doctrine to connect to MySQL using host:port

  • comment out doctrine.dbal.path: %database_path% in app/config/config.yml
  • remove/comment out the parameter database_path from app/config/parameters.yml
  • check your configuration files for correct indentation
  • wrap either none of the values in app/config/parameters.yml single quotes or put all in double quotes.

Afterwards your configuration should look like this:

Parameters

# app/config/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"

    # comment out or remove
    # database_path: ~

Configuration

# app/config/config.yml

# ...
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8

        # path is for pdo_sqlite: i.e. %kernel.root_dir%/data/data.db3
        # comment it out !
        # path:   ~
查看更多
放荡不羁爱自由
7楼-- · 2019-01-23 06:29

i had the same issue and fixed it changing

database_host: 127.0.0.1

to

database_host: localhost

in parameters.yml

hopefully this helps

查看更多
登录 后发表回答