无法将 MySQL 容器连接到 docker 中的 Tomcat 容器

Unable to Connect MySQL container to Tomcat Container in docker(无法将 MySQL 容器连接到 docker 中的 Tomcat 容器)
本文介绍了无法将 MySQL 容器连接到 docker 中的 Tomcat 容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计划

我希望我的 tomcat 服务器能够在单独的容器中连接到我的 MySQL 服务器.

I want my tomcat server to be able to connect to my MySQL server both in separate containers.

问题

Tomcat 无法连接 MySQL

Tomcat cannot connect to MySQL

我使用了 wordpress 教程中关于设置与mysql 容器并创建了到 MySQL 的链接.

I used some of the details from the wordpress tutorial about setting up a link with the mysql container and created the link to the MySQL.

虽然 tomcat 和 mysql 启动得很好,但我似乎无法让 tomcat 连接到 MySQL,但这些设置在我的本地机器上运行得很好.

Although the tomcat and mysql spin up just fine I can't seem to get tomcat to be able to connect to MySQL, the settings work on my local machine perfectly fine.

我也尝试使用 --net: "host" 尽管这不适用于 Tomcat,因为它会引发严重错误.

I've attempted to use --net: "host" as well although that does not work with Tomcat as it throws a severe error.

以前的答案

我在这篇文章中注意到了大量可能的错误修复,尽管我不相信其中任何一个会转化为我的问题,因为我相信这是一个 docker 问题而不是主机问题.

I noticed on this post a load of possible fixes for the error although I don't believe any of these would translate to my problem as I believe this is a docker problem not a host one.

docker-compose.yml

web:
  image: tomcat:7.0
  container_name: tomcat-container
  ports:
   - "80:8080"
  hostname: docker-tomcat
  volumes:
   - /home/webapps:/usr/local/tomcat/webapps
  links:
   - db
db:
  image: mysql
  container_name: mysql-container
  environment:
   MYSQL_ROOT_PASSWORD: Mysqlpassword1
   MYSQL_DATABASE: tracker
  volumes:
   - /home/mysqlDB:/var/lib/mysql

这是我来自 tomcat 的 Context.xml.

This is my Context.xml from tomcat.

<Context>
    <Resource
        name="jdbc/tracker" type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000" 
        url="jdbc:mysql://localhost:3306/tracker?useSSL=false"
        driverClassName="com.mysql.jdbc.Driver"
        username="root" password="mysqladmin1"
    />
    <Resource
        name="jdbc/jenkins" type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000" 
        url="jdbc:mysql://localhost:3306/jenkins?useSSL=false"
        driverClassName="com.mysql.jdbc.Driver"
        username="root" password="mysqladmin1"
    />
</Context>

错误代码.

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at databaseConnections.SQLDatabaseConnection.tableExists(SQLDatabaseConnection.java:131)
at databaseConnections.JiraSQLDatabaseConnection.<init>(JiraSQLDatabaseConnection.java:50)

推荐答案

当您将 db 链接为db"时,您不能使用 localhost 加入您的数据库.你应该db"

As you're linking db as "db", you cannot use localhost to join you database. you should "db"

jdbc:mysql://db:3306/tracker?useSSL=false

在您的容器中,localhost 设计您的 tomcat 容器,而不是您的主机.MySQL容器有自己的网络.

In your container, localhost design your tomcat container, not your host. MySQL container has his own network.

另外,如果你不喜欢db"这个名字,你可以用不同的名字来命名链接

Futhermore, if you don't like "db" name, you can name link it with different name

例如:

 links:
   - db:container-mysql

在这种情况下,在你的 tomcat 容器中,你可以使用

In this case, inside you tomcat container, you could use

jdbc:mysql://container-mysql:3306/tracker?useSSL=false

这篇关于无法将 MySQL 容器连接到 docker 中的 Tomcat 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)