mysql訪問被拒問題


寫之前,先心平氣和地說一句:Mysql,NMSL

以下正文

有時,在用jdbc連接mysql的時候,會無緣無故出現下面的異常:
java.sql.SQLException: Access denied for user 'root'@'localhost' (using pass:YES)

首先,修改mysql密碼

嗯,就是修改密碼,也不到為啥,雖然咱密碼也沒填錯

進入數據庫,使用mysql這個數據庫(use mysql),輸入命令

update user set password=PASSWORD("新密碼") where user='root';

如果是mysql5.7,此時會報錯:
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
因為mysql5.7已經沒有password這個字段了,password字段改成了:authentication_string
所以mysql5.7修改密碼的命令為:

update user set authentication_string=password('新密碼') where user='root';

修改密碼之后,要重啟一下Mysql服務(services.msc)或者刷新一下,新密碼才會生效

注:以上是Mysql5.7修改密碼的方式,其他版本自行BD

增加jdbc配置

這個時候還沒完,重啟后會發生第二個錯誤:
:java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized...

此時,要修改jdbc的yml或properties配置文件:

spring:
  datasource:
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/myblog?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

就是在'url: jdbc:mysql://127.0.0.1:3306/myblog'的后面加上'?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC'

這個時候應該就mou問題了


免責聲明!

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



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