我正在使用spring-boot在使用MySQL的docker上运行我的微服务,但我的应用无法与MySQL建立连接
例外是:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1 | at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
app_1 | at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
app_1 | at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
app_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:121)
app_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
app_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
app_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)
app_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560)
app_1 | ... 69 more
app_1 | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
app_1 |
app_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
app_1 | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
app_1 | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
app_1 | at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
app_1 | at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
app_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
app_1 | at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)
app_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
app_1 | ... 77 more
app_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
app_1 | at java.net.PlainSocketImpl.socketConnect(Native Method)
app_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
app_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
app_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
app_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
app_1 | at java.net.Socket.connect(Socket.java:589)
app_1 | at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
app_1 | at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
app_1 | ... 80 more
docker-compose文件为:
version: '3'
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_DATABASE=inward
# So you don't have to use root, but you can if you like
- MYSQL_USER=root
# You can use whatever password you like
- MYSQL_PASSWORD=unroot
# Password for root access
- MYSQL_ROOT_PASSWORD=unroot
ports:
# <Port exposed> : < MySQL Port running inside container>
- 3307:3306
app:
build:
context: ./
dockerfile: Dockerfile
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/inward?allowPublicKeyRetrieval=true&useSSL=false
depends_on:
- db
working_dir: /app
command: [sh, -c, 'mkdir -p ~/logs/; cd /src ; mvn clean spring-boot:run -Dspring.profiles.active=dev -DLOG_DIR=/root/logs/ -DLOG_FILE=hubstamper.log']
ports:
- "8080:8080"
volumes:
- "${HOME}/.m2:/root/.m2"
Dockerfiule
FROM maven:3.5-jdk-8-alpine
COPY . /app
和application.properties文件是:
jdbcUrl=jdbc:mysql://db:3306/inward?allowPublicKeyRetrieval=true&useSSL=false
dataSource.user=root
dataSource.password=unroot
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
driverClassName=com.mysql.jdbc.Driver
dataSource.driverClassName=com.mysql.jdbc.Driver
我已经尝试了链接中提到的解决方案:Communications link failure , Spring Boot + MySql +Docker + Hibernate
任何帮助都是适用的。
services:
db:
image: mysql:5.7
restart: always
environment:
- MYSQL_DATABASE=inward
# So you don't have to use root, but you can if you like
- MYSQL_USER=root
# You can use whatever password you like
- MYSQL_PASSWORD=unroot
# Password for root access
- MYSQL_ROOT_PASSWORD=unroot
ports:
# <Port exposed> : < MySQL Port running inside container>
- 3307:3306
app:
build:
context: ./
dockerfile: Dockerfile
depends_on:
- db
working_dir: /app
command: [sh, -c, 'mkdir -p ~/logs/; cd /src ; mvn clean spring-boot:run -Dspring.profiles.active=local -DLOG_DIR=/root/logs/ -DLOG_FILE=hubstamper.log']
ports:
- 8080:8080
volumes:
- "${HOME}/.m2:/root/.m2"
和apllication-local.properties文件
jdbcUrl=jdbc:mysql://db:3306/inward?allowPublicKeyRetrieval=true&useSSL=false dataSource.user=root dataSource.password=unroot dataSource.cachePrepStmts=true dataSource.prepStmtCacheSize=250 dataSource.prepStmtCacheSqlLimit=2048 driverClassName=com.mysql.jdbc.Driver dataSource.driverClassName=com.mysql.jdbc.Driver
和docker文件
FROM maven:3.5-jdk-8-alpine COPY . /app
并且请确保每次更改代码中的任何内容时,都删除旧的docker映像,然后创建一个新的docker映像(这也是我在我的方法中所缺少的。)>