2021-02-27 17:54:55.843 ERROR 7407 --- [           main] com.alibaba.druid.pool.DruidDataSource   : init datasource error, url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
	at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.23.jar:8.0.23]

本地代码通过springboot-mybatis-starter连接mysql,出现这个问题,可能的原因和解决方案如下:

    1.驱动版本问题。由于是mysql8,需要使用8以上的driver进行连接,且驱动需要使用新的:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

    2.用户没有对ip访问的权限。当配置使用了ip进行连接时,mysql需要在用户表设置对应用户的权限,如设置root用户可以使用所有ip进行连接:

mysql> create user root;
Query OK, 0 rows affected (0.02 sec)

mysql> grant all privileges on *.* to 'root'@'%';
Query OK, 0 rows affected (0.01 sec)

     设置完权限,重启服务,或执行 FLUSH PRIVILEGES。

    3.mysql服务未设置ip。在/etc/my.cnf进行设置

      [mysqld]

     bind-address=127.0.0.1  

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