2

I am trying to connect my database with MySQL, but I have the following error:

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

I have my project in the following path (I'm using OS X Capitan): /Applications/ XAMPP/htdocs/Projects/Cupon. I'm trying to complete the get and set methods automatically with the command: php app/console doctrine:generate:entities TiendaBundle.

Class code (Project/Cupon/src/Cupon/TiendaBundle/Entity/Tienda.php):

<?php

namespace Cupon\TiendaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity */

class Tienda
{

  /**
  * @ORM\Id
  * @ORM\Column(type="integer")
  * @ORM\GeneratedValue
  */
  protected $id;

  /** @ORM\Column(type="string", length=100) */
  protected $nombre;

  /** @ORM\Column(type="string", length=100) */
  protected $slug;

  /** @ORM\Column(type="string", length=10) */
  protected $login;

  /** @ORM\Column(type="string", length=255) */
  protected $password;

  /** @ORM\Column(type="string", length=255) */
  protected $salt;

  /** @ORM\Column(type="text") */
  protected $descripcion;

  /** @ORM\Column(type="text") */
  protected $direccion;

  /**
   * @ORM\ManyToOne(targetEntity="Cupon\CiudadBundle\Entity\Ciudad")
   * @ORM\JoinColumn(name="ciudad_id", referencedColumnName="id")
   */
  protected $ciudad;

}

I have a database called "symfony" in MySQL (XAMPP 5.6.3) and my configuration file in Symfony is as follows:

parameters:
    database_driver: pdo_mysql
    database_host: 127.0.0.1
    database_port: null
    database_name: symfony
    database_user: root
    database_password: '123'
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    locale: en
    secret: c11eccf2a788b331cb9548ff4106c7461

I don't know how I can connect my project Symfony with my database in phpmyadmin.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Are you sure your configuration file is ok? why are you using null for port property? –  Oct 17 '15 at 09:59
  • Alireza, I think so, I also used the port 3306, but I have the same problem, I've tried a thousand different ways but can not connect, you might help me a list of steps to make the connection phpmyadmin-symfony, thanks – Juan Carlos HC Oct 17 '15 at 10:09
  • 2
    Is the mysql service running? Did you try to replace 127.0.0.1 to localhost? – Iago Oct 17 '15 at 10:15
  • Iago, the service is running, If I change the port to localhost I have a different error: [Doctrine\DBAL\Exception\ConnectionException] An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory – Juan Carlos HC Oct 17 '15 at 10:27
  • 1
    Try to set the unix_socket as in this tutorial: http://stackoverflow.com/questions/19629932/symfony-2-sqlstatehy000-2002-connection-refused-error – Iago Oct 17 '15 at 20:50
  • Iago is saying "Did you test to replace 127.0.0.1 by localhost ?" He didn't speak about the port, but about the driver ;). Connect to mysql in console (or via phpmyadmin) and return us the result of this query : SHOW GRANTS FOR CURRENT_USER(); – Alexandre Tranchant Oct 19 '15 at 09:28
  • try clearing your cache `php app/console cache:clear` most of the times it's about caching. – mdh.heydari Oct 22 '15 at 18:31
  • @Juan Carlos HC Not sure if this makes a difference but in my parameters.yml file, the password is without quotes. You could also remove the reference to driver, that is not in my parameters either. – Michael Emerson Nov 04 '15 at 12:13

1 Answers1

2

I hope you have your problem already solved, if not my solution may help you.

I had a very similar problem, but I was using MAMP, I solved my problem changing the configuration of the pdo_mysql.default_socket in the /etc/php.ini.

Why? I used terminal to create the database with

php app/console doctrine:database:create

and I use MAMP to test the site.

So, the problem, terminal was using a diferent pdo_mysql.default_socket than MAMP.

MAMP uses the config from

/Applications/MAMP/bin/php/php5.3.6/conf/php.ini

and terminal use the

/etc/php.ini

My solution:

change in my /etc/php.ini the line

pdo_mysql.default_socket=/var/mysql/mysql.sock

with:

pdo_mysql.default_socket=/Applications/MAMP/tmp/mysql/mysql.sock

Also I change my default zone:

date.timezone = "Europe/Bucharest"

I hope, this could help you doing something simmilar but now using your XAMMP socket.

Sorry for my English.

Patino
  • 36
  • 2
  • Thanks this helped me, in my case I had to do sudo find / -name mysql.sock to find where it was for my xampp installation, then I followed your directions and it worked! Some things to note is for some reason my php.ini file said it was located in /etc/ but was not, so I had to copy one there to get it to work. Hope this helps someone. – Joseph Astrahan Aug 01 '16 at 09:22