从另一个docker容器连接到数据库 [英] Connect to database from another docker container
问题描述
我用这个命令运行phpmyadmin容器:
docker run -d -p 49160:22 -p 49161: 80 -p 49162:3306 - name db phpmyadmin:imported
我可以看到我的phpmyadmin浏览器在端口49161.我使用此命令运行tomcat容器
docker run -it -v〜/ docker / tomcat / tomcat -users.xml:/usr/local/tomcat/conf/tomcat-users.xml --name tomcat --link db:server -p 8888:8080 tomcat:deployment
每件事情都看起来不错。我可以从tomcat容器ping数据库。但是当我尝试使用hibernate和我的J2EE应用程序连接到它时,我得到以下错误。
2015年6月10日19 :20:12.293警告[http-nio-8080-exec-15] org.hibernate.cfg.SettingsFactory.buildSettings无法获取连接元数据
java.sql.SQLException:连接可能不从基础数据库中获取!
在com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
在com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:690)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
在org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:56)
这是我的hibernate.cfg.xml:
<?xml version =1.0encoding =UTF-8?>
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate配置DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd\">
< hibernate-configuration>
< session-factory>
< property name =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / property>
< property name =hibernate.connection.driver_class> com.mysql.jdbc.Driver< / property>
< property name =hibernate.connection.url> jdbc:mysql://172.17.0.20:3306 / auction< / property>
< property name =hibernate.connection.username> root< / property>
< property name =hibernate.show_sql> true< / property>
< property name =hibernate.query.factory_class> org.hibernate.hql.classic.ClassicQueryTranslatorFactory< / property>
< property name =hibernate.connection.provider_class> org.hibernate.connection.C3P0ConnectionProvider< / property>
< mapping class =ie.domain.entity.User/>
< mapping class =ie.domain.entity.Auction/>
< mapping class =ie.domain.entity.Offer/>
< / session-factory>
< / hibernate-configuration>
我尝试使用名称服务器而不是IP,我也试过端口49162而不是3306我得到了相同的错误。
尝试连接:
jdbc:mysql:// server:3306 / auction
端口映射在主机上,所以在直接与容器通话时不要使用它们。 docker链接功能会将名称server添加到具有容器IP地址的 / etc / hosts
中,因此您可以按名称引用它。
更新:根据 https:/ /registry.hub.docker.com/u/wnameless/mysql-phpmyadmin/ ,这不会起作用,因为服务器只侦听127.0.0.1,而不是0.0.0.0。
I ran two docker containers one is tomcat and one is phpmyadmin. I ran the phpmyadmin container with this command:
docker run -d -p 49160:22 -p 49161:80 -p 49162:3306 --name db phpmyadmin:imported
And I can see the phpmyadmin on my browser on port 49161. I ran the tomcat container with this command
docker run -it -v ~/docker/tomcat/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml --name tomcat --link db:server -p 8888:8080 tomcat:deployed
Every thing looks fine. I can ping the db from the tomcat container. but when I try to connect to it using hibernate and my J2EE application I get the following error.
10-Jun-2015 19:20:12.293 WARNING [http-nio-8080-exec-15] org.hibernate.cfg.SettingsFactory.buildSettings Could not obtain connection metadata
java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:690)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:56)
This is my hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://172.17.0.20:3306/auction</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<mapping class="ie.domain.entity.User"/>
<mapping class="ie.domain.entity.Auction"/>
<mapping class="ie.domain.entity.Offer"/>
</session-factory>
</hibernate-configuration>
I tried to use the name server instead of the IP and also I tried the port 49162 instead of 3306 and I got the same error.
Try connecting with:
jdbc:mysql://server:3306/auction
The port mappings are on the host, so you don't use them when talking directly to the container. The docker link functionality will add the name "server" to /etc/hosts
with container IP address, so you can just reference it by name.
UPDATE: Judging from the comment on https://registry.hub.docker.com/u/wnameless/mysql-phpmyadmin/, this isn't going to work as the server is only listening on 127.0.0.1, not 0.0.0.0.
这篇关于从另一个docker容器连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!