Table of contents

mysql 8 Error Server sent charset unknown to the client

Mysql Jul 23, 2020 Viewed 27 Comments 0

Issue

After install mysql 8. I'm trying to connect to a MySQL database from php. 

$dsn = "mysql:dbname=test;host=localhost;charset=utf8";
$user = "root";
$password = "root";
try {
    $pdo = new PDO($dsn, $user, $password);
    $res = $dbh->query($sql);
} catch (PDOException $e) {
    echo $e->getMessage();
}

But when I put in username and password I get the error message saying:

Server sent charset unknown to the client. Please, report to the developers

Solution

MySQL 8 changed the default charset to utf8mb4. But some clients don't know this charset. Hence when the server reports its default charset to the client, and the client doesn't know what the server means, it throws this error.

Edit my.cnf, specify the client code, and add the following content.

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
collation-server = utf8_unicode_ci
character-set-server = utf8
Updated Jul 23, 2020