6

I use Docker Maven Plugin

When test-integration starts i can connect to mysql on container in terminal with this command:

mysql -h 127.0.0.1 -P 32795 -uroot -p

and everythings works good but when i want to connect mysql in java app with JDBC with this code:

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection(
    "jdbc:mysql://127.0.0.1:" + System.getProperty("mysqlPort") + "/dashboardmanager",
    "root",
    "root"
);

i get this error:

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot create PoolableConnectionFactory (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 org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:615) ~[spring-jdbc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:866) ~[spring-jdbc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:927) ~[spring-jdbc-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:937) ~[spring-jdbc-4.2.4.RELEASE.jar:4.2.4.RELEASE]

I tried:

export _JAVA_OPTIONS="-Djava.net.preferIPv4Stack=true"

and

System.setProperty("java.net.preferIPv4Stack" , "true");

but nothing changed.

Docker Maven Plugin Conf:

<plugin>
            <groupId>org.jolokia</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>${docker-maven-plugin.version}</version>
            <configuration>
                <images>
                    <image>
                        <name>mysql:5.7.11</name>
                        <run>
                            <env>
                                <MYSQL_ROOT_PASSWORD>root</MYSQL_ROOT_PASSWORD>
                                <MYSQL_DATABASE>dashboardmanager</MYSQL_DATABASE>
                            </env>
                            <ports>
                                <port>mysqlPort:3306</port>
                            </ports>
                        </run>
                    </image>
                </images>
            </configuration>
            <executions>
                <execution>
                    <id>start</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stop</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
Arman
  • 1,019
  • 2
  • 14
  • 33

2 Answers2

4

The problem was this:

MySql starting process takes about 40 seconds, so i should stay about 40 seconds and after that try to connecting to mySql, so simple :)

Or i can use these settings in pom.xml:

<image>
    <name>mysql:5.7.11</name>
    <alias>mysqlContainer</alias>
    <run>
        <env>
            <MYSQL_ROOT_PASSWORD>root</MYSQL_ROOT_PASSWORD>
            <MYSQL_DATABASE>dashboard</MYSQL_DATABASE>
        </env>
        <ports>
            <port>mysqlPort:3306</port>
        </ports>
        <wait>
            <log>.*port: 3306  MySQL Community Server.*</log>
            <time>120000</time>
        </wait>
    </run>
</image>
Arman
  • 1,019
  • 2
  • 14
  • 33
0

Make sure your your MySQL config file (my.cnf) set in your mysql container uses:

bind-address = 0.0.0.0

As explained in this answer (for a reverse case: connecting to mysql running on host from a docker container, but the idea is the same here), in bridge mode, setting bind-address to broadcast mode would help validate that mysql is reacheable.

Note: if you use bind-address = 0.0.0.0 your MySQL server will listen for connections on all network interfaces. That means your MySQL server could be reached from the Internet ; make sure to setup firewall rules accordingly.

After this test, check "How to connect to mysql running in container from host machine"

By default, root only has access from the localhost, 127.0.0.1 & ::1, you need to specifically allow access from 192.168.99.1 or from anywhere using '%' in the user setup.
See "Securing the Initial MySQL Accounts".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I use [MYSQL](https://hub.docker.com/r/library/mysql/), is this use `bind-address = 0.0.0.0` ? If problem is ip address why i can connect in host machine terminal with `mysql -h 127.0.0.1 -P 32795 -uroot -p` ? – Arman Feb 17 '16 at 08:13
  • bind-address is not set. `my.cnf` is `#bind-address = 127.0.0.1` but i don't think problem is this because i can connect in host machine terminal with `mysql -h 127.0.0.1 -P 32795 -uroot -p` as said before – Arman Feb 17 '16 at 09:00
  • @Arman I though you tested from within the container, not from host (hence my suggestion). Still it is a good test to make: `bind-address = 0.0.0.0` – VonC Feb 17 '16 at 09:05