Fix Laravel issue: The server requested authentication method unknown to the client

0
5900
Fix Laravel issue: The server requested authentication method unknown to the client
Fix Laravel issue: The server requested authentication method unknown to the client

I will show you how to fix Laravel issue The server requested authentication method unknown to the client in this post. Don’t be panic if you face it!

The error might look similar to this when you enable debug mode in Laravel config:

Laravel issue: The server requested authentication method unknown to the client
Laravel issue: The server requested authentication method unknown to the client

The cause of this is from MySQL v8.0+, it changes the default method to authentication to database. Therefore, you need to get your hand to MySQL server to fix it.

Step 1: Update MySQL user

First, you need to edit database user, which is specified in the Laravel .env file, to use native password by executing following SQL query:

ALTER USER 'DB_USER'@'localhost' IDENTIFIED WITH mysql_native_password BY 'DB_PASS';

Replace DB_USER and DB_PASS with your MySQL username and password. For example:

ALTER USER 'petehouston'@'localhost' IDENTIFIED WITH mysql_native_password BY 'sEcReT123%';

Make sure to flush afterward:

FLUSH PRIVILEGES;

Try Laravel application, if the error persists, then move to next step.

Step 2: Update MySQL server config

In this second step, we will change MySQL server configuration to use native password as default method for authentication. On Linux, you will need to edit this file /etc/mysql/mysql.conf.d/mysqld.cnf, find the [mysqld] section and add following line:

[mysqld]
default_authentication_plugin = mysql_native_password

Then restart MySQL server, something like this, if you’re under Debian/Ubuntu:

$ sudo service mysql restart

After restarting MySQL service, if you still see the error, let’s move on to the next step.

Step 3: Edit Laravel DB connection modes

At this step, we will add connection modes for Laravel database config, which is config/database.php:

'connections' => [
        'mysql' => [
            'driver'      => 'mysql',
            'host'        => env( 'DB_HOST', '127.0.0.1' ),
            'port'        => env( 'DB_PORT', '3306' ),
            'database'    => env( 'DB_DATABASE', 'forge' ),
            'username'    => env( 'DB_USERNAME', 'forge' ),
            'password'    => env( 'DB_PASSWORD', '' ),
            'unix_socket' => env( 'DB_SOCKET', '' ),
            'charset'     => 'utf8mb4',
            'collation'   => 'utf8mb4_unicode_ci',
            'prefix'      => '',
            'strict'      => true,
            'engine'      => null,
            // add this option
            'modes'       => [
                'ONLY_FULL_GROUP_BY',
                'STRICT_TRANS_TABLES',
                'NO_ZERO_IN_DATE',
                'NO_ZERO_DATE',
                'ERROR_FOR_DIVISION_BY_ZERO',
                'NO_ENGINE_SUBSTITUTION',
            ],
        ],
    ],

Clear Laravel cache, and you should not see the error anymore.

That’s how you fix the Laravel issue The server requested authentication method unknown to the client“.