前提・実現したいこと
以下のサイトを参考にして勉強しています
前提としてDocker上で動かしており、ツールはIntelliJ IDEAを使用しています。
途中のFlywayの項まで進めたのですがコンパイルしようとするとエラーが発生しました。量が多く同じエラーの繰り返しなので一部を載せます。
発生している問題・エラーメッセージ
2020-12-24 14:01:38.640 INFO 115 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41] 2020-12-24 14:01:39.124 INFO 115 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-12-24 14:01:39.125 INFO 115 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 14247 ms 2020-12-24 14:01:40.746 INFO 115 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 7.1.1 by Redgate 2020-12-24 14:01:40.893 INFO 115 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2020-12-24 14:01:46.900 INFO 115 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2020-12-24 14:02:16.945 WARN 115 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.exception.FlywaySqlException: Unable to obtain connection from database: HikariPool-1 - Connection is not available, request timed out after 30002ms. ----------------------------------------------------------------------------------------------------------------------- SQL State : 08001 Error Code : 0 Message : Could not create connection to database server. Attempted reconnect 3 times. Giving up. 2020-12-24 14:02:16.945 INFO 115 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2020-12-24 14:02:22.978 INFO 115 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. 2020-12-24 14:02:23.011 INFO 115 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2020-12-24 14:02:23.072 INFO 115 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2020-12-24 14:02:23.263 ERROR 115 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.exception.FlywaySqlException: Unable to obtain connection from database: HikariPool-1 - Connection is not available, request timed out after 30002ms. ----------------------------------------------------------------------------------------------------------------------- SQL State : 08001 Error Code : 0 Message : Could not create connection to database server. Attempted reconnect 3 times. Giving up. ----------------------------------------------------------------------------------------------------------------------- SQL State : 08001 Error Code : 0 Message : Could not create connection to database server. Attempted reconnect 3 times. Giving up.
ソースコード
docker-comopse.yml
version: '3' services: app: image: openjdk:14-jdk-alpine ports: - "80:8080" volumes: - .:/app working_dir: /app command: ./gradlew bootRun environment: DATASOURCE: jdbc:mysql://db:3306/springboot-flyway db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: springboot-flyway MYSQL_USER: user MYSQL_PASSWORD: passwrod command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci --default-time-zone=Asia/Tokyo volumes: - ./docker/mysql:/docker-entrypoint-initdb.d ports: - "3306:3306"
application.yml
spring: datasource: url: jdbc:mysql://localhost:3306/springboot-flyway?autoReconnect=true&useSSL=false username: user password: password driver-class-name: com.mysql.cj.jdbc.Driver hikari: initialization-fail-timeout: 0
FlywayConfig.java
package shunp.geminiapi.config; import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FlywayConfig { @Bean public FlywayMigrationStrategy strategy1111() { return flyway -> { // flyway_schema_historyの初期化 flyway.clean(); // マイグレーション実行 flyway.migrate(); }; } }
試したこと
最初はドライバーは存在しないといわれていたので'com.mysql.jdbc.Driber'から'con.mysql.cj.jdbc.Driver'に変更しました。
ほかにHikariCPというライブラリのエラーかと思ったのでオプションの指定を行ったりました。
MysqlのTimeZoneの設定を行いました。
###まとめ
数時間格闘したのですが解決方法がわかりません。どうか回答のほうお願いします。
あなたの回答
tips
プレビュー