(JOOQ)当我尝试编译项目时,Mysql 出现连接错误

Posted

技术标签:

【中文标题】(JOOQ)当我尝试编译项目时,Mysql 出现连接错误【英文标题】:(JOOQ) When I try to compile project get connection error with Mysql 【发布时间】:2019-09-22 03:18:37 【问题描述】:

当我尝试编译在 Spring-Boot 上编写的项目时,Jooq 和 mysql 收到此错误:

[ERROR] Failed to execute goal org.jooq:jooq-codegen-maven:3.11.11:generate (default) on project myapp: Error running jOOQ code generation tool: Communications link failure
[ERROR] 
[ERROR] The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Connection refused (Connection refused)
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.jooq:jooq-codegen-maven:3.11.11:generate (default) on project myapp: Error running jOOQ code generation tool
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.apache.maven.plugin.MojoExecutionException: Error running jOOQ code generation tool
    at org.jooq.codegen.maven.Plugin.execute (Plugin.java:200)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: 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:590)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException (SQLExceptionsMapping.java:57)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO (ConnectionImpl.java:1606)
    at com.mysql.cj.jdbc.ConnectionImpl.<init> (ConnectionImpl.java:633)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance (ConnectionImpl.java:347)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect (NonRegisteringDriver.java:219)
    at org.jooq.codegen.GenerationTool.run (GenerationTool.java:320)
    at org.jooq.codegen.GenerationTool.generate (GenerationTool.java:222)
    at org.jooq.codegen.maven.Plugin.execute (Plugin.java:197)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: com.mysql.cj.core.exceptions.CJCommunicationsException: 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 sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance (Constructor.java:423)
    at com.mysql.cj.core.exceptions.ExceptionFactory.createException (ExceptionFactory.java:54)
    at com.mysql.cj.core.exceptions.ExceptionFactory.createException (ExceptionFactory.java:93)
    at com.mysql.cj.core.exceptions.ExceptionFactory.createException (ExceptionFactory.java:133)
    at com.mysql.cj.core.exceptions.ExceptionFactory.createCommunicationsException (ExceptionFactory.java:149)
    at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect (MysqlaSocketConnection.java:83)
    at com.mysql.cj.mysqla.MysqlaSession.connect (MysqlaSession.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly (ConnectionImpl.java:1726)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO (ConnectionImpl.java:1596)
    at com.mysql.cj.jdbc.ConnectionImpl.<init> (ConnectionImpl.java:633)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance (ConnectionImpl.java:347)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect (NonRegisteringDriver.java:219)
    at org.jooq.codegen.GenerationTool.run (GenerationTool.java:320)
    at org.jooq.codegen.GenerationTool.generate (GenerationTool.java:222)
    at org.jooq.codegen.maven.Plugin.execute (Plugin.java:197)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect (Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect (AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect (AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect (SocksSocketImpl.java:392)
    at java.net.Socket.connect (Socket.java:589)
    at com.mysql.cj.core.io.StandardSocketFactory.connect (StandardSocketFactory.java:202)
    at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect (MysqlaSocketConnection.java:57)
    at com.mysql.cj.mysqla.MysqlaSession.connect (MysqlaSession.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly (ConnectionImpl.java:1726)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO (ConnectionImpl.java:1596)
    at com.mysql.cj.jdbc.ConnectionImpl.<init> (ConnectionImpl.java:633)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance (ConnectionImpl.java:347)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect (NonRegisteringDriver.java:219)
    at org.jooq.codegen.GenerationTool.run (GenerationTool.java:320)
    at org.jooq.codegen.GenerationTool.generate (GenerationTool.java:222)
    at org.jooq.codegen.maven.Plugin.execute (Plugin.java:197)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command

java -version openjdk 版本“1.8.0_212” OpenJDK 运行时环境 (build 1.8.0_212-8u212-b01-1-b01) OpenJDK 64-Bit Server VM (build 25.212-b01,混合模式)

mvn -version Apache Maven 3.6.0 Maven 主页:/usr/share/maven Java 版本: 1.8.0_212,供应商:Oracle Corporation,运行时:/usr/lib/jvm/java-8-openjdk-amd64/jre 默认语言环境:en_US,平台 编码:UTF-8 操作系统名称:“linux”

application.yaml

db:
  username: root
  password: 123
  dbname: myapp
  dbaddress: localhost

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.myapp</groupId>
        <artifactId>myapp-api</artifactId>
        <version>0.1</version>
    </parent>
    <artifactId>myapp-api-db</artifactId>
    <version>0.2</version>

    <dependencies>
        <!-- Database access -->
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq</artifactId>
            <version>3.11.11</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>$spring-security.version</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>$junit.version</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <configLocation>
            $basedir/../myapp-api-app/src/main/resources/application.yml
        </configLocation>
        <buildEnvironment>prod</buildEnvironment>
        <databaseUri>
            jdbc:mysql://mysql/myapp?user=root&amp;password=123&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC&amp;characterEncoding=UTF-8&amp;characterSetResults=utf8&amp;connectionCollation=utf8_general_ci
        </databaseUri>
    </properties>

    <profiles>
        <profile>
            <id>default-cli</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jooq</groupId>
                        <artifactId>jooq-codegen-maven</artifactId>
                        <version>3.11.11</version>
                        <executions>
                            <execution>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>generate</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <jdbc>
                                <driver>com.mysql.cj.jdbc.Driver</driver>
                                <url>$databaseUri</url>
                                <user>root</user>
                                <password>123</password>
                            </jdbc>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources/hibernate</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>it.ozimov</groupId>
                <artifactId>yaml-properties-maven-plugin</artifactId>
                <version>1.1.3</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>$configLocation</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.jooq</groupId>
                <artifactId>jooq-codegen-maven</artifactId>
                <version>3.11.11</version>
                <configuration>
                    <generator>
                        <database>
                            <inputSchema>myapp</inputSchema>
                            <excludes>SCHEMA_VERSION</excludes>
                            <name>org.jooq.meta.mysql.MySQLDatabase</name>
                        </database>
                        <target>
                            <packageName>com.myapp.db</packageName>
                            <directory>target/generated-sources/jooq</directory>
                            <encoding>UTF-8</encoding>
                        </target>
                        <generate>
                            <fluentSetters>true</fluentSetters>
                            <daos>true</daos>
                        </generate>
                    </generator>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

可能是什么原因?谢谢。

【问题讨论】:

从日志中我们可以看到与您的数据源的连接存在问题:Connection refused。请附上您的数据源配置。 更准确地说,您为代码生成器工具提供的数据源无法连接到您的应用程序。除您的数据库配置外,请提供您的pom.xmlbuild.gradle 文件。 您能否使用外部工具连接到您的数据库,否则它也会失败? @SofoGial 我可以。 mysql连接没有问题。 【参考方案1】:

我认为pom.xml 中的数据库 URL 看起来有点奇怪。你有jdbc:mysql://mysql/myapp?user=root&amp;amp;password=123... 而在application.yaml 你说主机是localhost。所以试试jdbc:mysql://localhost/myapp?user=root&amp;amp;password=123...吧。

【讨论】:

谢谢@knutwannheden。原因是flyway自动生成的文件。我已经删除了它们,它有帮助。不过你说的也是真的。再次感谢您。

以上是关于(JOOQ)当我尝试编译项目时,Mysql 出现连接错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 JOOQ 从 MS SQL 数据库生成代码时遇到问题

JOOQ 从表中获取列

尝试连接到远程 MySQL 主机(错误 2003)

无法使用Debian 9下的mysql工作台连接到数据库服务器

当我尝试连接到Mysql时,如何在检测到JSONException时修复错误?

不存在的 MySql 消息错误