connecttomcat与mysql在docker中的组合

nhn9ugyo  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(276)

我在研究这个项目。现在我正处于将项目转移到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上。
我为拼写错误道歉,但我来自波兰,所以英语不是我的母语

8cdiaqws

8cdiaqws1#

通过compose文件,您的数据库服务将通过 db . links关键字是不必要的,除非你只是需要一个不同的容器名称。如果您更改java代码以连接到 jdbc:mysql://db:3306/uczelnia?useTimezone=true&serverTimezone=UTC 它应该像预期的那样工作。
https://docs.docker.com/compose/networking/

相关问题