java.sql.SQLException:从连接器 8.0.11 的服务器收到未知的初始字符集索引“255”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50474156/
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-10 23:38:33  来源:igfitidea点击:

java.sql.SQLException: Unknown initial character set index '255' received from server for connector 8.0.11

javamysqljdbc

提问by sachin sarangamath

While establishing the connection to a MySQL database, I'm getting the following error

在建立与 MySQL 数据库的连接时,出现以下错误

java.sql.SQLException: Unknown initial character set index '255' received from 
server. Initial client character set can be forced via the 'characterEncoding' 
property.

Upon googling, I got to know that we need to modify 2 params in my.ini or my.cnf.

通过谷歌搜索,我知道我们需要修改 my.ini 或 my.cnf 中的 2 个参数。

I am using MySQL version 8.0.11 and it does not have this file.

我使用的是 MySQL 8.0.11 版,但它没有这个文件。

Hence I modified these parameters using the SQL commands:
Please note name and duration are column name in the table.

因此我使用 SQL 命令修改了这些参数:
请注意名称和持续时间是表中的列名。

ALTER TABLE courses MODIFY name VARCHAR(50) COLLATE utf8_unicode_ci;    
ALTER TABLE courses MODIFY duration VARCHAR(50) COLLATE utf8_unicode_ci;

ALTER TABLE courses MODIFY name VARCHAR(50) CHARACTER SET utf8;
ALTER TABLE courses MODIFY duration VARCHAR(50) CHARACTER SET utf8;

Hence my table looks like this

因此我的桌子看起来像这样

Table

桌子

After this, I restarted MySQL server, but I'm still getting the above error.

在此之后,我重新启动了 MySQL 服务器,但仍然出现上述错误。

Please note I'm deploying my application in tomcat and running rest API call which will connect to the database. While connecting to the database, I'm getting the above error.

请注意,我正在 tomcat 中部署我的应用程序并运行将连接到数据库的 rest API 调用。连接到数据库时,出现上述错误。

采纳答案by Knautiluz

Using MySQLWorkbench interface:

使用 MySQLWorkbench 接口:

First click in your connection ->

首先单击您的连接->

enter image description here

在此处输入图片说明

After click in Options File

单击选项文件后

enter image description here

在此处输入图片说明

And change character-set-server and collation-server values to the values below:

并将 character-set-server 和 collat​​ion-server 值更改为以下值:

enter image description here

在此处输入图片说明

Make sure to restart mysql server by opening services and restarting mysql server.

确保通过打开服务并重新启动 mysql 服务器来重新启动 mysql 服务器。

回答by Fredy Fischer

With version 8 the default characterset changed. When you add your character set to the connection URL like ?characterEncoding=latin1 it might work.

在第 8 版中,默认字符集发生了变化。当您将字符集添加到连接 URL 时,如 ?characterEncoding=latin1 可能会起作用。

回答by Avinash Khadsan

it is because of mysql-connector version , i,e. My-sql install version and dependency version did not match.

这是因为 mysql-connector 版本,即 My-sql 安装版本和依赖版本不匹配。

use same version for myS

对 myS 使用相同的版本

回答by Varghese George

Add connector dependency jar file in the POM file after a cross-check with your MySql server version 8.0.x add this dependency it was working for me mysqlmysql-connector-java 8.0.11

在与您的 MySql 服务器版本 8.0.x 进行交叉检查后,在 POM 文件中添加连接器依赖项 jar 文件添加此依赖项,它对我有用 mysql mysql-connector-java 8.0.11

回答by lalit kumar Mehta

Use the same version of MySQL as well as mysql-connector. If you are using tomcat then keep the connector in the lib folder of tomcat.

使用相同版本的 MySQL 以及 mysql-connector。如果您使用的是 tomcat,请将连接器保存在 tomcat 的 lib 文件夹中。

回答by Manish Patel

You can add ?characterEncoding=latin1"after your url String like this for e.g.:

您可以?characterEncoding=latin1"像这样在您的 url 字符串之后添加,例如:

"jdbc:mysql://localhost/yourDB?characterEncoding=latin1";

It might work.

它可能会起作用。

回答by ankit

I am using springboot,liquibase, mysql

我正在使用springboot, liquibase,mysql

I got solution: This error occur just because of mysql connector in dependency and mysql server you install not match(in my case i am using lower version mysql connector and latest mysql server). So just change scope of mysql connector to runtime:

我得到了解决方案:发生此错误只是因为依赖关系中的 mysql 连接器和您安装的 mysql 服务器不匹配(在我的情况下,我使用的是较低版本的 mysql 连接器和最新的 mysql 服务器)。因此,只需将 mysql 连接器的范围更改为runtime

maven

行家

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

gradle

等级

dependencies {
//... other dependencies
runtimeOnly group:'mysql', name:'mysql-connector-java'
}

Or if problem yet not solve then use same version or nearby version for both mysql server and mysql connector.

或者,如果问题尚未解决,则对 mysql 服务器和 mysql 连接器使用相同版本或附近版本。

回答by Manali Bundele

Adding the following to your connection string should work:

将以下内容添加到您的连接字符串应该可以工作:

jdbc:mysql://localhost/yourDB?characterEncoding=latin1;

回答by Anand Sonwane

Just download latest version of hibernate jar file from
https://www.javatpoint.com/src/hb/hibernatejar.zip
and the MySQL connector jar file from
https://dev.mysql.com/downloads/connector/j/5.1.html

只要下载从最新版本的Hibernate jar文件的
https://www.javatpoint.com/src/hb/hibernatejar.zip
从和MySQL连接器jar文件
https://dev.mysql.com/downloads/connector/j/5.1 .html