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

Filth's avatar

Registered Command cant connect to mysql

Im racking my brain here. I have registered a command called sendbookings;

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Booking;
use Mail;
use DB;

class sendBookings extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sendBookings';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Email daily bookings';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $data['customerBooked'] = Booking::where('updated_at', '>=', DB::raw('(NOW() - INTERVAL 1 Day)'))
            ->where('type', 'Customer Booked')
            ->where('status', 'Complete')->get();
        $data['modified'] = Booking::where('status', '!=' , 'Available')
        ->where('updated_at', '>=', DB::raw('(NOW() - INTERVAL 1 Day)'))->get();

        Mail::send('emails.bookingsEmail', $data, function ($message) {
                $message->to('emailad', 'Website');
                $message->replyTo('replyto');
                $message->from('noreply@mg.com');
                $message->subject('Last 24 Hours Bookings');
            });
    }
}

I registered my command in the kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\Inspire::class,
        \App\Console\Commands\sendBookings::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('inspire')
                 ->hourly();
    }
}

When i run the command in temrinal I recieve the error: [PDOException]
SQLSTATE[HY000] [2002] No such file or directory

Why would the command not be able to connect to mysql?

0 likes
5 replies
Qlic's avatar

In my project i have the kernel configured like this, perhaps that will work for you.

protected $commands = [
        Commands\SynchronizeData::class
    ];

 protected function schedule(Schedule $schedule)
    {
        $schedule->command('data:sync')->dailyAt('00:00');
    }
Snapey's avatar

Do you have a non-standard port for mysql?

Do migrations work?

In your DB configuration, try changing 127.0.0.1 to 'localhost' .

Filth's avatar

@snapey it must be something like that. I pushed my changes to live server and the command works fine there. so must be something to do with my local MAMP enviroment

Snapey's avatar

I use Mamp and have issues if I don't set it up appropriately.

if database server is at 'localhost' then the PDO drivers try to use Unix Sockets

if the server is '127.0.0.1' then it uses TCPIP

You can add a port number to the DB config. For Mamp (and I can't remember if this is the default) but the port number is 8889

This works for me with MAMP

.env

DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=myproject
DB_USERNAME=root
DB_PASSWORD=root

config\database.php

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'port'      => env('DB_PORT', '3306'),   
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

Please or to participate in this conversation.