error de conexion:java.sql.sqlexception:找不到适合的驱动程序jdbc:mysql://localhost:3306

zu0ti5jz  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(331)

在我的linux本地机器中有这样的场景。
mysql数据库在本地主机中运行(不在容器中)
在容器中运行的java应用程序需要连接到mysql。
我正在使用intellij和maven开发代码。
如果我从intellij运行代码,应用程序运行良好,但是如果我生成docker映像并部署它,错误是
error de conexion:java.sql.sqlexception:找不到适合的驱动程序jdbc:mysql://localhost:3306/database_name?useunicode=true&usejdbccomplianttimezoneshift=true&uselegacydatetimecode=false&servertimezone=utc
我不明白这个错误是因为库jar mysql文件在路径中不正确,还是我的docker compose或my.cnf中有问题。
这些都是配置和java代码。
提前感谢您的建议!
java代码,其中config.getmysqlconnection()是
"jdbc:mysql://localhost:3306/database_name?useunicode=true&usejdbccomplianttimezoneshift=true&uselegacydatetimecode=false&servertimezone=utc"

try
{
    // DriverManager: The basic service for managing a set of JDBC drivers.
    dbConnection = DriverManager.getConnection(config.getMySqlConnection(), config.getMySqlLogin(), config.getMySqlPassword());

    if (dbConnection != null)
    {
        System.out.println("Connection Successful! Enjoy. Now it's time to push data");
    }
    else
    {
        System.out.println("Failed to make connection!");
    }
} catch (SQLException e) {
    System.out.println("Error de conexion: "+e);
    e.printStackTrace();
} catch (Exception e) {
    System.out.println("Error desconocido: "+e);
    e.printStackTrace();
}

我的.cnf

[mysqld]

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

port = 3306
bind-address = 0.0.0.0

Docker

version: '2.3'
services:
  stationfeeder:
    image: repository/service_name:2.0
    container_name: containername
    mem_limit: 2048m
    entrypoint:
        - java
        - -cp
        - /app/resources:/app/classes:/app/libs/
        - -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
        - com.prject-entrypoint.Host
        - /properties
    network_mode: "host"
    environment:
        - JAVA_OPTS="-Xms2g -Xmx2g"
    volumes:
      - ./properties:/properties
    restart: always
    logging:
        driver: "json-file"
        options:
            max-size: "100m"
            max-file: "10"

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>com.project.name</groupId>
    <artifactId>http-opendata</artifactId>
    <version>2.0</version>

    <properties>
        <avro.version>1.8.2</avro.version>
        <kafka.version>0.11.0.1</kafka.version>
        <confluent.version>3.3.1</confluent.version>
        <jib-maven-plugin.version>2.5.0</jib-maven-plugin.version>
    </properties>

    <!--necessary to resolve confluent dependencies-->
    <repositories>
        <repository>
            <id>confluent</id>
            <url>http://packages.confluent.io/maven/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20140107</version>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
        <dependency>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro</artifactId>
            <version>${avro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>${kafka.version}</version>
        </dependency>

        <dependency>
            <groupId>io.confluent</groupId>
            <artifactId>kafka-avro-serializer</artifactId>
            <version>${confluent.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <dependency>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>jib-maven-plugin</artifactId>
            <version>1.6.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/services/java.sql.Driver</resource>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!--for specific record-->
            <plugin>
                <groupId>org.apache.avro</groupId>
                <artifactId>avro-maven-plugin</artifactId>
                <version>${avro.version}</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>schema</goal>
                            <goal>protocol</goal>
                            <goal>idl-protocol</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
                            <stringType>String</stringType>
                            <createSetters>false</createSetters>
                            <enableDecimalLogicalType>true</enableDecimalLogicalType>
                            <fieldVisibility>private</fieldVisibility>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--force discovery of generated classes-->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/generated-sources/avro</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>${jib-maven-plugin.version}</version>
                <configuration>
                    <container>
                        <mainClass>com.nameserviceHost</mainClass>
                        <args>
                            <arg>/opt/asd/properties</arg>
                        </args>
                    </container>
                    <from>
                        <image>openjdk:8u212-jre</image>
                    </from>
                    <to>
                        <image>qwerty/name-service:${project.version}</image>
                    </to>
                    <extraDirectories>
                        <paths>/opt/asd</paths> 
                        <permissions>
                            <permission>
                                <file>/opt/asd/properties</file>
                                <mode>755</mode> 
                            </permission>
                        </permissions>
                    </extraDirectories>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
x8diyxa7

x8diyxa71#

我刚找到解决办法。我的docker文件有个错误…一个愚蠢的错误。。。

version: '2.3'
services:
stationfeeder:
image: projectName/imageName:2.5
container_name: name
mem_limit: 2048m
entrypoint:
- java
- -cp
-/app/resources:/app/classes:/app/libs/*   <------ without /***doesn't run!**

等。。。

相关问题