zoukankan      html  css  js  c++  java
  • 本地springboot项目连接本地mysql报错。com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

    报错描述:

    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.

    Caused by: java.net.SocketTimeoutException: connect timed out

    1.所使用的mysql-connector-java为8.0.25,mysql也为8.0.25。

    2.idea为社区版的。

    使用测试类测试数据库连接:就报了上面的错误。

    package com.example.demo;
    
    import java.sql.SQLException;
    
    import javax.sql.DataSource;
    
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class DemoApplicationTests {
    
        @Test
        void contextLoads() {
        }
    
        @Autowired
        DataSource dataSource;
    
        @Test
         void contextLoad() throws SQLException {
            System.out.println(dataSource.getClass());
            System.out.println(dataSource.getConnection());
        }

    查了很多资料,也换了idea版本,mysql版本,发现都不对,

    在尝试https://www.cnblogs.com/lzj-/p/12904279.html 这个修改的时候,发现这位同学说的连接的符号也从“&”换成”&不对,修改了之后还是会报错,无法解析。

    但是受到了一些启发,把自己的localhost改成了127.0.0.1,发现连接成功。

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncodeing=UTF-8&useSSL=false&serverTimezone=GMT 

    //原来是jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncodeing=UTF-8&useSSL=false&serverTimezone=GMT
    //直接用spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test 也可以。

    spring.datasource.username=xxxx
    spring.datasource.password
    =xxxxxxx
    ----------天道酬勤----------------
  • 相关阅读:
    如何使用Redis实现分布式缓存
    如何使用Swagger生成API文档
    Asp.Net Core WebApi入门
    如何使用Entity Framework Core实现增删改查(CRUD)
    Microsoft.Extensions.DependencyInjection入门
    什么是中介者模式
    什么是依赖注入
    什么是事件总线
    点滴智慧
    并查集
  • 原文地址:https://www.cnblogs.com/jiliunyongjin/p/14806073.html
Copyright © 2011-2022 走看看