70

I'm trying to connect to a MySQL database from Symfony 3 application. But when trying to create MySQL schema from a Symfony console command I get this error: PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers

Both PHP and MySQL are running in Docker containers.

MySQL version: 8.0.1

PHP version: 7.1.3

Driver: pdo_mysql

charset: UTF8

dsn: "mysql:host=mysql;dbname=database;charset=UTF8;"

Any ideas?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Napas
  • 2,692
  • 3
  • 28
  • 33

12 Answers12

143

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.

See also https://bugs.mysql.com/bug.php?id=71606

That bug is against the MySQL Connector/C++ so it's affecting more than just PHP.

The correct solution is to upgrade your client, but in the meantime I got it to work by changing the server's character set to utf8, to be compatible with non-upgraded clients. I added this to /etc/my.cnf and restarted mysqld:

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8


[mysqld]
collation-server = utf8_unicode_ci
character-set-server = utf8

I found these settings in an answer from 2010: Change MySQL default character set to UTF-8 in my.cnf?

miken32
  • 42,008
  • 16
  • 111
  • 154
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • It worked in my environment. MySQL 8.0.1, PHP 7.1.4. Did you restart the mysql service? – Bill Karwin Apr 18 '17 at 20:47
  • 1
    FYI: https://bugs.php.net/bug.php?id=74461 See the comment from nikic, apparently this has been fixed in PHP source, and I assume it will be released in the near future. – Bill Karwin Apr 18 '17 at 20:47
  • 1
    Stop mysql server if running, do as stated in the answer and start mysql server back. This way it worked for me. – Lahar Shah Oct 25 '18 at 15:08
  • On windows the file mysqlrouter.conf.sample was here C:\Program Files\MySQL\MySQL Server 8.0\etc. I added the above lines and removed the .sample. I then restarted the service (Control Panel -> Administrative Tools -> Services). – Guy Lowe Jun 09 '19 at 12:09
  • 1
    On windows the file is here C:\ProgramData\MySQL\MySQL Server 8.0\my.ini – Guy Lowe Jun 13 '19 at 00:50
55

After upgrading to MySQL 8.0.11, I experienced the same problem as the OP when using PHP's mysqli_connect() function. In my MySQL directory (in my case, usr/local/mysql), I created the my.cnf file, added the content in the accepted answer, then restarted the MySQL server. However, this produced a new error:

mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password]

I added the line default_authentication_plugin = mysql_native_password, so my.cnf now looked like:

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
collation-server = utf8_unicode_ci
character-set-server = utf8
default_authentication_plugin = mysql_native_password

and I was good to go!

For additional reference: https://github.com/laradock/laradock/issues/1392

Note the comment from ARA1307:

"Note you should create new users specifying "IDENTIFIED WITH mysql_native_password" explicitly, and alter existing users, e.g.: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';"

Dharman
  • 30,962
  • 25
  • 85
  • 135
skwidbreth
  • 7,888
  • 11
  • 58
  • 105
  • 6
    Note you should create new users specifying "IDENTIFIED WITH mysql_native_password" explicitly, and alter existing users, e.g.: `ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';` – ARA1307 Aug 29 '18 at 05:19
  • 1
    Of course use your password in: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourrootpassword'; And beware: the change in authentication method in my.cnf will revert to the old password hashing system and is strongly discouraged – Stefan Verhagen Oct 14 '19 at 21:42
  • 1
    did finally work after I issued: ALTER USER root@'%' IDENTIFIED WITH mysql_native_password BY 'secretpassword'; – Stefan Verhagen Oct 14 '19 at 21:48
  • Could you add the information from @ARA1307 in the answer? – Master DJon Apr 27 '22 at 19:18
  • instead of adding it to my.cnf, you should add it to `/etc/mysql/conf.d/charset.cnf` – hanshenrik Mar 12 '23 at 13:04
26

My case was that I was using RDS (MySQL db verion 8) of AWS and was connecting my application through EC2 (my php code 5.6 was in EC2).

Here in this case since it is RDS there is no my.cnf the parameters are maintained by PARAMETER Group of AWS. Refer: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_WorkingWithParamGroups.html

So what I did was:

  1. Created a new Parameter group and then edited them.

  2. Searched all character-set parameters. These are blank by default. edit them individually and select utf8 from drop down list.

character_set_client, character_set_connection, character_set_database, character_set_server

  1. SAVE

And then most important, Rebooted RDS instance.

This has solved my problem and connection from php5.6 to mysql 8.x was working great.

Please view this image for better understanding.

enter image description here

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Qayed Johar
  • 361
  • 3
  • 4
  • this answer is precise for AWS RDS, I have exactly this issue and manage to resolve it. My legacy app runs on php 5.6 while coming change from RDS requires version 8.0. Thanks for clear details. – Long TRAN Feb 21 '23 at 01:12
  • yes, REBOOT is the most important. Even if you select "Apply immediately", you still need to reboot the instant. Take me quite a while to figure it out. Thank to this answer. – Daron Tancharoen Aug 22 '23 at 16:28
