我遇到了一个关于Spring启动的非常奇怪的现象,并使用mysql存储数据,今天在我的私人计算机上我git克隆了我正在开发的项目。在eclipse中运行入口主类时,它会抛出异常
2015-11-15 18:28:08.912 ERROR 9183 --- [ restartedMain] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1665) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1207) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2249) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2280) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2079) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794) ~[mysql-connector-java-5.1.36.jar:5.1.36]
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) ~[mysql-connector-java-5.1.36.jar:5.1.36]
它的application.properties:
spring.datasource.url = jdbc:mysql://localhost:3306/foo?useUnicode=true&characterEncoding=utf8
spring.datasource.username = root
spring.datasource.password = 123456
但我可以直接在终端登录mysql,见下文:
mysql -hlocalhost -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 50
Server version: 5.6.27-0ubuntu0.14.04.1 (Ubuntu)
我甚至在同一个项目中编写了一个示例java代码来测试与本地mysql服务器的连接,
public static void main(String[] args) throws SQLException {
String url = "jdbc:mysql://localhost:3306/foo?useUnicode=true&characterEncoding=utf8";
String user = "root";
String password = "123456";
Connection connection = DriverManager.getConnection(url , user , password);
System.out.println(connection);
Statement stat = connection.createStatement();
ResultSet rs = stat.executeQuery("select version()");
rs.next();
System.out.println(rs.getString(1));
}
运行它有正常输出:
com.mysql.jdbc.JDBC4Connection@48cf768c
5.6.27-0ubuntu0.14.04.1
那么为什么spring-boot无法连接到本地mysql服务器,但其他方式可以吗?