无法使用JDBC

时间:2016-02-17 07:19:06

标签: java mysql maven jdbc docker

我使用Docker Maven Plugin

当测试集成开始时,我可以使用以下命令连接到终端上的容器上的mysql:

mysql -h 127.0.0.1 -P 32795 -uroot -p

并且每一件事情都很好,但是当我想用java代码将java app中的mysql与这段代码连接起来时:

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

我收到此错误:

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]

我试过了:

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

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

但没有改变。

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>

2 个答案:

答案 0 :(得分:4)

问题在于:

MySql启动过程大约需要40秒,所以我应该保持大约40秒,然后尝试连接到mySql,这么简单:)

或者我可以在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>

答案 1 :(得分:0)

确保在mysql容器中设置的MySQL配置文件(my.cnf)使用:

bind-address = 0.0.0.0

this answer中所述(对于相反的情况:从docker容器连接到主机上运行的mysql,但这里的想法是相同的),在桥接模式下,将bind-address设置为广播模式将有助于验证mysql是否可以访问。

  

注意:如果您使用bind-address = 0.0.0.0,您的MySQL服务器将侦听所有网络接口上的连接。这意味着可以从Internet访问您的MySQL服务器;确保相应地设置防火墙规则。

完成此测试后,请检查“How to connect to mysql running in container from host machine

  

默认情况下,root只能访问localhost,127.0.0.1&amp; :: 1,您需要特别允许从192.168.99.1或在用户设置中使用'%'的任何地方进行访问   请参阅“Securing the Initial MySQL Accounts”。