Docker
為什麼我無法連接到 docker 中的 postgres?
我使用了這個項目中的 docker-compose 。兩個 docker 容器都成功啟動。
kshnkvn@kshnkvn-vb:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 10fafbab73dc openpoiservice_gunicorn_flask "/ops_venv/bin/gunic…" 23 minutes ago Up 22 minutes 0.0.0.0:5000->5000/tcp openpoiservice_gunicorn_flask_1 a66fe5691455 kartoza/postgis:11.0-2.5 "/bin/sh -c /docker-…" 23 minutes ago Up 22 minutes 5432/tcp openpoiservice_psql_postgis_db_1
但是當試圖檢查服務的功能時 - 他無法連接到數據庫。我嘗試手動進行:
kshnkvn@kshnkvn-vb:~$ docker exec -it 10fafbab73dc /bin/bash root@10fafbab73dc:/deploy/app# psql -h localhost -U gis_admin-gis psql: could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Cannot assign requested address Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? root@10fafbab73dc:/deploy/app#
奇怪,檢查以防容器網路的類型是網橋:
kshnkvn@kshnkvn-vb:~$ docker network ls NETWORK ID NAME DRIVER SCOPE 81001dac99c0 bridge bridge local 8e65fb4ef6f8 host host local 94ce4e1605ef none null local a3f48ac3facc openpoiservice_default bridge local e3d4286df013 openpoiservice_poi_network bridge local
檢查 postgres 啟動日誌:
kshnkvn@kshnkvn-vb:~$ docker logs a66fe5691455 Add rule to pg_hba: 0.0.0.0/0 Add rule to pg_hba: replication replicator Setup master database psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? 2020-02-08 13:50:20.675 UTC [25] LOG: listening on IPv4 address "127.0.0.1", port 5432 2020-02-08 13:50:20.683 UTC [25] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2020-02-08 13:50:20.756 UTC [37] LOG: database system was interrupted; last known up at 2020-02-08 13:35:17 UTC 2020-02-08 13:50:21.830 UTC [48] postgres@postgres FATAL: the database system is starting up psql: FATAL: the database system is starting up 2020-02-08 13:50:22.726 UTC [37] LOG: database system was not properly shut down; automatic recovery in progress 2020-02-08 13:50:22.730 UTC [37] LOG: redo starts at 0/21CCC50 2020-02-08 13:50:22.730 UTC [37] LOG: invalid record length at 0/21CCC88: wanted 24, got 0 2020-02-08 13:50:22.730 UTC [37] LOG: redo done at 0/21CCC50 2020-02-08 13:50:22.867 UTC [25] LOG: database system is ready to accept connections List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+-----------+----------+---------+---------+----------------------- gis | gis_admin | UTF8 | C.UTF-8 | C.UTF-8 | postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 | template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (4 rows) postgres ready Setup postgres User:Password Creating superuser gis_admin ALTER ROLE Creating replication user replicator ALTER ROLE gis db already exists List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+-----------+----------+---------+---------+----------------------- gis | gis_admin | UTF8 | C.UTF-8 | C.UTF-8 | postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 | template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (4 rows) 2020-02-08 13:50:24.785 UTC [25] LOG: received smart shutdown request 2020-02-08 13:50:24.799 UTC [25] LOG: background worker "logical replication launcher" (PID 58) exited with exit code 1 2020-02-08 13:50:24.801 UTC [53] LOG: shutting down 2020-02-08 13:50:24.838 UTC [25] LOG: database system is shut down Postgres initialisation process completed .... restarting in foreground 2020-02-08 13:50:25.842 UTC [148] LOG: listening on IPv4 address "0.0.0.0", port 5432 2020-02-08 13:50:25.842 UTC [148] LOG: listening on IPv6 address "::", port 5432 2020-02-08 13:50:25.850 UTC [148] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2020-02-08 13:50:25.880 UTC [150] LOG: database system was shut down at 2020-02-08 13:50:24 UTC 2020-02-08 13:50:25.887 UTC [148] LOG: database system is ready to accept connections
看起來 postgre 從 ip 0.0.0.0 開始
我查看了 docker
ip addr show
命令使用的 ip。嘗試使用此 ip 重新連接:psql: could not connect to server: Connection refused Is the server running on host "172.17.0.1" and accepting TCP/IP connections on port 5432? root@10fafbab73dc:/deploy/app# psql -h 172.17.255.255 -U gis_admin-gis psql: could not connect to server: Connection timed out Is the server running on host "172.17.255.255" and accepting TCP/IP connections on port 5432?
我可以嘗試做些什麼來將腳本連接到數據庫?
TLDR;
psql -h psql_postgis_db -U gis_admin gis # or psql -h psql_postgis_db gis gis_admin
伺服器地址有問題
您嘗試的所有 IP 都不正確。
- 127.0.0.1 是本地主機地址。由於您從燒瓶容器啟動命令,因此沒有執行 posgres 服務。
- 172.17.0.1 是 docker 網橋的 ip。這實際上是您的 docker 引擎主機的 ip,正如您在同一網橋上的容器所看到的那樣。除非你在你的機器上執行一個 postgres 並監聽那個 ip,否則你不會得到任何答案(而且這無論如何都不是正確的 postgres 伺服器)
- 172.17.255.255是之前橋接網路的網路廣播地址。
從您的啟動日誌中,我們可以看到您的 postgres 應該正確監聽。0.0.0.0 實際上並不是真正的 ip:它代表“在此主機上配置的任何 ip”。
您可以查找您的 postgres 容器的 ip 來聯繫它(例如參見SO 上的這個答案),但您甚至不必這樣做。docker/docker-compose 通過自動將同一網路上的容器/服務名稱映射到它們各自的 IP,使您可以輕鬆完成此操作。所以您的數據庫伺服器可以使用服務名稱訪問
psql_postgis_db
使用者名和數據庫名有問題
我真的不明白你在命令
-U
選項中寫的內容psql
(使用者+數據庫名稱的混合……)。無論如何,它應該是你想用來連接到 postgres 伺服器的使用者名。從您的撰寫文件中,它是gis_admin
. 由於沒有與使用者同名的數據庫,因此您需要指定要連接的數據庫名稱。您可以使用-U
option(psql -U <user> <db>
) 或使用位置參數 (psql <db> <user>
)