无法将 MySQL 服务器连接到 Docker 中的 Spring 应用程序?
Not able to connect with MySQL server to Spring Application in Docker?
我是 Docker 的新手,作为 docker 化 spring mvc 应用程序的一部分,我无法将我的应用程序连接到 MySQL服务器
Dockerfile :this is Spring mvc application so need to copy my war to
tomcat container
FROM tomcat:8.0.20-jre8
COPY /target/CTH.war /usr/local/tomcat/webapps/
docker-compose.yml
version: '3'
services:
mysql-standalone:
image: 'mysql:5.7'
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
volumes:
- ./docker/provision/mysql/init:/docker-entrypoint-initdb.d
environment:
- MYSQL_ROOT_PASSWORD= root
- MYSQL_USER= root
- MYSQL_PASSWORD= root
- MYSQL_DATABASE= CTH
- MYSQL_DATABASE= CTH_CHAT
ports:
- "3307:3306"
cth-docker-container:
image: cth-docker-container
ports:
- "8082:8082"
environment:
CTH_DATASOURCE_DATABASE: CTH
CTH_DATASOURCE_SERVERNAME: mysql-standalone
CTH_DATASOURCE_USERNAME: root
CTH_DATASOURCE_PASSWORD: root
CTH_DATASOURCE_PORT: 3307
CTH_CHAT_DATASOURCE_DATABASE: CTH_CHAT
CTH_CHAT_DATASOURCE_SERVERNAME: mysql-standalone
CTH_CHAT_DATASOURCE_USERNAME: root
CTH_CHAT_DATASOURCE_PASSWORD: root
CTH_CHAT_DATASOURCE_PORT: 3307
build:
context: "./"
dockerfile: "Dockerfile"
depends_on:
- mysql-standalone
application.properties :this is spring mvc application and application uses MySQL db with two databases
1st db
dbuser.local=${CTH_DATASOURCE_USERNAME:root}
dbpassword.local=${CTH_DATASOURCE_PASSWORD:root}
dbdatabaseName.local=${CTH_DATASOURCE_DATABASE:CTH}
dbserverName.local=${CTH_DATASOURCE_SERVERNAME:localhost}
dbportNumber.local=${CTH_DATASOURCE_PORT:3306}
2nd db
dbuser.cth.chat.local=${CTH_CHAT_DATASOURCE_USERNAME:root}
dbpassword.cth.chat.local=${CTH_CHAT_DATASOURCE_PASSWORD:root}
dbdatabaseName.cth.chat.local=${CTH_CHAT_DATASOURCE_USERNAME:CTH_CHAT}
dbserverName.cth.chat.local=${CTH_CHAT_DATASOURCE_SERVERNAME:localhost}
dbportNumber.cth.chat.local=${CTH_CHAT_DATASOURCE_PORT:3306}
我阅读了我创建 docker 文件和 docker 撰写文件的文章 https://medium.com/@asce4s/dockerize-spring-mvc-application-a9ffbd11eadb https://github.com/abagayev/docker-bootstrap-collection/tree/master/mysql-few-databases/docker/provision/mysql/init
but i'm getting following erros when i execute
docker-compose -f docker-compose.yml up
Caused by: com.mysql.cj.core.exceptions.CJCommunicationsException: Communications link failure
cth-docker-container_1 |
cth-docker-container_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
cth-docker-container_1 | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
cth-docker-container_1 | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
cth-docker-container_1 | at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
cth-docker-container_1 | at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
cth-docker-container_1 | at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
cth-docker-container_1 | at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:93)
cth-docker-container_1 | at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:133)
cth-docker-container_1 | at com.mysql.cj.core.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:149)
cth-docker-container_1 | at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:83)
cth-docker-container_1 | at com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:122)
cth-docker-container_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1754)
cth-docker-container_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1624)
cth-docker-container_1 | ... 125 common frames omitted
cth-docker-container_1 | Caused by: java.net.UnknownHostException: mysql-standalone
cth-docker-container_1 | at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797)
cth-docker-container_1 | at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
cth-docker-container_1 | at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
cth-docker-container_1 | at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)
cth-docker-container_1 | at com.mysql.cj.core.io.StandardSocketFactory.connect(StandardSocketFactory.java:179)
cth-docker-container_1 | at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:57)
cth-docker-container_1 | ... 128 common frames omitted
如果我设置了错误的环境或者我的方式不正确你的任何建议我一定会帮助我
谢谢!
阅读大量文章后,我成功地在 docker 容器中使用 MySQL 数据库启动我的应用程序并在 Mysql workbench.[=13 中访问=]
这是我的 docker-compose 文件
version: '3'
services:
app:
container_name: cth-app
build:
context: .
dockerfile: Dockerfile
ports:
- '8080:8080'
environment:
##db1
CTH_DATASOURCE_DATABASE: CTH
CTH_DATASOURCE_HOST: mysqldb
CTH_DATASOURCE_USERNAME: cth
CTH_DATASOURCE_PASSWORD: root
CTH_DATASOURCE_PORT: 3306
##db2
CTH_CHAT_DATASOURCE_DATABASENAME: CTH_CHAT
CTH_CHAT_DATASOURCE_HOST: mysqldb
CTH_CHAT_DATASOURCE_USERNAME: cth
CTH_CHAT_DATASOURCE_PASSWORD: root
CTH_CHAT_DATASOURCE_PORT: 3306
depends_on:
- mysqldb
mysqldb:
image: mysql/mysql-server:5.7
container_name: mysqldb
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
##here i am able to access dump file which I'll create two databases
with user created for me.
volumes:
- ./docker/provision/mysql/init:/docker-entrypoint-initdb.d
ports:
- '3308:3306'
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: cth
MYSQL_PASSWORD: root
you need to create a dump file refer the article [ https://github.com/abagayev/docker-bootstrap-collection/tree/master/mysql-few-databases]
你什么时候docker-compose up -d --build
您的应用程序在 运行 端口 8080 上启动,MySQL 数据库 运行 在主机中
谢谢!
我是 Docker 的新手,作为 docker 化 spring mvc 应用程序的一部分,我无法将我的应用程序连接到 MySQL服务器
Dockerfile :this is Spring mvc application so need to copy my war to
tomcat container
FROM tomcat:8.0.20-jre8
COPY /target/CTH.war /usr/local/tomcat/webapps/
docker-compose.yml
version: '3'
services:
mysql-standalone:
image: 'mysql:5.7'
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
volumes:
- ./docker/provision/mysql/init:/docker-entrypoint-initdb.d
environment:
- MYSQL_ROOT_PASSWORD= root
- MYSQL_USER= root
- MYSQL_PASSWORD= root
- MYSQL_DATABASE= CTH
- MYSQL_DATABASE= CTH_CHAT
ports:
- "3307:3306"
cth-docker-container:
image: cth-docker-container
ports:
- "8082:8082"
environment:
CTH_DATASOURCE_DATABASE: CTH
CTH_DATASOURCE_SERVERNAME: mysql-standalone
CTH_DATASOURCE_USERNAME: root
CTH_DATASOURCE_PASSWORD: root
CTH_DATASOURCE_PORT: 3307
CTH_CHAT_DATASOURCE_DATABASE: CTH_CHAT
CTH_CHAT_DATASOURCE_SERVERNAME: mysql-standalone
CTH_CHAT_DATASOURCE_USERNAME: root
CTH_CHAT_DATASOURCE_PASSWORD: root
CTH_CHAT_DATASOURCE_PORT: 3307
build:
context: "./"
dockerfile: "Dockerfile"
depends_on:
- mysql-standalone
application.properties :this is spring mvc application and application uses MySQL db with two databases
1st db
dbuser.local=${CTH_DATASOURCE_USERNAME:root}
dbpassword.local=${CTH_DATASOURCE_PASSWORD:root}
dbdatabaseName.local=${CTH_DATASOURCE_DATABASE:CTH}
dbserverName.local=${CTH_DATASOURCE_SERVERNAME:localhost}
dbportNumber.local=${CTH_DATASOURCE_PORT:3306}
2nd db
dbuser.cth.chat.local=${CTH_CHAT_DATASOURCE_USERNAME:root}
dbpassword.cth.chat.local=${CTH_CHAT_DATASOURCE_PASSWORD:root}
dbdatabaseName.cth.chat.local=${CTH_CHAT_DATASOURCE_USERNAME:CTH_CHAT}
dbserverName.cth.chat.local=${CTH_CHAT_DATASOURCE_SERVERNAME:localhost}
dbportNumber.cth.chat.local=${CTH_CHAT_DATASOURCE_PORT:3306}
我阅读了我创建 docker 文件和 docker 撰写文件的文章 https://medium.com/@asce4s/dockerize-spring-mvc-application-a9ffbd11eadb https://github.com/abagayev/docker-bootstrap-collection/tree/master/mysql-few-databases/docker/provision/mysql/init
but i'm getting following erros when i execute docker-compose -f docker-compose.yml up
Caused by: com.mysql.cj.core.exceptions.CJCommunicationsException: Communications link failure cth-docker-container_1 | cth-docker-container_1 | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. cth-docker-container_1 | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) cth-docker-container_1 | at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) cth-docker-container_1 | at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) cth-docker-container_1 | at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) cth-docker-container_1 | at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54) cth-docker-container_1 | at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:93) cth-docker-container_1 | at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:133) cth-docker-container_1 | at com.mysql.cj.core.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:149) cth-docker-container_1 | at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:83) cth-docker-container_1 | at com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:122) cth-docker-container_1 | at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1754) cth-docker-container_1 | at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1624) cth-docker-container_1 | ... 125 common frames omitted cth-docker-container_1 | Caused by: java.net.UnknownHostException: mysql-standalone cth-docker-container_1 | at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797) cth-docker-container_1 | at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505) cth-docker-container_1 | at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364) cth-docker-container_1 | at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298) cth-docker-container_1 | at com.mysql.cj.core.io.StandardSocketFactory.connect(StandardSocketFactory.java:179) cth-docker-container_1 | at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:57) cth-docker-container_1 | ... 128 common frames omitted
如果我设置了错误的环境或者我的方式不正确你的任何建议我一定会帮助我 谢谢!
阅读大量文章后,我成功地在 docker 容器中使用 MySQL 数据库启动我的应用程序并在 Mysql workbench.[=13 中访问=]
这是我的 docker-compose 文件
version: '3'
services:
app:
container_name: cth-app
build:
context: .
dockerfile: Dockerfile
ports:
- '8080:8080'
environment:
##db1
CTH_DATASOURCE_DATABASE: CTH
CTH_DATASOURCE_HOST: mysqldb
CTH_DATASOURCE_USERNAME: cth
CTH_DATASOURCE_PASSWORD: root
CTH_DATASOURCE_PORT: 3306
##db2
CTH_CHAT_DATASOURCE_DATABASENAME: CTH_CHAT
CTH_CHAT_DATASOURCE_HOST: mysqldb
CTH_CHAT_DATASOURCE_USERNAME: cth
CTH_CHAT_DATASOURCE_PASSWORD: root
CTH_CHAT_DATASOURCE_PORT: 3306
depends_on:
- mysqldb
mysqldb:
image: mysql/mysql-server:5.7
container_name: mysqldb
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
##here i am able to access dump file which I'll create two databases
with user created for me.
volumes:
- ./docker/provision/mysql/init:/docker-entrypoint-initdb.d
ports:
- '3308:3306'
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: cth
MYSQL_PASSWORD: root
you need to create a dump file refer the article [ https://github.com/abagayev/docker-bootstrap-collection/tree/master/mysql-few-databases]
你什么时候docker-compose up -d --build
您的应用程序在 运行 端口 8080 上启动,MySQL 数据库 运行 在主机中
谢谢!