在尝试使用Tomcat的PersistentManager将会话数据写入本地计算机上运行的Postgres DB时,我收到以下错误:
I received the following error when trying to write session data using Tomcat's PersistentManager to a Postgres DB running on my local machine:
SEVERE: A SQL exception occurred org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.应用程序本身运行在docker容器中。为了完整起见,我当前的context.xml文件是:
The application itself runs in a docker container. For completeness sake, my current context.xml file is:
<?xml version='1.0' encoding='utf-8'?> <Context> <Manager className="org.apache.catalina.session.PersistentManager" distributable="true" processExpiresFrequency="6" maxIdleBackup="0" debug="99" > <Store className="org.apache.catalina.session.JDBCStore" driverName="org.postgresql.Driver" connectionURL="jdbc:postgresql://localhost/admin?stringtype=unspecified" connectionName="admin" connectionPassword="admin" sessionAppCol="app_name" sessionDataCol="session_data" sessionIdCol="session_id" sessionLastAccessedCol="last_access" sessionMaxInactiveCol="max_inactive" sessionTable="tomcat_sessions_tb" sessionValidCol="valid_session" /> </Manager> </Context>根据以下建议: Postgresql:Connection被拒绝。检查主机名和端口是否正确,邮政主管是否接受TCP / IP连接
我通过netstat -aln | grep LISTEN Postgresql正在运行并侦听正确的端口:
I confirmed via a netstat -aln | grep LISTEN that Postgresql is running and listening on the correct ports:
tcp4 0 0 127.0.0.1.5432 *.* LISTEN tcp6 0 0 ::1.5432 *.* LISTEN而我的postgresql.conf在usr / local / var / postgres中)具有listen_addresses = localhost和port = 5432,它反映了Pgadmin3中正在运行的服务器的主机和端口。
and that my postgresql.conf (located in usr/local/var/postgres) has listen_addresses = localhost and port = 5432, which mirrors the host and port of my running server in Pgadmin3.
我怀疑问题是Docker在VM中运行,因此我获得的本地信息可能不是全部的故事。在线阅读可用的信息,似乎可能需要某种桥接网络,但我承认我是这方面的新手,我不确定如何设置。
I suspect that the problem is that Docker runs in a VM, and thus the local information I have obtained may not be the whole story. Reading up on the available information online, it seems that I may require some sort of bridged networking, however I admit I am a novice in this area, and I'm unsure of how to set it up.
推荐答案为什么我无法连接到localhost:5432?
Why I can NOT connect to localhost:5432?
您的容器的 / etc / hosts
$ sudo docker exec -it [container] cat /etc/hosts对于码头网络是 bridge 默认情况下, localhost 里面指向容器本身( Docker默认网桥)。 那么你没有在你的容器中收听 5432
For docker networks is bridge by default, the localhost inside points to container itself(Docker default bridge network). Then you don't have 5432 listening in your container:
$ sudo docker exec [container] nc -v -z localhost 5432
解决方案1.如果要在配置xml内部编写localhost:5432,最简单的方法是使用--net = host选项创建容器:
Solution 1. If you wanna hardcode the "localhost:5432" inside your config xml, the easiest way is creating your container with the option "--net=host":
$ sudo docker run --net=host -it ...解决方案2.更改 localhost 您的码头主机ip在容器内
Solution 2. Change the localhost of your docker host ip inside the container
- 获取您的 docker host ip :
解决方案3.修改db配置文件以使用别名而不是 localhost :
Solution 3. Modify your db config file to use an alias instead of localhost:
connectionURL="jdbc:postgresql://DB_ALIAS/admin?stringtype=unspecified"然后将 DB_ALIAS 添加到容器的主机中:
Then add the DB_ALIAS to the container's hosts :
$ sudo docker run --add-host DB_ALIAS:192.168.5.1 -it [image] ...
更多推荐
使用Docker启动Web应用程序,无法连接到Postgresql DB?
发布评论