简体   繁体   中英

Unable to connect to MySQL running inside a Docker container (configured using docker-maven-plugin)

I'm facing issues connecting to the MySQL server running inside a Docker container. Here's a docker-maven-plugin configuration.

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.34.1</version>
    <extensions>true</extensions>
    <configuration>
        <images>
            <image>
                <alias>database</alias>
                <name>mysql:8.0</name>
                <run>
                    <log>
                        <prefix>TC</prefix>
                        <date>default</date>
                        <color>cyan</color>
                    </log>
                    <wait>
                        <log>mysqld: ready for connections</log>
                        <time>750000</time>
                    </wait>
                    <env>
                        <MYSQL_ROOT_PASSWORD>${test.database.password}</MYSQL_ROOT_PASSWORD>
                        <MYSQL_DATABASE>${test.database.name}</MYSQL_DATABASE>
                        <MYSQL_USER>${test.database.username}</MYSQL_USER>
                        <MYSQL_PASSWORD>${test.database.password}</MYSQL_PASSWORD>
                    </env>
                    <ports>
                        <port>3308:3306</port>
                    </ports>
                </run>
            </image>
        </images>
    </configuration>
    <executions>
        <execution>
            <id>docker:start</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>docker:stop</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

I see that MySQL started successfully in the logs:

04:20:18.583 TC2020-10-12T04:20:18.583211Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
[INFO] DOCKER> Pattern 'mysqld: ready for connections' matched for container 2f7f66ba896c
04:20:18.611 TC2020-10-12T04:20:18.610295Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.21'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
04:20:18.625 TC2020-10-12 04:20:18+00:00 [Note] [Entrypoint]: Temporary server started.
04:20:23.415 TCWarning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
[INFO] DOCKER> [mysql:8.0] "database": Waited on log out 'mysqld: ready for connections' 172861 ms
04:20:23.415 TCWarning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.

码头工人

Error connecting via JDBC, maven-failsafe-plugin fails to connect: I tried connecting with localhost:3308 , 127.0.0.1:3308 , 0.0.0.0:3308 and 10.0.75.0:3308 . Also, tried Network type, bridge , host and container .

04:20:31.697 TCWarning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
04:20:31.697 TCWarning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
.09:50:31.962 [main] INFO  com.zaxxer.hikari.HikariDataSource - jdbc:mysql://localhost:3308/test_db_name?useSSL=false&trustServerCertificate=false&serverSslCert= - Starting... 
.09:50:33.291 [main] ERROR com.zaxxer.hikari.pool.HikariPool - jdbc:mysql://localhost:3308/test_db_name?useSSL=false&trustServerCertificate=false&serverSslCert= - Exception during pool initialization. 
java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=localhost)(port=3308)(type=master) : Could not connect to localhost:3308 : unexpected end of stream, read 0 bytes from 4 (socket was closed by server)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.createException(ExceptionFactory.java:73)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.create(ExceptionFactory.java:192)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1346)
    at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:634)
    at org.mariadb.jdbc.MariaDbConnection.newConnection(MariaDbConnection.java:150)
    at org.mariadb.jdbc.Driver.connect(Driver.java:89)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:354)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:554)

Docker Network Config: Docker 网络配置

It is mostly about mysql version.

It will probably work if you try with mysql:5.7

Security requirements are different in later versions.

it can be disabled with

default_authentication_plugin=mysql_native_password

but i don't know where to put it in docker-maven-plugin.

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