Mysql连接失败的问题

安装什么的问题就不说了,就记录一下连接数据库失败的一些解决方法。

一..报错。com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client

public static void main(String[] args) throws ClassNotFoundException {
		String driver ="com.mysql.jdbc.Driver";
		String url="jdbc:mysql://localhost:3306/myblog;
		String username="localhost";
		String password="123456";
		Class.forName(driver);
		getConn(url, username, password);
		
	}
	public static Connection getConn(String url, String username,String password){
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(url, username, password);
			System.out.println("数据库连接成功!!");
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("数据库连接失败!!");
		}
		return conn;
	}

这种报错可能就是用户和密码的问题,如果不知道的可以创建一个新的用户进行连接。创建方法如下:

1.打开命令行cmd

2.开启数据库:net start Mysql

3.登陆数据库

4.更改数据库的加密方式输入:ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;

5.更改密码: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123';

6.刷新权限:FLUSH PRIVILEGES;

7.在客户端就可连接成功。

二.可能eclipse会继续报错

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

这个就是url的路径出问题了,编码格式不对需要在后面添加?useUnicode=true&characterEncoding=utf8";

        String url="jdbc:mysql://localhost:3306/myblog?useUnicode=true&characterEncoding=utf8";

这样之后就没问题了。

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值