关于SQL Error: 1251 的解决方案
关于SQL Error: 1251 的解决方案
今天在做项目的时候遇到了SQL Error: 1251 Client does not support authentication protocol requested by server; consider upgrading MySQL client 错误
网页先报了一个Servlet错误
Exception
org.apache.jasper.JasperException: 在 [12] 行处理 [/index.jsp] 时发生异常
9: </head>
10:
11: <body>
12: <jsp:forward page="MessageServlet?method=view"></jsp:forward>
13: </body>
14: </html>
一开始认为是过滤器配置问题,在检查了过滤器并更改了doFilter代码后查看控制台输出
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (encoding != null) {
request.setCharacterEncoding(encoding);
response.setContentType("text/html; charset=" + encoding);
System.out.println("过滤器运行中......");
}else {
request.setCharacterEncoding("GBK");
response.setContentType("text/html; charset=GBK");
}
chain.doFilter(request, response);
}
过滤器正在运行
过滤器正在运行
15:49:51,023 WARN JDBCExceptionReporter:71 - SQL Error: 1251, SQLState: 08004
15:49:51,025 ERROR JDBCExceptionReporter:72 - Client does not support authentication protocol requested by server; consider upgrading MySQL client
org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at com.lyq.dao.MessageDao.findPaging(MessageDao.java:114)
at com.lyq.service.MessageServlet.doPost(MessageServlet.java:75)
at com.lyq.service.MessageServlet.doGet(MessageServlet.java:28)
查询后得知是这个项目的mysql驱动太老了,8.0后mysql的加密方式就使用了sha2转native
- 打开mac的系统偏好设置
- 找到MySql的图标点击Stop MySql Server
- 选择InitalizeDatabase ,这时候我们选择下面的Use Lagacy Password Encryption,换成老的加密方式并填写密码
- 接下来我们重新创建数据库和表数据
- 重新运行,发现控制台出现了 Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
- 解决思路:根据提示强制更改characterEncoding属性,然后更改数据库连接参数,如下代码
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--显示sql语句-->
<property name="hibernate.show_sql">true</property>
<!--格式化sql-->
<property name="hibernate.format_sql">true</property>
<!-- 自动建表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 数据库连接 -->
<property name="connection.url">jdbc:mysql://localhost:3306/db_database16?characterEncoding=gbk</property>
<!-- 数据库连接用户名 -->
<property name="connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="connection.password">Cc105481</property>
<!-- 数据库驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 映射文件 -->
<mapping resource="com/lyq/model/Message.hbm.xml"/>
<mapping resource="com/lyq/model/Revert.hbm.xml"/>
<mapping resource="com/lyq/model/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
至此跑通了这个servlet项目.
Keep Clam and Carry Keen.