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

pietertje's avatar

PDO connection refused with laravel, but not with $con = new PDO()

I've configured mysql for my laravel 5.0-dev project like so:

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST') ?: 'localhost',
    'database'  => env('DB_DATABASE') ?: 'homestead',
    'username'  => env('DB_USERNAME') ?: 'homestead',
    'password'  => env('DB_PASSWORD') ?: 'secret',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

However, I get connection refused ('PDOException' with message 'SQLSTATE[HY000] [2002]') whenever I try to interact with the database using either php artisan, or the laravel app itself.

The weird thing is, when I run the code below, the connection doesn't get refused.

<?php
$dsn = 'mysql:dbname=homestead;host=localhost';
$user = 'homestead';
$password = 'secret';

try
{
    $dbh = new PDO($dsn, $user, $password);
}
catch (PDOException $e)
{
    echo 'Connection failed: ' . $e->getMessage();
}

I've been googling for 3 hours now and I haven't found a single solution which actually works. So any help is greatly appreciated.

0 likes
17 replies
pietertje's avatar

I've just run composer update on my project and the problem still persists.

My .env file:

FOO=bar
BAR=baz
SPACED=with spaces

NULL=
bestmomo's avatar

Set your .env file :

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
bashy's avatar

Note - It depends where you run the connection from and if you're using a VM you can't use localhost.

pietertje's avatar

@bestmomo I've changed my .env, but that also didn't work. I've tried both localhost as well as 127.0.0.1. What is this .env file supposed to do?

@codeatbusiness Errr, config file? do you mean homestead.yaml? if so, it's set to local.

@bashy Why can't you use localhost with a VM? For the record, I've tried 127.0.0.1 as well as localhost multiple times.

xingfucoder's avatar

Hi @pietertje, within Laravel 5 you can find the config folder. Within this folder you can find:

- database.php

Before the 5.0 version of Laravel you could select local or production environment.

How did you put in your homestead.yaml file the database items?

pietertje's avatar

Ah, I understand now. I've configured database.php as mentioned in my first post in this thread. I've configured homestead.yaml like so:

---
ip: "192.168.10.10"
memory: 2048
cpus: 1

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/Dropbox/workspace
      to: /home/vagrant/Code

sites:
    - map: huisrekening.app
      to: /home/vagrant/Code/HuisrekeningV2/public
    - map: phpmyadmin.app
      to: /home/vagrant/Code/phpmyadmin

databases:
    - homestead

variables:
    - key: APP_ENV
      value: local
xingfucoder's avatar

Hi @pietertje,

May you need to connecto using PDO the port that the HOMESTEAD are using to connect to MySQL. Try adding to your mysql config array the following line:

'mysql'            => [
'driver'            => 'mysql',
'host'               => env('DB_HOST') ?: 'localhost',
'database'      => env('DB_DATABASE') ?: 'homestead',
'username'     => env('DB_USERNAME') ?: 'homestead',
'password'      => env('DB_PASSWORD') ?: 'secret',
'charset'          => 'utf8',
'collation'        => 'utf8_unicode_ci',
'prefix'            => '',
'strict'             => false,
'port'              => //here you need to put your mysql port
],

I have not tried it because I didn't need to use, but I think that could be the solution.

Hope it helps you.

alidbc's avatar
alidbc
Best Answer
Level 11

Adding the port should resolve your issue

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST') ?: 'localhost',
        'port'      => '33060',
        'database'  => env('DB_DATABASE') ?: 'forge',
        'username'  => env('DB_USERNAME') ?: 'forge',
        'password'  => env('DB_PASSWORD') ?: '',
2 likes
bashy's avatar

@pietertje

Why can't you use localhost with a VM? For the record, I've tried 127.0.0.1 as well as localhost multiple times.

Well, your local system and a VM will have different network interfaces, therefore you will need to specify it properly because localhost will go to the local interface of the current machine. 127.0.0.1 is normally where localhost is pointed to anyway.

rleger's avatar

Yes but it will break on production you might want to have the port set on the .env file

DB_PORT=33060

with

'port' =>  env('DB_PORT')

on the config file

actually you might want to default to mysql standard port

'port' =>  env('DB_PORT', 3306)
beznez's avatar

I had a similar issue. I added the port but that didn't resolve my issue. I had to edit my my.cnf file to make sure my configurations were set correctly and then I also had to add this line to my mysql array in config/database.php and my migration completed properly after that.

'unix_socket' => '/path/to/my/socket/mysqld.sock',
1 like
katerwall's avatar

This is a simple fix. Your mysql database has lost its connection to the server. If you are running a local server run this in your terminal:

mysqld

That will reconnect your database. Then (if you are using homebrew) run:

brew services start mysql

This is automatically connect your database on login.

katerwall's avatar

This is a simple fix. Your mysql database has lost its connection to the server. If you are running a local server run this in your terminal:

mysqld

That will reconnect your database. Then (if you are using homebrew) run:

brew services start mysql

This is automatically connect your database on login.

Please or to participate in this conversation.