java – 无法使用JDBC连接到mySql docker容器

我用Docker Maven Plugin

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

mysql -h 127.0.0.1 -P 32795 -uroot -p

并且每一件事情都很好,但是当我想用java代码连接这个代码时,在java应用程序中连接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:

最佳答案
问题是这样的:

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

或者我可以在pom.xml中使用这些设置:

相关文章

最近一直在开发Apworks框架的案例代码,同时也在一起修复Apw...
最近每天都在空闲时间努力编写Apworks框架的案例代码WeText。...
在《Kubernetes中分布式存储Rook-Ceph部署快速演练》文章中,...
最近在项目中有涉及到Kubernetes的分布式存储部分的内容,也...
CentOS下Docker与.netcore(一) 之 安装 CentOS下Docker与.ne...
CentOS下Docker与.netcore(一) 之 安装 CentOS下Docker与.ne...