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

Hello, i have an issue when i try to deploy my app with the managed database on digitalocean (MYSQL 8), i have error at build time, after composer install:

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

it’s a symfony application with doctrine.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there

The error SQLSTATE[HY000] [2002] No such file or directory often occurs when your PHP application is trying to connect to a MySQL server via a Unix socket, but the socket file doesn’t exist at the specified location. When deploying to a DigitalOcean managed MySQL database, you will connect over the network (typically using a hostname and port), not a Unix socket.

When deploying applications, especially on platforms that use distinct build and deploy stages, it’s crucial to understand the appropriate time to establish database connections or perform database-related operations.

During the build stage, the primary goal is to compile, package, or prepare the application for deployment. Typically, this involves tasks like:

  • Installing dependencies (e.g., composer install for PHP projects)
  • Compiling source code, if applicable
  • Minifying or bundling assets

Connecting to a database during the build stage is not recommended, as:

  • The build environment might not have access to your production or staging databases for security reasons.
  • You could inadvertently modify your database during the build process.
  • The build stage should remain environment-agnostic. This means it should be identical whether you’re deploying to a staging, production, or any other environment.

During the deploy stage, the application is actually being launched and made ready for user traffic. This is the right time to:

  • Establish connections to databases
  • Run database migrations or schema updates
  • Perform any final configurations that are environment-specific

Given that you’re facing the SQLSTATE[HY000] [2002] No such file or directory error during the build stage after composer install, it seems like there might be a post-install script or some logic that’s attempting to connect to the database.

  1. Check composer.json: Look in the scripts section of your composer.json file. Ensure there aren’t any post-install or post-update scripts that are trying to connect to the database or run Symfony commands that require a database connection.

  2. Move Database-related Operations: If there are any, consider moving them to a deployment script or another mechanism that runs during the deploy stage. Remember, the build stage should only prepare the code and assets, not interact with external services.

  3. Environment Variables: Make sure your application knows when it’s in build mode vs. deploy mode. This way, even if there’s code that tries to connect to the database, you can prevent it from executing during the build stage.

Let me know how it goes!



Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel