无法将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

这是我的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

在您的容器中,本地主机设计您的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