无法从 Docker 连接到 postgres
can not connect to postgres from Docker
我有一个 docker 图像,其中我有一个主节点和两个从节点组成的 hadoop 集群。在这个集群上,我有 HBase。
我正在尝试使用 scoop 将安装在本地计算机上的 postgres 数据库迁移到 Docker 图像上的 HBase 数据库。
对于 postgres,这是我的 postgresql.conf
配置文件:
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart) port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart) unix_socket_directories = '/var/run/postgresql' # comma-separated list
of directories
# (change requires restart)
#unix_socket_group = '' # (change requires restart)
#unix_socket_permissions = 0777 # begin with 0 to use octal notation
# (change requires restart)
#bonjour = off # advertise server via Bonjour
# (change requires restart)
#bonjour_name = '' # defaults to the computer name
# - Security and Authentication -
#authentication_timeout = 1min # 1s-600s ssl = on
#ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
#ssl_prefer_server_ciphers = on
#ssl_ecdh_curve = 'prime256v1'
#ssl_dh_params_file = '' ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem' ssl_key_file =
'/etc/ssl/private/ssl-cert-snakeoil.key'
#ssl_ca_file = ''
#ssl_crl_file = ''
#password_encryption = md5 # md5 or scram-sha-256
#db_user_namespace = off
#row_security = on
# GSSAPI using Kerberos
#krb_server_keyfile = ''
#krb_caseins_users = off
# - TCP Keepalives -
# see "man 7 tcp" for details
#tcp_keepalives_idle = 0 # TCP_KEEPIDLE, in seconds;
# 0 selects the system default
#tcp_keepalives_interval = 0 # TCP_KEEPINTVL, in seconds;
# 0 selects the system default
#tcp_keepalives_count = 0 # TCP_KEEPCNT;
# 0 selects the system default
这也是 pg_hba.conf
文件的内容:
# Database administrative login by Unix domain socket local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only local all all trust
# IPv4 local connections: host all all 127.0.0.1/32 trust
# IPv6 local connections: host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128
我的问题是当我尝试使用 scoop 使用此命令从 docker 图像连接到 postgres 时:
sqoop import --connect jdbc:postgresql://localhost:5432/mimic --username postgres --password 0000 --table admission_ids --hbase-table mimic --column-family admission_ids --hbase-row-key id -m 1
我刚遇到这个问题:
Check that the hostname and port are correct and that the postmaster
is accepting TCP/IP connections. org.postgresql.util.PSQLException:
Connection refused. Check that the hostname and port are correct and
that the postmaster is accepting TCP/IP connections.
经过搜索,我才明白 docker 是这个问题的根源,为了确认这一点,我只是在第一时间尝试从我的本地计算机使用 tenlnet 连接到 postgres,这是结果:
telnet localhost 5432
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
第二次来自docker:
telnet localhost 5432
Trying 127.0.0.1...
Trying ::1...
telnet: Unable to connect to remote host: Cannot assign requested address
我的 docker 图像被命名为 spark-hadoop。
我尝试运行此命令但始终无法正常工作:
docker run -d --name bridgeToHadoop --publish=127.0.0.1:5432:5432 -p 172.18.0.2:5432:5432 spark-hadoop
主机上的 Postgresql 运行,您正尝试从 docker 容器连接它。执行此操作时,主机不应在连接字符串中为 localhost
。这里localhost的意思是docker容器。
用IP地址替换localhost,应该是下面的
sqoop import --connect jdbc:postgresql://host-pi-addr:5432/mimic --username postgres --password 0000 --table admission_ids --hbase-table mimic --column-family admission_ids --hbase-row-key id -m 1
Docker 为容器网络使用默认的 172.17.0.0/16 子网 IP 范围。您需要允许 non-local 个连接,因为您的容器网络不再被视为相对于 postgres 服务器的本地网络。要允许适当范围内的外部连接,请将以下内容添加到 pg_hba.conf
(对数据库、用户和方法进行适当的选择)。
#TYPE DATABASE USER ADDRESS METHOD
host all all 172.17.0.0/16 scram-sha-256`
此外,默认情况下 postgres 服务器仅侦听 localhost
,因此您还需要通过将以下内容添加到 postgresql.conf
.
来告诉 postgres 侦听其他 IP 地址
listen_addresses = '*'
我有一个 docker 图像,其中我有一个主节点和两个从节点组成的 hadoop 集群。在这个集群上,我有 HBase。
我正在尝试使用 scoop 将安装在本地计算机上的 postgres 数据库迁移到 Docker 图像上的 HBase 数据库。
对于 postgres,这是我的 postgresql.conf
配置文件:
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart) port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart) unix_socket_directories = '/var/run/postgresql' # comma-separated list
of directories
# (change requires restart)
#unix_socket_group = '' # (change requires restart)
#unix_socket_permissions = 0777 # begin with 0 to use octal notation
# (change requires restart)
#bonjour = off # advertise server via Bonjour
# (change requires restart)
#bonjour_name = '' # defaults to the computer name
# - Security and Authentication -
#authentication_timeout = 1min # 1s-600s ssl = on
#ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
#ssl_prefer_server_ciphers = on
#ssl_ecdh_curve = 'prime256v1'
#ssl_dh_params_file = '' ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem' ssl_key_file =
'/etc/ssl/private/ssl-cert-snakeoil.key'
#ssl_ca_file = ''
#ssl_crl_file = ''
#password_encryption = md5 # md5 or scram-sha-256
#db_user_namespace = off
#row_security = on
# GSSAPI using Kerberos
#krb_server_keyfile = ''
#krb_caseins_users = off
# - TCP Keepalives -
# see "man 7 tcp" for details
#tcp_keepalives_idle = 0 # TCP_KEEPIDLE, in seconds;
# 0 selects the system default
#tcp_keepalives_interval = 0 # TCP_KEEPINTVL, in seconds;
# 0 selects the system default
#tcp_keepalives_count = 0 # TCP_KEEPCNT;
# 0 selects the system default
这也是 pg_hba.conf
文件的内容:
# Database administrative login by Unix domain socket local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only local all all trust
# IPv4 local connections: host all all 127.0.0.1/32 trust
# IPv6 local connections: host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128
我的问题是当我尝试使用 scoop 使用此命令从 docker 图像连接到 postgres 时:
sqoop import --connect jdbc:postgresql://localhost:5432/mimic --username postgres --password 0000 --table admission_ids --hbase-table mimic --column-family admission_ids --hbase-row-key id -m 1
我刚遇到这个问题:
Check that the hostname and port are correct and that the postmaster
is accepting TCP/IP connections. org.postgresql.util.PSQLException:
Connection refused. Check that the hostname and port are correct and
that the postmaster is accepting TCP/IP connections.
经过搜索,我才明白 docker 是这个问题的根源,为了确认这一点,我只是在第一时间尝试从我的本地计算机使用 tenlnet 连接到 postgres,这是结果:
telnet localhost 5432
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
第二次来自docker:
telnet localhost 5432
Trying 127.0.0.1...
Trying ::1...
telnet: Unable to connect to remote host: Cannot assign requested address
我的 docker 图像被命名为 spark-hadoop。
我尝试运行此命令但始终无法正常工作:
docker run -d --name bridgeToHadoop --publish=127.0.0.1:5432:5432 -p 172.18.0.2:5432:5432 spark-hadoop
主机上的 Postgresql 运行,您正尝试从 docker 容器连接它。执行此操作时,主机不应在连接字符串中为 localhost
。这里localhost的意思是docker容器。
用IP地址替换localhost,应该是下面的
sqoop import --connect jdbc:postgresql://host-pi-addr:5432/mimic --username postgres --password 0000 --table admission_ids --hbase-table mimic --column-family admission_ids --hbase-row-key id -m 1
Docker 为容器网络使用默认的 172.17.0.0/16 子网 IP 范围。您需要允许 non-local 个连接,因为您的容器网络不再被视为相对于 postgres 服务器的本地网络。要允许适当范围内的外部连接,请将以下内容添加到 pg_hba.conf
(对数据库、用户和方法进行适当的选择)。
#TYPE DATABASE USER ADDRESS METHOD
host all all 172.17.0.0/16 scram-sha-256`
此外,默认情况下 postgres 服务器仅侦听 localhost
,因此您还需要通过将以下内容添加到 postgresql.conf
.
listen_addresses = '*'