php PDO::__construct():服务器发送客户端未知的字符集 (255)。请向开发者报告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43437490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:50:11  来源:igfitidea点击:

PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers

phpmysqlpdo

提问by Napas

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

我正在尝试从 Symfony 3 应用程序连接到 MySQL 数据库。但是当尝试从 Symfony 控制台命令创建 MySQL 模式时,我收到此错误:PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers

Both PHP and MySQL are running in Docker containers.

PHP 和 MySQL 都在 Docker 容器中运行。

MySQL version: 8.0.1

MySQL版本: 8.0.1

PHP version: 7.1.3

PHP版本: 7.1.3

Driver: pdo_mysql

司机: pdo_mysql

charset: UTF8

字符集: UTF8

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

域名: "mysql:host=mysql;dbname=database;charset=UTF8;"

Any ideas?

有任何想法吗?

回答by Bill Karwin

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.

MySQL 8 将默认字符集更改为 utf8mb4。但是有些客户不知道这个字符集。因此,当服务器向客户端报告其默认字符集,而客户端不知道服务器的含义时,它会抛出此错误。

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

另见https://bugs.mysql.com/bug.php?id=71606

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

该错误针对 MySQL 连接器/C++,因此它影响的不仅仅是 PHP。

Okay—I got it to work by changing the character set to utf8, to be compatible with non-upgraded clients. I added this to /etc/my.cnf and restarted mysqld:

好的 - 我通过将字符集更改为 utf8 使其与未升级的客户端兼容。我将此添加到 /etc/my.cnf 并重新启动 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?

我在 2010 年的回答中找到了这些设置:在 my.cnf 中将 MySQL 默认字符集更改为 UTF-8?

回答by skwidbreth

The accepted answer saved me (thanks, Bill!!!), but I ran into another related issue, just wanted to provide some details on my experience -

接受的答案救了我(谢谢,比尔!!!),但我遇到了另一个相关问题,只是想提供一些关于我的经验的细节 -

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.cnffile, added the content in the accepted answer, then restarted the MySQL server. However, this produced a new error:

升级到 MySQL 8.0.11 后,我在使用 PHP 的mysqli_connect()功能时遇到了和 OP 一样的问题。在我的 MySQL 目录(在我的例子中usr/local/mysql)中,我创建了my.cnf文件,在接受的答案中添加了内容,然后重新启动了 MySQL 服务器。然而,这产生了一个新的错误:

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

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.cnfnow looked like:

我添加了 line default_authentication_plugin = mysql_native_password,所以my.cnf现在看起来像:

[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

其他参考:https: //github.com/laradock/laradock/issues/1392

回答by reyqueson

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

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

  • Not works on> C:\Program Files\MySQL\MySQL Server 5.5\my.ini
  • 不适用于> 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/

回答by Qayed

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).

我的情况是我正在使用 AWS 的 RDS(mysql db verion 8)并通过 EC2 连接我的应用程序(我的 php 代码 5.6 在 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

在这种情况下,由于是 RDS,因此没有 my.cnf,参数由 AWS 的 PARAMETER 组维护。参考: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.

  1. 创建一个新的参数组,然后对其进行编辑。

  2. 搜索所有字符集参数。默认情况下这些是空白的。单独编辑它们并从下拉列表中选择 utf8。

character_set_client, character_set_connection, character_set_database, character_set_server

character_set_client、character_set_connection、character_set_database、character_set_server

  1. SAVE
  1. 节省

And then most important, Rebooted RDS instance.

然后是最重要的,重启 RDS 实例。

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

这解决了我的问题,从 php5.6 到 mysql 8.x 的连接运行良好。

hope this helps.

希望这可以帮助。

Please view this image for better understanding. enter image description here

请查看此图像以更好地理解。 在此处输入图片说明

回答by jbrahy

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.

我也有这个问题。问题是因为我运行的是旧版本的 PHP (5.6),一旦我升级到 PHP 7.4,错误就消失了。

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

对我来说这是在 Amazon Linux 2 上,升级命令是:

amazon-linux-extras install php7.4

amazon-linux-extras 安装 php7.4

回答by npsingh

I was facing same issue so I have reinstall MySQL 8 with different Authentication Method "Use Legacy Authentication Method (Retain MySQL 5.x compatibility)" then work properly.

我遇到了同样的问题,所以我用不同的身份验证方法“使用传统身份验证方法(保留 MySQL 5.x 兼容性)”重新安装了 MySQL 8,然后正常工作。

Choose Second Method of Authentication while installing.

安装时选择第二种身份验证方法。

回答by CPP

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.

在测试 mysql 和关联的 php 系统的恢复备份时,我遇到了与此非常相似的问题。无论我在 mysql 上添加什么配置设置,它都没有任何区别。在对问题进行了一段时间的故障排除后,我在 mysql 日志中注意到 mysql 配置文件在很大程度上被 mysql 忽略了。原因是文件权限太开放了,这是因为我的 zip 备份和还原过程丢失了所有文件权限。我修改了我的备份和恢复脚本以使用 tar,然后一切正常。

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

总之,检查您的 mysql 配置文件的文件权限是否正确。希望这可以帮助某人。

回答by hardik chugh

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

我通过将以下代码添加到我的/etc/my.cnf文件中解决了这个问题-

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.

保存后,我进入我的系统设置并停止 MYSQL 服务器并再次启动,它工作正常。

回答by hardik chugh

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

[MySQLi]在您的部分中找到php.ini并添加 line mysqli.default_charset = "UTF-8"。我对部分[Pdo_mysql][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

问题似乎特别与 MySQL 8.0 版有关,并且发现上述解决方案适用于 PHP 7.2 版,并且该解决方案不适用于 PHP 7.0

回答by Shashikant Pandit

I had the same issue to connect to local system.

我在连接到本地系统时遇到了同样的问题。

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

我检查了服务列表(Run->Services.msc->Enter)wampmysqld64 被停止了。

Restarted it. and was able to login.

重新启动它。并且能够登录。