42

I am new to Symfony2 (beta4) and Doctrine and am having issues when i try to create the DB schema via command line.

Here's the error:

$ php app/console doctrine:schema:create

Creating database schema...

[PDOException]                                    
SQLSTATE[HY000] [2002] No such file or directory  

[ErrorException]                                                                                          
Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) 
in /Applications/MAMP/htdocs/sf-test-2/vendor/doctrine-dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php line 36

The mysql database settings are correctly inserted in the config/parameters.ini file.

And here's the Doctrine configuration in config.yml

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

And the entity (i made only one to test it)

<?php
// src/Acme/NewsBundle/Entity/Article.php
namespace Acme\NewsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="articles")
 */
class Article
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
protected $id;

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

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

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

/**
 * @ORM\Column(type="date")
 */
protected $date;
}
?>
LBridge
  • 2,135
  • 5
  • 21
  • 32

12 Answers12

167

Just today I had a similar situation (but in other context, I was trying to create entities from db).

I solved it simply modifying the database_host from "localhost" to "127.0.0.1" in the parameters.ini file.

I think my Mysql instance is running only via TCP and not socket and because this when use database_host="localhost" it fails.

starball
  • 20,030
  • 7
  • 43
  • 238
acanimal
  • 4,800
  • 3
  • 32
  • 41
  • 2
    Beware, in the way of MySQL connects, we realized at work that there is a difference between the connection string host "localhost" and 127.0.0.1. With SSH proxy, we have to prevent writing localhost. Hope it's useful. – renoirb May 03 '12 at 17:18
  • 7
    @neil: Thx. I had this problem on my mac. 127.0.0.1 safed my day ;) – ownking Nov 16 '12 at 22:24
  • 4
    I also had to explicitly define the MySQL port to get this to work. My MAMP runs on port 8889, so my database_host ended up looking like: `127.0.0.1:8889`. Hope this helps someone else. – Mike Jul 01 '14 at 19:05
19

Try to add this line

unix_socket: /tmp/mysql.sock

into your config.yml file > doctrine > dbal just after the password line.

Saperlipopette
  • 424
  • 2
  • 5
  • 12
11

I changed 'host'=> 'localhost' to 'host'=> '127.0.0.1' on app/config/database.php and everything is working correctly

victormfv
  • 111
  • 1
  • 4
5

I fixed it by following this small tutorial: http://andreys.info/blog/2007-11-07/configuring-terminal-to-work-with-mamp-mysql-on-leopard

[EDIT]: I modified the right php.ini and everything's working fine now.

Now I get the following error:

[Exception]                                                                                            
 DateTime::__construct(): It is not safe to rely on the system's timezone settings.
 You are *required* to use the date.timezone setting or the date_default_timezone_set() function.
 In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.
 We selected 'Europe/Berlin' for 'CEST/2.0/DST' instead 

Here's the date.timezone config in php.ini

date.timezone = "Europe/Paris"

I'll try to figure it out myself but if any of you know how to fix it don't hesitate to comment on this. Thanks!

LBridge
  • 2,135
  • 5
  • 21
  • 32
  • I typed php -ini in the terminal and the setting date.timezone has no value. I'll look for the file path, see what php.ini file is used by the console. – LBridge Jun 07 '11 at 17:17
2

The problem for me was the mysql.default_socket setting in PHP.INI defaults to a different location than where MySQL actually puts that file.

Instead of editing the config files, I created an alias in the location that PHP is looking that connects to the real mysql.sock.

Just run these two commands (no restart needed):

mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock
Justin
  • 26,443
  • 16
  • 111
  • 128
0

Sometimes you may got several solution for location path of mysql.lock. But I think the best solution is just see the location path of mysql using php phpinfo() function.

Note: if you are not familar with phpinfo(), then just create a file give any name and include content of that file like this

<?php
phpinfo();
?>

run this file and get the path of mysql.lock file. I think it will help.

Md Mehedi Hasan
  • 1,733
  • 1
  • 21
  • 34
0

Worth checking to see if Mysql is running too.

Isaac
  • 1,505
  • 15
  • 8
0

Too late but hope it can help someone as well.

I am using Vagrant

Just change localhost to 127.0.0.1 AND change the port to 3333

worked perfectly.

maxshuty
  • 9,708
  • 13
  • 64
  • 77
0

I think just restart the mysql, and apache, that's work for me.

0

Restarting mysql worked for me:

sudo /etc/init.d/mysql restart

Geoffrey Hale
  • 10,597
  • 5
  • 44
  • 45
0

Had the same 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

aina
  • 111
  • 1
  • 4
0

Laravel .env add DB_SOCKET with path to mysql.sock

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root
DB_PASSWORD=mypassword

DB_SOCKET=/Users/username/Library/Containers/MAMP/mysql/tmp/mysql.sock

or add unix_socket in PDO

PDO('mysql:host=localhost;port=3306;unix_socket=/Users/username/Library/Containers/MAMP/mysql/tmp/mysql.sock', root, mypassword);