Symfony 2 SQLSTATE[HY000] [2002] Connection refused Error
Asked Answered
B

12

16

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?

Breann answered 28/10, 2013 at 8:19 Comment(7)
Are you sure that you can connect to MySQL on 127.0.0.1:8889 with username/password root:root ? The default MySQL port 3306 btw.Germinate
Yes I'm sure try{ $conn = new PDO('mysql:host=127.0.0.1;dbname=symfony; port=8889', 'root','root'); }catch (PDOException $e){ echo $e->getMessage(); } $sql = "SELECT * FROM Pages"; foreach ($conn->query($sql) as $row) { echo $row['name'].'<br />'; } it's workingBreann
try to comment out the database_path directive - do you want to connect through 127.0.0.1:8889 or the socket? If you want to use the socket - did you configure MySQL to use this socket location and tried connecting through it aswell?Germinate
... and remove the single quotes ' around your port :)Germinate
database path: null I tried did not work in the form ofBreann
did you remove the single quotes? either ommit them completely or use double-quotes to enter a string in YAML.Germinate
I have tried without the quotes problem continuesBreann
S
14

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
Sanative answered 28/10, 2013 at 9:41 Comment(1)
As stated in the comments above he wants to connect through ip:port not through a socket but this might definitely help others :)Germinate
C
40

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

Ceratodus answered 3/11, 2014 at 17:18 Comment(1)
In later versions of Symfony this may also be in your .env file, specifically the DATABASE_URLColettacolette
M
16

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

Minna answered 24/2, 2016 at 20:15 Comment(0)
S
14

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
Sanative answered 28/10, 2013 at 9:41 Comment(1)
As stated in the comments above he wants to connect through ip:port not through a socket but this might definitely help others :)Germinate
S
8

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
Stromboli answered 3/8, 2014 at 17:36 Comment(1)
I'm using OSX and I just started mysql in Terminal then entered SHOW VARIABLES WHERE Variable_name = 'port';. Port number was 8889, I entered it in parameters.yml and connection finaly made.Hedi
C
5

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...

Croupier answered 16/2, 2016 at 19:12 Comment(0)
A
3

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

Arluene answered 9/2, 2018 at 19:37 Comment(0)
K
2

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.

Kassel answered 19/2, 2014 at 12:44 Comment(0)
M
2

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).

Mistrial answered 6/1, 2016 at 9:22 Comment(0)
T
1

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:   ~
Timmons answered 28/10, 2013 at 9:34 Comment(0)
S
1

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).

Succinic answered 17/5, 2016 at 18:9 Comment(0)
U
1

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
Unlive answered 16/7, 2016 at 14:30 Comment(0)
S
1

In MAMP PRO I have activated "Allow Network Access to mysql" and my "php artisan migrate" is working now for that special app.

Saharan answered 16/4, 2019 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.