Symfony 2 SQLSTATE[HY000] [2002] Connection refused Error - symfony

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?

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

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

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

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

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

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

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.

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

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: ~

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

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:
Change the database_host parameter
Change the authorization inside the database server

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

Related

Symfony 2.8(Wamp) : [Doctrine\DBAL\Exception\ConnectionException] PDOException: SQLSTATE[HY000] [2002]

I try to create some entity to my Symfony projet using the console with the following command :
php app/console doctrine:generate:entity
but when i choose a name for it , i get this error :
[Doctrine\DBAL\Exception\ConnectionException] PDOException:
SQLSTATE[HY000] [2002] No connection could be made because the target
machine actively refused it
and this is my parameters.yml on my symfony project :
# This file is auto-generated during the composer install
parameters:
database_host: 127.0.0.1
database_port: 3306
database_name: symfony
database_user: root
database_password: root
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
secret: ThisTokenIsNotSoSecretChangeIt
I tried to replace 127.0.0.1 by Localhost but it still the same.
Im on Windows 7 and i use Wamp.
EDIT : My port number is 3307 ... Problem solved !
If you are using 127.0.0.1 then you need to specify a port (3306 in most cases). If you use localhost instead, the argument port will be ignored. Specifying the port number might work in your case.
It has to come from you db config: either your parameters are wrong for your db, either your db server rejects the connection even though the parameters are good.
what DBMS are you using?
Some DBMS require additional config (like Oracle).
If i were you i would first check that i am using the right pdo driver for my DBMS.

fail to make connection with mysql symfony2 doctrine

I have problem with database connection in symfony2.7
my config.yml is
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
and my parameters.yml
parameters:
database_host: 192.168.123.11
database_port: 3306
database_name: test
database_user: root
database_password: xxx
mailer_transport: smtp
mailer_host: localhost
mailer_user: null
mailer_password: null
secret: ThisTokenIsNotSoSecretChangeIt
my server is
192.168.123.11
password xyz
user data base account is
account name: root
password : xxx
all user privileges assigned to this user. Now when I run this it give me error
[Doctrine\DBAL\Exception\DriverException]
An exception occured in driver: SQLSTATE[HY000] [1130] Host 'ap-hof.swisstr
affic.local' is not allowed to connect to this MySQL server
any idea where I am doing mistake? i can access my server pluse user name access through ssh...
You must authorize remote access root on your mysql server.
try to delete this line in your my.cnf file :
bind-address = 127.0.0.1
For authorize connection root without local

Doctrine: Could not create database for connection

I'm currently working on a website using Symfony2 and doctrine2 for the database. I did everything from this tutorial:http://tutorial.symblog.co.uk/docs/doctrine-2-the-blog-model.html
However, when I try to do this:
php app/console doctrine:database:create
I get this error:
Could not create database for connection named `CHuts_DB`
SQLSTATE[28000] [1045] Access denied for user 'rootDB'#'localhost' (using password: YES)
And this is my parameters.yml:
parameters:
database_driver: pdo_mysql
database_host: localhost
database_port: '3396'
database_name: CHuts_DB
database_user: rootDB
database_password: ***
...
database_path: null
I don't understand why it's not working. I'm fairly new to Symfony2 and I've searched a bit on internet but I couldn't find someone with the same error message as me.
In PHPMyAdmin go to:
Users -> rootDB -> Structure (check CREATE)
and save privileges of user.
I think your user and/or password aren't correct.
You can try mysql command line:
> mysql -u rootDB -p
or install adminer.php in your webserver and try to verify the account.
This solution worked for me on Mac OS X with MAMP (or MAMP Pro).
Add database.unix_socket in app/config/parameters.xml
parameters:
database.driver: pdo_mysql
database.host: localhost
database.port: 8888
database.name: my-database-name
database.user: root
database.password: root
database.unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock
Add unix_socket in app/config/config.xml
# Doctrine Configuration
doctrine:
dbal:
# ...
unix_socket: "%database.unix_socket%"

Symfony2 and postgresql

I want to create a project using symfony2 with postgresql, but when I run the command: php app/console doctrine:database:create I got the error: Could create database named "test", could not find driver, I know It has something with drivers but I made sure wampserver load php_pdo_pgsql,
this is my parameters.yml file:
database_driver: pdo_pgsql
database_host: 127.0.0.1
database_port: 5432
database_name: test
database_user: postgres
database_password: openpgpwd
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: en
secret: eb10e110d2a3c3f3f5df66181eb10376109f
database_path: null
Try this. Tested on Symfony 2.7. In parameters.yml the database_driver does not work. To make postgre work with Symfony, in config.yml find
doctrine:
dbal:
driver: pdo_mysql
And change it to
doctrine:
dbal:
driver: pdo_pgsql
That should do the trick. You can get rid of database_driver in parameters.yml or use it as you change pdo_mysql to %database_driver% .
There is no need to tell that the php postgresql module is necessary.
I had the same problem, I solved it by installing php-pgsql

Cannot create database connection for Doctrine2 in Symfony2 with MAMP (Connection refused)

I'm in the process of learning Symfony2 for a project that me and some friends are taking on, and am taking a tutorial where you make a blog in Symfony2: http://tutorial.symblog.co.uk/docs/doctrine-2-the-blog-model.html
Problem
I can't seem to get Doctrine2 to work. I give the following instruction to the Terminal:
php app/console doctrine:database:create
And get the following error message:
Could not create database for connection named symfony
SQLSTATE[HY000] [2002] Connection refused
System info
I have a mac that runs Mountain lion and I use MAMP.
What I have done
I went to the config page (http://localhost/NameOfProject/web/config.php) and went to the place where you configure your database. There I put in the path, port, user, password, etc from the MAMP start page.
What I have tried
A blog I came across suggested to create a symbolic link to the the MAMP mysql socket with the following command:
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock
This did not make the error go away for me.
Some of my files in symfony (if they are relevant)
From app/config/config.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: en
secret: 0408312e2b9861c0c86dbb7488aceec4296054ec
database_path: /Applications/MAMP/tmp/mysql/mysql.sock
From 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
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: en
secret: 0408312e2b9861c0c86dbb7488aceec4296054ec
database_path: /Applications/MAMP/tmp/mysql/mysql.sock
The tutorial talks updating the database_* options in the parameters file located at app/config/parameters.ini. However, I don't have a parameters.ini file located in this folder! Only parameters.yml.
Edit: Solution:
I was using the wrong port number for MySql. When I used 3306 it worked.
I was using the wrong port number for MySql. When I used 3306, the standard mysql port, it worked.
(Thank you jperovic)

Resources