Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NorseMan's avatar

PDO problem - can't connect to DB

Hi😊 I am almost new here on Laracasts and thank you for the membership. I got a huge problem I can't find out about. But my PDO connection won't work after I wanted it to print out an error message if it fails to connect with the error code.

I got a problem with my new PDO connection. I wanted to change it because I want it to print to screen the error code and an error message if there is no contact with MySQL DB, and give a message if it's connected. But what has happened is like nothing. Or, the connection script won't connect. It writes out the message on the screen.

I have used some hours trying to fix the problem but without luck. So, I ask you "gurus" to be so kind and look over it for me and fix the problem for me? I can't manage to fix it myself. Please😊

By the way. This is the error message i am receiving: Unable to connect to the database server: SQLSTATE[HY000] [2002] Connection refused

I have checked my user data like password, username, address to the host and DB name several times, and over and over again. So there is something in the script itself, but I can't find out what or where.

0 likes
4 replies
NorseMan's avatar

@jlrdw There should be my PDO code here too, but I don't understand why it isn't posted.

jlrdw's avatar

@NorseMan if you run a:

phpinfo();

Does it show your PDO driver enabled? And can you connect to MySql from the command line or another front end like phpmyadmin?

Snapey's avatar

Just use the connection provided by Laravel. You don't need to worry about PDO settings.

Configure the settings in your .env file is the most common setup.

Please or to participate in this conversation.