我有一个与mysql连接的spring-boot服务。当我使用maven和mysql的docker容器运行应用程序时,应用程序设置正常。
现在我想用docker运行应用程序,所以我使用docker-compose来引导mysql和我的服务作为容器。
但是我不工作,我的服务无法连接到mysql,发生以下异常:
accountservice_1 | 2015-11-18 08:55:11.626 ERROR 1 --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
accountservice_1 | com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
accountservice_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
accountservice_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_66-internal]
accountservice_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_66-internal]
accountservice_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_66-internal]
accountservice_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_66-internal]
accountservice_1 | at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:983) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2252) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_66-internal]
accountservice_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_66-internal]
accountservice_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_66-internal]
accountservice_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_66-internal]
accountservice_1 | at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:307) ~[tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:200) ~[tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:708) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:642) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:464) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:141) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:115) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:102) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:126) [tomcat-jdbc-8.0.28.jar!/:na]
accountservice_1 | at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:50) [flyway-core-3.2.1.jar!/:na]
accountservice_1 | at org.flywaydb.core.Flyway.execute(Flyway.java:1385) [flyway-core-3.2.1.jar!/:na]
accountservice_1 | at org.flywaydb.core.Flyway.migrate(Flyway.java:1006) [flyway-core-3.2.1.jar!/:na]
accountservice_1 | at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) [spring-boot-autoconfigure-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1051) [spring-context-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:828) [spring-context-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) [spring-context-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]
accountservice_1 | at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) [spring-boot-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [spring-boot-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) [spring-boot-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) [spring-boot-1.3.0.RELEASE.jar!/:1.3.0.RELEASE]
accountservice_1 | at net.twentyfourseven.account.AccountServiceApplication.main(AccountServiceApplication.java:44) [account-service.jar!/:0.0.1-SNAPSHOT]
accountservice_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_66-internal]
accountservice_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_66-internal]
accountservice_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_66-internal]
accountservice_1 | at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_66-internal]
accountservice_1 | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53) [account-service.jar!/:0.0.1-SNAPSHOT]
accountservice_1 | at java.lang.Thread.run(Thread.java:745) [na:1.8.0_66-internal]
accountservice_1 | Caused by: java.net.ConnectException: Connection refused
accountservice_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_66-internal]
accountservice_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_66-internal]
accountservice_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_66-internal]
accountservice_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_66-internal]
accountservice_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_66-internal]
accountservice_1 | at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_66-internal]
accountservice_1 | at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:214) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
accountservice_1 | at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
似乎Flyway数据库迁移存在问题......
现在,当我的服务容器退出并使用CTRL-C退出docker-compose并使用docker-compose重启时,一切正常,我的servive和mysql启动,并且飞路迁移按预期发生。
有没有人有想法?它对我来说没问题,因为我知道第二次启动时一切都会好起来的,但我真的不知道为什么会出现这个错误,这真的很烦人......
这是我的配置,缩写为:
flyway:
enabled: true
spring.datasource:
url: jdbc:mysql://localhost:3306/dbaccounts
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
spring.jpa:
database-platform: org.hibernate.dialect.MySQL5Dialect
show-sql: true
generate-ddl: false
hibernate:
ddl-auto: validate
我的docker-compose.yml:
mysqlaccounts:
image: mysql:5.6
ports:
- 3306:3306
environment:
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: dbaccounts
accountservice:
image: account-service
ports:
- 8081:8081
links:
- mysqlaccounts
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://mysqlaccounts:3306/dbaccounts
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=root
当我从头开始禁用飞路时,一切都很好......
提前感谢任何想法和建议..;)
P.S:我正在使用Ubuntu 15.10,Java8,spring-boot 1.3.RELEASE和Docker 1.9.0以及docker-compose版本:1.5.1。
答案 0 :(得分:0)
您应该将datasource url
中的主机替换为docker-machine
的IP。它看起来像:
jdbc:mysql://192.168.99.100/dbaccounts