Symfony3 - SQLSTATE[HY000] [2002] No such file or directory

Symfony3 - SQLSTATE[HY000] [2002] No such file or directory

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding the "No such file or directory" Error in Symfony3 Doctrine

When you run php bin/console doctrine:database:create on a Linux machine and hit a wall with SQLSTATE[HY000] [2002] No such file or directory, the error message is misleading. It suggests a missing path, but in the context of PHP connecting to MySQL, this almost always indicates that the TCP/IP socket connection failed before it could even attempt to resolve the database name.

The root cause usually lies in how the default parameters.yml configuration interacts with your operating system's networking stack and the specific state of your MySQL installation. In many Linux distributions, including Ubuntu variants like Mint 18, the default PHP-FPM or CLI setup defaults to connecting via a Unix socket file rather than a TCP port when database_port is set to null. However, Doctrine DBAL interprets null differently depending on the underlying driver configuration.

If you inspect your parameters.yml, you will see database_host: localhost and database_port: null. When Doctrine attempts to connect using these settings, it often tries to initiate a TCP connection to port 3306. If MySQL is not configured to listen on all network interfaces (bind-address = 0.0.0.0) or if the PHP socket configuration is mismatched, the system returns the "No such file or directory" error because the expected socket file for that specific host/port combination cannot be found in the standard /var/run/mysqld or /tmp directory.

The dpkg -l php*mysql output you provided reveals a critical piece of the puzzle: php7.0-mysql is installed, but php-pdo-mysql is marked as unavailable (un). While php7.0-mysql provides the driver for MySQL, older PHP versions or specific package managers might separate the PDO layer. You need to ensure that the pdo_mysql extension is actually loaded in your php.ini. You can verify this by running:

php -m | grep pdo

If pdo_mysql does not appear in the list, you must enable it either by adding a line to your php.ini or ensuring the appropriate package is installed. On Debian/Ubuntu systems, this is often handled by installing the php7.0-pdo-mysql package, which was marked as desired but not installed in your logs.

Once the extension is loaded and the configuration aligns with how MySQL is listening, you may still face socket vs. TCP issues. If you want to force a TCP connection explicitly in your parameters, change database_host to 127.0.0.1 instead of localhost and ensure database_port remains 3306 (or remove the null value depending on your driver version). Conversely, if MySQL is configured strictly for local Unix sockets, you might need to point Doctrine directly to the socket file path by setting database_host to /var/run/mysqld/mysqld.sock.

Another common pitfall involves the database user permissions. You are currently using root with a null password. While this works in development, MySQL often restricts root access from non-localhost connections or requires specific socket authentication methods. It is best practice to create a dedicated Symfony user:

CREATE DATABASE symfony CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL PRIVILEGES ON symfony.* TO 'symfony'@'localhost' IDENTIFIED BY 'secure_password';
FLUSH PRIVILEGES;

Update your parameters.yml with this new user and password. This avoids the complexity of root socket authentication and ensures that Doctrine can connect regardless of whether it uses TCP or a Unix socket.

If you are transitioning from tutorials that assume a specific environment to a fresh Linux install, remember that "localhost" in PHP does not always resolve to the loopback interface for MySQL connections unless explicitly configured. The error is a symptom of the driver failing to locate the communication endpoint, not a missing directory on your filesystem. By aligning the host/port strategy with your MySQL server configuration and ensuring the necessary PHP extensions are active, you can bypass this exception entirely.

Our network