我想把我的spring rest api与数据库在mysql在Docker。应用程序。属性文件:
server.port=8082
spring.datasource.url=jdbc:mysql://database:8083/schoolregistrationsystem
spring.datasource.username=user
spring.datasource.password=pwd
spring.jpa.hibernate.ddl-auto=none
spring.liquibase.change-log=classpath:/db/changelog/dbchangelog.xml
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
接下来我创建了docker-compose。项目目录下的Yml文件:
version: "3.8"
services:
api:
build: ./SchoolRegistrationSystem
ports:
- "8082:8082"
depends_on:
- database
database:
container_name: "mysql"
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: schoolregistrationsystem
MYSQL_USER: user
MYSQL_PASSWORD: pwd
MYSQL_TCP_PORT: 8083
healthcheck:
test: "/usr/bin/mysql --user=user --password=pwd --execute "SHOW DATABASES;""
interval: 5s
timeout: 2s
retries: 15
ports:
- "8083:3306"
接下来,当我在power shell中运行docker-compose时,我得到了失败。我有2个容器:mysql -状态"运行"以及状态为"退出"的API。
来自docker的日志编辑:来自docker的完整日志
2022-07-18 14:09:52+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1.el8 started.
2022-07-18 14:09:52+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-07-18 14:09:52+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1.el8 started.
2022-07-18 14:09:53+00:00 [Note] [Entrypoint]: Initializing database files
2022-07-18 14:10:00+00:00 [Note] [Entrypoint]: Database files initialized
2022-07-18 14:10:00+00:00 [Note] [Entrypoint]: Starting temporary server
2022-07-18 14:10:00+00:00 [Note] [Entrypoint]: Temporary server started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
2022-07-18 14:10:03+00:00 [Note] [Entrypoint]: Creating database schoolregistrationsystem
2022-07-18 14:10:03+00:00 [Note] [Entrypoint]: Creating user user
2022-07-18 14:10:03+00:00 [Note] [Entrypoint]: Giving user user access to schema schoolregistrationsystem
2022-07-18 14:10:03+00:00 [Note] [Entrypoint]: Stopping temporary server
2022-07-18 14:10:04+00:00 [Note] [Entrypoint]: Temporary server stopped
2022-07-18 14:10:04+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
2022-07-18T14:09:53.062484Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.29) initializing of server in progress as process 42
2022-07-18T14:09:53.069610Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-07-18T14:09:53.547572Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-07-18T14:09:54.932915Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2022-07-18T14:10:00.436541Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.29) starting as process 97
2022-07-18T14:10:00.448283Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-07-18T14:10:00.610010Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-07-18T14:10:00.862119Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-07-18T14:10:00.862179Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-07-18T14:10:00.866036Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2022-07-18T14:10:00.912594Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
2022-07-18T14:10:00.912814Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.29' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server - GPL.
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
2022-07-18T14:10:03.489315Z 9 [Warning] [MY-013360] [Server] Plugin sha256_password reported: ''sha256_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'
2022-07-18T14:10:03.622492Z 14 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.29).
2022-07-18T14:10:04.566088Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.29) MySQL Community Server - GPL.
2022-07-18T14:10:04.808842Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.29) starting as process 1
2022-07-18T14:10:04.814632Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-07-18T14:10:04.919826Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-07-18T14:10:05.104905Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-07-18T14:10:05.104952Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-07-18T14:10:05.107347Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2022-07-18T14:10:05.122300Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2022-07-18T14:10:05.122383Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.29' socket: '/var/run/mysqld/mysqld.sock' port: 8083 MySQL Community Server - GPL.
我将添加Api
的日志om.mysql.cj.jdbc.exceptions.CommunicationsException: 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 com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar!/:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar!/:na]
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:266) ~[liquibase-core-4.9.1.jar!/:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.21.jar!/:5.3.21]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.21.jar!/:5.3.21]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.21.jar!/:5.3.21]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.21.jar!/:5.3.21]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.21.jar!/:5.3.21]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.21.jar!/:5.3.21]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.21.jar!/:5.3.21].....
也许您应该在启动api
容器之前为database
容器添加健康检查。在docker-compose.yml
的database
服务中添加以下行,然后重试
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=schoolregistrationsystem
- MYSQL_USER=user
- MYSQL_PASSWORD=pwd
- MYSQL_TCP_PORT=8083
healthcheck:
test: "/usr/bin/mysql --user=user --password=pwd --execute "SHOW DATABASES;""
interval: 5s
timeout: 2s
retries: 100
Mysql默认运行在3306端口
所以你必须在docker组合文件中使用这个端口,并将它映射到8083。
version: "3.8"
services:
api:
build: ./SchoolRegistrationSystem
ports:
- "8082:8082"
depends_on:
- database
database:
container_name: "mysql"
image: mysql:8
environment:
MYSQL_DATABASE: schoolregistrationsystem
MYSQL_ROOT_PASSWORD: root
ports:
- "8083:3306"
我还建议公开它3306,以免混淆别人。