简体   繁体   中英

Error de conexion: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306

I have this scenery in my Linux local machine.

  1. mysql database run in my localhost (not in a container)

  2. A Java application, run in a container, needs to connect to Mysql.

  3. I'm using IntelliJ and Maven to develop code.

If I run the code from IntelliJ the application runs well, but if I generate the docker image and deploy it, the error is

Error de conexion: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/database_name?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

I can't understand if the error depends on the library jar mysql file that isn't correctly in the path or that there is something wrong in my docker-compose or in my.cnf.

These are all the configuration and the java code.

Thank's in advance for any suggestion!

Java code , where config.getMySqlConnection() is

"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();
}

my.cnf

[mysqld]

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

port = 3306
bind-address = 0.0.0.0

Docker-Compose

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>

I've just found the solution. There is an error in my docker-file...a stupid error...

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!**

etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM