關於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

  1. 打開mac的系統偏好設置
  2. 找到MySql的圖標點擊Stop MySql Server
  3. 選擇InitalizeDatabase ,這時候我們選擇下面的Use Lagacy Password Encryption,換成老的加密方式並填寫密碼
  4. 接下來我們重新創建數據庫和表數據
  5. 重新運行,發現控制台出現了 Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
  6. 解決思路:根據提示強制更改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項目.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM