在docker-compose中连接tomcat和mysql

我正在研究项目。现在我处于将项目移至Docker容器的阶段。我在我的网站上放置了tomcat Tomcat,并在数据库中使用了mysql做docker-

compose.yml。不幸的是,tomcat中的类无法与数据库连接


回答:

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;

}

}


回答:

FROM tomcat:8.0.41-jre8

COPY ./webapp /usr/local/tomcat/webapps/webapp

CMD ["catalina.sh", "run"]


回答:

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上。
  • 我为拼写错误表示歉意,但我来自波兰,所以英语不是我的母语

回答:

通过compose文件,您的数据库服务将通过可以在网络上引用db。除非您只需要为容器使用其他名称,否则无需使用links关键字。如果更改您的Java代码以连接到jdbc:mysql://db:3306/uczelnia?useTimezone=true&serverTimezone=UTC它,则应该可以正常工作。

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

以上是 在docker-compose中连接tomcat和mysql 的全部内容, 来源链接: utcz.com/qa/405425.html

回到顶部