在docker容器中运行的Solr没有连接到PostgreSQL数据库
我有一个Docker容器中的PostgreSQL和Solr,运行在同一台机器上。 我正在尝试从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>
然后在本地主机上,您可以连接到专用IP或公共IP的方框 – 该是在容器内运行solr的端口。
要从容器内部进行连接,我将通过环境variables将IP和端口传递到容器中,如下所示:
docker run -it -p '<OUTSIDE_PORT>:<INSIDE_PORT>' -e POSTGRESQL_ADDR=<IP>:<PORT> <IMAGE_NAME>
然后,您可以使用ENV ['POSTGRESQL_ADDR']在容器外部进行连接。 最有可能的是,你的私人IP地址将被使用。