在docker-compose中连接tomcat和mysql

Posted

技术标签:

【中文标题】在docker-compose中连接tomcat和mysql【英文标题】:Connect tomcat and mysql in docker-compose 【发布时间】:2019-01-31 07:43:09 【问题描述】:

我正在研究项目。现在我正处于将项目转移到 docker 容器的阶段。我在我的网站上放置了tomcat,在数据库中放置了mysql做docker-compose.yml。不幸的是,来自 tomcat 的类无法连接到数据库


Java 代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Waliduj 

String url="jdbc:mysql://localhost:3306/uczelnia?useTimezone=true&serverTimezone=UTC";
String username="root";
String password="121212";

public boolean Waliduje(String uname,String pass) 


    try 
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(url,username,password);
        PreparedStatement st=con.prepareStatement("SELECT email,haslo FROM logowanie WHERE email=? AND haslo=?");
        st.setString(1,uname);
        st.setString(2,pass);
        ResultSet rs = st.executeQuery();
        if(rs.next()) 
            return true;
        
     catch (SQLException e) 
        e.printStackTrace();
     catch (ClassNotFoundException se) 
        // TODO Auto-generated catch block
        se.printStackTrace();
    

    return false;



Dockerfile 代码

FROM tomcat:8.0.41-jre8
COPY ./webapp /usr/local/tomcat/webapps/webapp
CMD ["catalina.sh", "run"]

docker-compose.yml 代码

version: '3'
services: 
    web:
        build: .
        ports:
         - "8080:8080"
        links:
         - "db:uczelnia"
    db:
        image: mysql
        environment:
         MYSQL_ROOT_PASSWORD: 121212
         MYSQL_DATABASE: uczelnia

        ports:
         - "3306:3306" 
    phpMyAdmin:
        image: nazarpc/phpmyadmin
        ports:
         - "8090:80"
        links:
         - db:mysql

错误代码

web_1         | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
web_1         |
web_1         | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
web_1         |         at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172)
web_1         |         at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
web_1         |         at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
web_1         |         at java.sql.DriverManager.getConnection(DriverManager.java:664)
web_1         |         at java.sql.DriverManager.getConnection(DriverManager.java:247)
web_1         |         at com.login.walidacja.Waliduj.Waliduje(Waliduj.java:20)
web_1         |         at com.login.Login.doPost(Login.java:27)
web_1         |         at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
web_1         |         at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
web_1         |         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
web_1         |         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
web_1         |         at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
web_1         |         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
web_1         |         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
web_1         |         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
web_1         |         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
web_1         |         at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
web_1         |         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
web_1         |         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
web_1         |         at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
web_1         |         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
web_1         |         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509)
web_1         |         at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
web_1         |         at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
web_1         |         at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
web_1         |         at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)
web_1         |         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
web_1         |         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
web_1         |         at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
web_1         |         at java.lang.Thread.run(Thread.java:745)
web_1         | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
web_1         |
web_1         | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
web_1         |         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
web_1         |         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
web_1         |         at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
web_1         |         at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
web_1         |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
web_1         |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:103)
web_1         |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:149)
web_1         |         at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:165)
web_1         |         at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:92)
web_1         |         at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:982)
web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
web_1         |         ... 30 more
web_1         | Caused by: java.net.ConnectException: Connection refused (Connection refused)
web_1         |         at java.net.PlainSocketImpl.socketConnect(Native Method)
web_1         |         at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
web_1         |         at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
web_1         |         at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
web_1         |         at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
web_1         |         at java.net.Socket.connect(Socket.java:589)
web_1         |         at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:173)
web_1         |         at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:66)
web_1         |         ... 33 more

附加信息:

我正在使用 Windows 10,所以 docker 也在 Windows 上。 我为拼写错误道歉,但我来自波兰,所以英语不是 我的

【问题讨论】:

快速提问,数据库的 URL 是否总是设置为 localhost 【参考方案1】:

通过撰写文件,您的数据库服务将可通过db 在网络上引用。除非您只是需要一个不同的容器名称,否则不需要使用 links 关键字。如果您更改 Java 代码以连接到 jdbc:mysql://db:3306/uczelnia?useTimezone=true&amp;serverTimezone=UTC,它应该可以按预期工作。

https://docs.docker.com/compose/networking/

【讨论】:

谢谢,就是这样。现在我只需要将 ftp 服务器添加到 docker-compose。

以上是关于在docker-compose中连接tomcat和mysql的主要内容,如果未能解决你的问题,请参考以下文章

在 docker-compose 中挂载 NFS 卷时连接超时

使用docker部署nginx+tomcat架构:使用docker-compose简化部署操作

使用 docker-compose 无法在同一网络中连接 mysql 和 spring boot

在 docker-compose 中使用 MySQL 初始化 Hibernate 时连接被拒绝

简单的docker-compose编写介绍(包含 nginx+tomcat 实例演示)

简单的docker-compose编写介绍(包含 nginx+tomcat 实例演示)