connecteev
1 month ago
540
18
Laravel

Error running Cron Commands to Connect with MySQL8 database?

Posted 1 month ago by connecteev

I am seeing a rather strange issue when running a Laravel Command. I am trying to do a simple insert into MySQL 8 with a cron command.

In app/Console/Kernel.php:

<?php

namespace App\Console;

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

use Illuminate\Support\Facades\DB;
use Faker\Generator as Faker;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function (Faker $faker) {
            DB::table('users')->insert(
                [
                    'email' => $faker->unique()->safeEmail,
                    'name' => $faker->username,
                    'password' => $faker->password,
                ]
            );
        })->everyMinute();
    }

If I execute php artisan schedule:run from the command line, everything works beautifully, and a new entry gets added to the users table.

However, my cron job (which is configured correctly and is able to run other commands) errors out:

Running scheduled command: Closure

   Illuminate\Database\QueryException  : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: insert into `users` (`email`, `name`, `password`) values (nolan.maxi$

  at /Users/kunalpunjabi/Code/EXPLORATIONS/EMAILS_QUEUES_CACHING_REDIS/connecteev_laravel_email_campaigns_and_tracking_example/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
    661|         // If an exception occurs when attempting to run a query, we'll format the error
    662|         // message to include the bindings with SQL, which will make this exception a
    663|         // lot more helpful to the developer instead of just the database's errors.
    664|         catch (Exception $e) {
  > 665|             throw new QueryException(
    666|                 $query, $this->prepareBindings($bindings), $e
    667|             );
    668|         }
    669|

  Exception trace:

  1   App\Console\Kernel::App\Console\{closure}(Object(Faker\Generator))
      [internal]:0

  2   App\Console\Kernel::App\Console\{closure}(Object(Faker\Generator))
      [internal]:0

  Please use the argument -v to see more details.

Note: As per this recommendation, I already tried this:

        // On MySQL 8.0, may need to run: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'; FLUSH PRIVILEGES; // replace 'root' with MySQL password

But it did not help. I am seeing the same error.

Any advice on what the problem might be?

Please sign in or create an account to participate in this conversation.