Question

SQLSTATE[HY000] [2002] No such file or directory - My Database is not connected

I followed all the steps to connect my database to my Laravel Application, including connecting to mysql using the terminal and using the connection parameters. I am able to see all of the database tables in the terminal. But, when I try to log into my application on the web, or run php artisan migrate, I get the following message: SQLSTATE[HY000] [2002] No such file or directory SELECT * FROM users WHERE email = info@example.com. I am assuming this is because my database is not connected.

Here are my .env values:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testing
DB_USERNAME=username
DB_PASSWORD=protected

My app-level environment variables:
APP_NAME: App
APP_KEY: Key values
DB_CONNECTION: mysql
DB_HOST: 127.0.0.1
DB_PORT: 25060
DB_DATABASE: defaultdb
DB_USERNAME: username
DB_PASSWORD: password
APP_DEBUG: true
SSL_MODE: REQUIRED

Any help would be appreciated. Thank you.


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.

alexdo
Site Moderator
Site Moderator badge
January 5, 2023

Hello @codeideas

You can try to connect to the database via terminal/shell with the mysql command. This will give you the opportunity to test if the login credentials are correct.

Regards

KFSys
Site Moderator
Site Moderator badge
January 5, 2023

Hi @codeideas,

Usually, the error is caused when you use DB_HOST=localhost instead of DB_HOST=127.0.0.1.

Additionally, restart you MySQL instance and try to enter using the credetianls of your application(not sure how did you logged in to MySQL before with root or with the laravel user).

Try DigitalOcean for free

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

Sign up

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

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

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
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
Get started for free

Enter your email to get $200 in credit for your first 60 days with DigitalOcean.

New accounts only. By submitting your email you agree to our Privacy Policy.

© 2023 DigitalOcean, LLC.