JDBC Common Connection Problems

connection problem

Access denied for user ‘root’@‘localhost’ (using password: YES)

The first situation

Problem Description

环境:IDEA 2021.1 windows11 mysql8.0 
已知条件:
	1.已正常安装数据库
	2.无法通过CMD输入mysql -u root -p 密码进入数据库
	3.无法通过Navicat进入
	4.修改my.ini依然无法进入
提示:Access denied for user 'root'@'localhost' (using password: YES) 或者 Access denied for user 'root'@'localhost' (using password: NO)

solution

【解决Mysql:ERROR 1045 (28000):Access denied for user ‘root‘@‘localhost‘ (using password: NO)的方法】
--https://www.jb51.net/article/250474.html

The following command line code is run with administrator privileges:

Step 1: Close the Mysql service
net stop mysql
Step 2: Skip Mysql password verification

Note: mysql8.0 cannot directly add –skip-grant-tables in my.ini to skip password verification. You need to use the command line.

mysqld -console --skip-grant-tables --shared-memory

Please add image description

Step 3: Enter Mysql without password

After the above steps, open a cmd.exe running in administrator mode.

No need to net start mysqlopen the mysql service

Then enter

mysql -u root -p

Just press Enter to enter the mysql interface.

Please add image description

Step 4: Set the login password to empty
use mysql; (使用mysql数据表)
update user set authentication_string='' where user='root';(将密码置为空)
quit; (然后退出Mysql)
Step 5: Change your login password

1. Close the first two cmd windows (must be closed!);

2. Enter the code in the third window;

Then enter

cd D:\mysql-8.0.19-winx64\bin  (此处输入自己电脑上的安装目录)
mysql -u root -p
(此处会显示输入密码,直接回车就好了,第四步我们已经将他置为空了)
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';(By 后面跟的字符串就是你想要更新的密码)
Last step: Verify whether the password has been changed successfully
quit(退出mysql)
mysql -u root -p 
(输入新密码,再次登录)

Recording time: 2022-7-27-17:13 -- It seems to be an old problem that has not been corrected before. I happened to correct it during review. Be sure to follow the steps slowly.

Second case

Problem Description

环境:IDEA 2021.1 windows11 mysql8.0 
已知条件:
	1.已确定正常安装对应的驱动
	2.在windows的CMD窗口中能正常通过 [mysql -u root -p 密码] 进入数据库
	3.可以在IDEA的终端中正常输入账号密码和数据库进入
	4.远程访问或者idea登录时出现此错误。
	5.使用IDEA测试JDBC连接时,发现无法连接JDBC
	6.Access denied for user 'root'@'localhost' (using password: YES)
代码:
public static void main(String[] args) {
    
    
        try {
    
    
            Driver driver=new com.mysql.cj.jdbc.Driver();
            DriverManager.registerDriver(driver);
            String url="jdbc:mysql://localhost:3306/sqlstudy?useUnicode=true&characterEncoding=UTF-8";
            String user="root";
            String password="li1473606768";
            Connection connection=DriverManager.getConnection(url,user,password);
            System.out.println(connection);
        } catch (SQLException e) {
    
    
            System.out.println("检测到异常");
            e.printStackTrace();
        }
}

solution

原因:
服务中有一个mysqlzt的服务器将端口占用了,无法进行远程访问
参考文章:
CSDN 文章:https://blog.csdn.net/evan_one/article/details/113941416

The service where the error occurred:

Please add image description

The server time zone value ‘xxxxxxxxxx’ is unrecognized or represents more than one time zone.

Problem Description

环境:IDEA 2021.1 windows11 mysql8.0 
1.可以连接数据库
2.通过JAVA JDBC连接时,出现以上问题

solution

问题提示时区未配置正确
在进行JDBC连接时,8.0版本以上就要求绑定时区了,最好将charactEncoding=UTF8加上...

String url="jdbc:mysql://localhost:3306/sqlstudy";
更改:
String url="jdbc:mysql://localhost:3306/sqlstudy?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT";

//可以使GMT也可以是UTC

Guess you like

Origin blog.csdn.net/weixin_44673253/article/details/131990936