我有 PostgreSQL 和 Solr,在一个 docker 容器中,在同一台机器上运行。我正在尝试将 Postgres 中的数据导入到 Solr,但我遇到了错误。以下是日志的相关部分:
Full Import failed:java.lang.RuntimeException: java.lang.RuntimeException: org.apache.solr.handler.dataimport.DataImportHandlerException: Unable to execute query: SELECT * FROM formulas
org.postgresql.util.PSQLException: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
基本上连接被拒绝/从未建立,但我很困惑。
netstat -nltp
产量
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 772/sshd
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 1822/postgres
tcp6 0 0 :::22 :::* LISTEN 772/sshd
tcp6 0 0 :::8983 :::* LISTEN 28508/docker-proxy
tcp6 0 0 ::1:5432 :::* LISTEN 1822/postgres
并说 Postgres 正在监听
127.0.0.1
.此外,在我的 pg_hba.conf
, 我有local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 md5
我相信这是允许连接的正确设置。
我负责数据导入设置的 solrconfig.xml 如下所示:
<dataConfig>
<dataSource driver="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1:5432/formulagrid"
user="myuser"
password="mypassword"/>
<document>
<entity name="formula" query="SELECT * FROM formulas">
<field column="formula_id" name="id" />
<field column="name" name="name" />
<field column="formula" name="formula" />
</entity>
</document>
</dataConfig>
我也试过地址
jdbc:postgresql://localhost:5432/formulagrid
.我不知道从这里还能去哪里。
最佳答案
基本上,您需要在容器上打开一个端口,例如:docker run -it -p '<OUTSIDE_PORT>:<INSIDE_PORT>' <IMAGE_NAME>
然后在 localhost 上,您可以连接到盒子的私有(private) IP 或公共(public) IP - 这是 solr 在容器内运行的端口。
要从容器内部向外连接,我会通过环境变量将 IP 和端口传递到容器中,例如:docker run -it -p '<OUTSIDE_PORT>:<INSIDE_PORT>' -e POSTGRESQL_ADDR=<IP>:<PORT> <IMAGE_NAME>
然后您可以使用 ENV['POSTGRESQL_ADDR'] 连接到容器外部。很可能会使用您的私有(private) IP 地址。
关于postgresql - 在 docker 容器内运行的 Solr 未连接到 PostgreSQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32023386/