18

I am new to Symfony and started doing some tutorials in order to get some things done. I am working on a Linux Mint 18 machine and a standard Symfony3-project.

I'd like to create a simple database to play around with it, but I am encountering an error I can't find the right solution for.

My parameters.yml file looks like this:

parameters:
   database_host: localhost
   database_port: null
   database_name: symfony
   database_user: root
   database_password: null
   mailer_transport: smtp
   mailer_host: 127.0.0.1
   mailer_user: null
   mailer_password: null
   secret: (not relevant)

My Genus.php file looks like this:

namespace AppBundle\Genus;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="genus")
 */
class Genus
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;
    /**
     * @ORM\Column(type="string")
     */
    private $name;

}

When I execute the command dpkg -l php*mysql I get the following result:

dpkg -l php*mysql
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name              Version       Architecture  Description
+++-=================-=============-=============-============================
un  php-pdo-mysql     <none>        <none>        (no description available)
ii  php7.0-mysql      7.0.8-0ubuntu amd64         MySQL module for PHP
un  php7.0-pdo-mysql  <none>        <none>        (no description available)

When I try to create a local database using the command prompt using

XXX@XXX-pc ~/PhpstormProjects/test_symfony $ php bin/console doctrine:database:create

This is what I get:

[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory
[Doctrine\DBAL\Driver\PDOException] SQLSTATE[HY000] [2002] No such file or directory
[PDOException] SQLSTATE[HY000] [2002] No such file or directory

I've came across many other topics but wasn't able to find the right answer. Could anyone tell me what I am missing here? Any help is much appreciated :)

Yash Sharma
  • 55
  • 2
  • 11
M. Douglas
  • 347
  • 1
  • 4
  • 17

9 Answers9

77

In my case, I only changed localhost->127.0.0.1 and it worked!

Mari Y
  • 918
  • 6
  • 8
8

May be It's too late but I had the same issue "SQLSTATE[HY000] [2002] No such file or directory" on my symfony 3.2.9 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 someone

aina
  • 111
  • 1
  • 4
  • No such file or directory error due to looking for **sock** file and it fixed by adding the `unix_socket: /var/lib/mysql/mysql.sock` into **doctrine** configuration. If you want to use sock file from MAMP it will be like this `/Applications/MAMP/tmp/mysql/mysql.sock` – sheerazabbas Feb 07 '23 at 06:04
  • You ABSOLUTELY must read this : https://www.php.net/manual/en/ref.pdo-mysql.connection.php#refsect1-ref.pdo-mysql.connection-notes – MatteoOreficeIT May 12 '23 at 17:02
  • For docker addicted also read this to map socket outside https://superuser.com/questions/1411402/how-to-expose-linux-socket-file-from-docker-container-mysql-mariadb-etc-to#saves-btn-1411424 or change to 127.0.0.1 – MatteoOreficeIT May 12 '23 at 17:04
4

if you use mamp test this:

sudo ln -s /Applications/MAMP/tmp/mysql /var/mysql
Ahmed Msafri
  • 81
  • 1
  • 3
2

Had the same error.

OS: macOS High Sierra. MAMP 5.0.1

The problem was that the default MySQL port on MAMP was 8889 and in parameters.yml it was 3306

1

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

This error occurs generally when your web application is not connected to the database. As per your parameter.yml it's searching for mysql server & wasn't getting connected because at that time mysql wasn't installed there at your machine.

Yash Sharma
  • 55
  • 2
  • 11
1

You don't have MySQL installed, Install MySQL first

1

Sometimes this happens because your mySql is stopped for some reason.

Check it:

service mysql status

And if it is inactive, you just need to start it:

service mysql start
Milad Safaei
  • 492
  • 1
  • 5
  • 16
0

My knowledge of MySQL suggests one variant which might happen and explain this:

You had (old) mysqld running, then deleted out all files from datadir. Then probably new server wasn't able to start up Then you connected to that old mysqld, which is still running, but getting 'No such file or directory' error trying to access own system files (which were deleted).

noonex
  • 1,975
  • 1
  • 16
  • 18
0

Symfony mysql connection

  1. Start your mamp, xamp or other alternative for my sql

  2. go to http://localhost:8888/phpMyAdmin/ and get these informations, bd_name : can be root bd_pass : can be root or nothing host : can be localhost port : attention the default port in the URL is 8888 but it's not the port that you should use in your .env file, check at the top of your admin phpmyAdmin page for Server:localhost:the_port_is_here. So it can be 8889

  3. write your Server version it can be 5.7.26

  4. Go back to your .env file on your symfony project and change the line DATABASE_URL= ex : DATABASE_URL="mysql://root:root@127.0.0.1:8889/mybddname?serverVersion=5.7"

  5. Open the integrated terminal of your project and write if it the first time

    php bin/console doctrine:database:create

if you wan to drop the data base

php bin/console doctrine:database:drop --force

PS : I'm using nmap on Mac OS for symfony project

Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97