6

Works for me >

the environment:

localhost
Windows 10
PHP 5.6.31
MYSQL 8

set:

default-character-set=utf8

on:

c:\programdata\mysql\mysql server 8.0\my.ini

  • Not works on> C:\Program Files\MySQL\MySQL Server 5.5\my.ini

Tools that helps:

C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -u root -p

mysql> show variables like 'char%';

mysql> show variables like 'collation%';

\m/
Alicia
  • 1,152
  • 1
  • 23
  • 41
reyqueson
  • 459
  • 1
  • 7
  • 9
2

I had this problem also. The issue was because I was running an old version of PHP (5.6) and once I upgraded to PHP 7.4 the errors went away.

This was on Amazon Linux 2 for me and the command to upgrade was:

amazon-linux-extras install php7.4

Other flavors of *nix might have it in their repositories otherwise if you're on a flavor of Linux then Remi might have it for you here: https://rpms.remirepo.net/

jbrahy
  • 4,228
  • 1
  • 42
  • 54
2

In My Aws Windows I Solved it and steps are

  1. Open "C:\ProgramData\MySQL\MySQL Server 8.0"
  2. Open My.ini
  3. Find "[mysql]"
  4. After "no-beep=" Brake Line and insert "default-character-set=utf8"
  5. Find "[mysqld]"
  6. Insert "collation-server = utf8_unicode_ci"
  7. Insert "character-set-server = utf8"

example

...
# socket=MYSQL

port=3306

[mysql]
no-beep=
default-character-set=utf8
# default-character-set=

# SERVER SECTION
# ----------------------------------------------------------------------
# 
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this 
# file.=
# 
# server_type=2
[mysqld]

collation-server = utf8_unicode_ci
character-set-server = utf8
# The next three options are mutually exclusive to SERVER_PORT below.
# skip-networking=
# enable-named-pipe=
# shared-memory=
...

mysql screenshot

MRRaja
  • 1,073
  • 12
  • 25
1

I know this is an old question but this week we had to migrate an old PHP 5.3 + Apache web server tools to docker containers and been forced to used php:5.6-apache.

We couldn't change the MySQLs remote servers configs since we are not the owners.

We didn't have any issue with PHP 5.3 to connect on MySQL 8+ servers so I compared both configs and php_info() outputs and realized that PHP 5.4+ is now using 'mysqlnd' as a built-in default driver (native) to connect to MySQL databases using mysql or mysqli extensions. It seems that the 'mysqlnd' provided with PHP 5.6 was the problem.

The way we fixed it was by installing the mysql-client and default-libmysqlclient-dev packages in our images that is basically the MariaDb's driver that IS able to connect to MySQL 8+ and did configure PHP to use it instead of native mysqlnd one.

In the Dockerfile, we first installed:

RUN apt update -y \
&& DEBIAN_FRONTEND=noninteractive apt install -y \
mysql-client \
default-libmysqlclient-dev

Then with php:5.6-apache image's built-in docker-php-ext-* scripts:

RUN docker-php-ext-configure mysql --with-mysql=/usr/ \
&& docker-php-ext-install mysql \
&& docker-php-ext-configure mysqli --with-mysqli=/usr/bin/mysql_config \
&& docker-php-ext-install mysqli

I hope this will help the community.

Thanks.

0

I was facing the same issue so I had reinstalled MySQL 8 with different Authentication Method "Use Legacy Authentication Method (Retain MySQL 5.x compatibility)" and it worked properly.

I recommend, Choose Second Method of Authentication while installing.

Channa
  • 742
  • 17
  • 28
npsingh
  • 61
  • 1
  • 7
0

I had a very similar issue to this when testing a restored backup of a mysql and associated php system. No matter what configuration settings I added on mysql it was not making any difference. After troubleshooting the issue for some time I noticed in the mysql logs that the mysql config files were being largely ignored by mysql. The reason was due to the file permissions being too open which was due to the fact my zip backup and restore process was losing all file permissions. I modified my backup and restore scripts to use tar instead and then everything worked as it should.

In summary, check the file permissions on your mysql config files are correct. Hope this helps someone.

CPP
  • 1,031
  • 3
  • 10
  • 26
-1

find section [MySQLi] in your php.ini and add line mysqli.default_charset = "UTF-8". Similar changes my require for section [Pdo_mysql] and [mysqlnd].

Issue seems to be specifically with MySQL version 8.0, and above solution found working with PHP Version 7.2, and solution didn't work with PHP 7.0

-2

I had the same issue to connect to local system.

I checked in the services list (Run->Services.msc->Enter) wampmysqld64 was stopped.

Restarted it. and was able to login.

Shashikant Pandit
  • 2,752
  • 22
  • 29
-2

I solved this problem by adding following code into my /etc/my.cnf file -

enter image description here

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8


[mysqld]
collation-server = utf8_unicode_ci
character-set-server = utf8

After saving that,I went into my system settings and stopped MYSQL server and started again, and it worked.

hardik chugh
  • 1,082
  • 15
  • 12