簡體   English   中英

無法使用JDBC連接到mySql docker容器

[英]Can't connect to mySql docker container with JDBC

我使用Docker Maven插件

當測試集成開始時,我可以使用以下命令在終端的容器上連接到mysql:

mysql -h 127.0.0.1 -P 32795 -uroot -p

一切都很好,但是當我想使用以下代碼將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插件配置

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

問題是這樣的:

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>

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

bind-address = 0.0.0.0

就像這個答案中所解釋的(相反的情況:從docker容器連接到在主機上運行的mysql,但是這里的想法是一樣的),在橋接模式下,將bind-address設置為broadcast模式將有助於驗證mysql是可訪問的。

注意:如果使用bind-address = 0.0.0.0 MySQL服務器將偵聽所有網絡接口上的連接。 這意味着您可以從Internet訪問MySQL服務器。 確保相應地設置防火牆規則。

完成此測試后,檢查“ 如何從主機連接到在容器中運行的mysql

默認情況下,root僅具有從本地主機127.0.0.1&:: 1的訪問權限,您需要專門允許從192.168.99.1或用戶設置中使用“%”的任何位置進行訪問。
請參閱“ 保護初始MySQL帳戶 ”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM