将SpringBoot与Mysql连接时出错

时间:2018-06-28 15:45:50

标签: mysql spring-boot

我正在尝试使用SpringBoot连接到MySQL数据库,但出现错误

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.*;
    import java.sql.*;

    @RestController
    @RequestMapping
    public class miController {

        private static String driver = "com.mysql.jdbc.Driver";
        private static String server = "jdbc:mysql://localhost/test";
        private static Connection conexion = null;

        miController(){
            conectar();
        }


        private void conectar() {
             try {
                 Class.forName(driver);
                 conexion = DriverManager.getConnection(server, "Yo", "pass");
             } catch (Exception e) {
                 System.out.println("Error: Imposible realizar la conexion a BD.");
                 e.printStackTrace();
             }

        }

    @RequestMapping(value = "/hola", method = RequestMethod.GET)
        public @ResponseBody String hola() {
            return "Hola mundo2!!  " ;
        }

    }

应用程序启动时给出的错误代码是:

Thu Jun 28 17:24:42 CEST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Error: Imposible realizar la conexion a BD.
java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:869)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:865)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1746)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2219)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2014)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)

这是我的第一个使用SpringBoot的应用程序,我不明白为什么我在连接mysql时会出错...我认为我看到了SSL内容,但我不明白... Ty

1 个答案:

答案 0 :(得分:0)

应在application.properties文件中为SpringBoot配置Mysql。这就是SpringBoot的优点。

配置示例如下:

#Mysql connection configurations
spring.datasource.url = jdbc:mysql://localhost:3306/yourDatabaseName?useSSL=false
spring.datasource.username = yourDbUserName
spring.datasource.password = yourDbPassword
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver