【问题标题】:Can't connect to mySql docker container with JDBC无法使用 JDBC 连接到 mySql docker 容器
【发布时间】:2016-02-17 07:19:06
【问题描述】:

我用Docker Maven Plugin

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

mysql -h 127.0.0.1 -P 32795 -uroot -p

一切正常,但是当我想用 JDBC 将 java 应用程序中的 mysql 连接到 JDBC 时:

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 PluginConf:

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

【问题讨论】:

  • 您是在 in docker 容器中运行测试,还是要连接 mysql(从外部)容器?
  • 我想从外部连接到mysql
  • 你必须弄清楚你的容器的IP。很可能您必须使用 docker-machine ip YOUR_ENV 来找出该 ip。
  • 我可以在主机终端连接mysql -h 127.0.0.1 -P 32795 -uroot -p所以ip是127.0.0.1
  • 能否确定 System.getProperty("mysqlPort") 的值为 32795?

标签: java mysql maven jdbc docker


【解决方案1】:

问题是这样的:

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
【解决方案2】:

确保您在 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 和 ::1 访问,您需要明确允许从 192.168.99.1 或在用户设置中使用“%”的任何地方访问。
见“Securing the Initial MySQL Accounts”。

【讨论】:

  • 我用MYSQL,这是用bind-address = 0.0.0.0吗?如果问题是 IP 地址,为什么我可以在主机终端中使用 mysql -h 127.0.0.1 -P 32795 -uroot -p 连接?
  • 绑定地址未设置。 my.cnf#bind-address = 127.0.0.1 但我认为这不是问题,因为我可以使用 mysql -h 127.0.0.1 -P 32795 -uroot -p 在主机终端中连接,如前所述
  • @Arman 我虽然你是从容器内测试的,而不是从主机测试的(因此我的建议)。仍然是一个很好的测试:bind-address = 0.0.0.0
猜你喜欢
  • 2020-01-29
  • 2016-04-26
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 2020-12-30
  • 2020-07-29
  • 2022-11-18
  • 2020-05-25
相关资源
最近更新 更多