简体   繁体   English

(psycopg2.OperationalError)无法连接到服务器:连接被拒绝是服务器

[英](psycopg2.OperationalError) could not connect to server: Connection refused Is the server

I am trying to connect two docker containers, one posgresql and the other a python flask application. 我正在尝试连接两个docker容器,一个posgresql,另一个连接python flask应用程序。 both are linking correctly, all connection variables in the python app are taken directly from the ones in the postgres container that are exposed via linking and are identical to those found when inspecting the postgresql container. 两者都正确链接,python应用程序中的所有连接变量都是直接从postgres容器中通过链接公开的变量获取的,并且与检查postgresql容器时发现的变量相同。 when I use psql with the exact parameters from the connection string ie: 当我将psql与连接字符串中的确切参数一起使用时,即:

psql -p 5432 -h 172.17.0.2 -d mydb -U user

connection to the database in the postgres container is successful, so I know that postgres is communicating correctly over the specified ports etc. however, when I attempt to connect to the database via flask with the same connection variables I get this error: 成功连接到postgres容器中的数据库,因此我知道postgres通过指定的端口等正确通信。但是,当我尝试通过使用相同连接变量的flask连接到数据库时,出现此错误:

Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1122, in _do_get return self._pool.get(wait, self._timeout) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/queue.py", line 145, in get raise Empty sqlalchemy.util.queue.Empty 追溯(最近一次调用):文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py”,行1122,在_do_get中返回self._pool.get(wait,self._timeout)在第145行的文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/util/queue.py”中,清空sqlalchemy.util.queue.Empty

During handling of the above exception, another exception occurred: 在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2138, in _wrap_pool_connect return fn() File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 328, in unique_connection return _ConnectionFairy._checkout(self) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 766, in _checkout fairy = _ConnectionRecord.checkout(pool) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 516, in checkout rec = pool._do_get() File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1138, in _do_get self._dec_overflow() File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 60, in exit compat.reraise(exc_type, exc_value, exc_tb) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise raise value File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1135, in _do_get return self._create_connectio 追溯(最近一次呼叫最近):文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py”,第2138行,在_wrap_pool_connect中返回fn()文件“ / usr / local / lib / python3.6 / site-packages / sqlalchemy / pool.py”,第328行,在unique_connection中返回_ConnectionFairy._checkout(self)文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/pool”。 py”,第766行,在_checkout fairy = _ConnectionRecord.checkout(pool)文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py”,第516行,在check rec rec = pool._do_get ()在_do_get self._dec_overflow()中的文件1138行中的文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py”()文件“ /usr/local/lib/python3.6/site -packages / sqlalchemy / util / langhelpers.py”,第60行,位于出口 compat.reraise(exc_type,exc_value,exc_tb)文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat”中。 py”,第187行,在提高价值文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py”的第1135行,在_do_get中返回self._create_connectio n() File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 333, in _create_connection return _ConnectionRecord(self) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 461, in init self. n()文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py”,行333,在_create_connection中返回_ConnectionRecord(self)文件“ /usr/local/lib/python3.6/ init自我中的“ site-packages / sqlalchemy / pool.py”行461。 connect(first_connect_check=True) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 651, in __connect connection = pool._invoke_creator(self) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 105, in connect return dialect.connect(*cargs, **cparams) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 393, in connect return self.dbapi.connect(*cargs, **cparams) File "/usr/local/lib/python3.6/site-packages/psycopg2/__init .py", line 164, in connect conn = _connect(dsn, connection_factory=connection_factory, async=async) psycopg2.OperationalError: could not connect to server: Connection refused Is the server running on host "172.17.0.2" and accepting TCP/IP connections on port 5432? connect(first_connect_check = True)文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py”,第651行,位于__connect connection = pool._invoke_creator(self)文件“ / usr / local / lib / python3.6 / site-packages / sqlalchemy / engine / strategies.py“,第105行,在connect返回方言.connect(* cargs,** cparams)文件” /usr/local/lib/python3.6/site -packages / sqlalchemy / engine / default.py“,连接中的第393行返回self.dbapi.connect(* cargs,** cparams)文件” /usr/local/lib/python3.6/site-packages/psycopg2/ __init .py“,行164,在connect conn = _connect(dsn,connection_factory = connection_factory,async = async)psycopg2.OperationalError:无法连接到服务器:连接被拒绝服务器是否在主机“ 172.17.0.2”上运行并接受TCP / IP连接5432上的端口?

The above exception was the direct cause of the following exception: 上面的异常是以下异常的直接原因:

Traceback (most recent call last): File "./wsgi.py", line 14, in app = getModule("/var/www", name).app File "./wsgi.py", line 5, in getModule return imp.load_source(module, ("%s/%s.py" % (path, name))) File "/usr/local/lib/python3.6/imp.py", line 172, in load_source module = _load(spec) File "/var/www/backend.py", line 3, in import json, logging, db, os File "/var/www/db.py", line 36, in engine.connect() File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2082, in connect return self._connection_cls(self, **kwargs) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 90, in init if connection is not None else engine.raw_connection() File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2168, in raw_connection self.pool.unique_connection, _connection) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2142, in _wrap_pool_connect e, dialect, self) File "/usr/local/lib/python3.6/site-pack 追溯(最近一次通话最近):app = getModule(“ / var / www”,name)中的文件“ ./wsgi.py”,第14行,getModule中的文件“ ./wsgi.py”,第5行返回imp.load_source(module,(“%s /%s.py”%(路径,名称)))文件“ /usr/local/lib/python3.6/imp.py”,行172,在load_source模块中= _load(spec)文件“ /var/www/backend.py”,第3行,在导入json,日志记录,数据库,操作系统中文件“ /var/www/db.py”,第36行,在engine.connect()文件中“ /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py”,第2082行,在connect中返回self._connection_cls(self,** kwargs)文件“ / usr / local / lib / python3.6 / site-packages / sqlalchemy / engine / base.py“,第90行,如果连接不是init ,则在init初始化 engine.raw_connection()文件” /usr/local/lib/python3.6/site-packages/ sqlalchemy / engine / base.py”,第2168行,在raw_connection self.pool.unique_connection,_connection中)文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py”,第2142行,在_wrap_pool_connect e中,方言,自身)文件“ /usr/local/lib/python3.6/site-pack ages/sqlalchemy/engine/base.py", line 1456, in _handle_dbapi_exception_noconnection exc_info File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause reraise(type(exception), exception, tb=exc_tb, cause=cause) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 186, in reraise raise value.with_traceback(tb) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2138, in _wrap_pool_connect return fn() File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 328, in unique_connection return _ConnectionFairy._checkout(self) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 766, in _checkout fairy = _ConnectionRecord.checkout(pool) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 516, in checkout rec = pool._do_get() File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1138, in _do_get self._dec_overflow() File "/usr/local _handle_dbapi_exception_noconnection exc_info文件中的“ ages / sqlalchemy / engine / base.py”,第1456行,exc_info文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py”,第203行,在raise_from_cause中引发(类型(异常),异常,tb = exc_tb,原因=原因)文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py”,第186行,重新提高价值。with_traceback (tb)_wrap_pool_connect中的文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py”,第2138行返回fn()文件“ /usr/local/lib/python3.6 /site-packages/sqlalchemy/pool.py”,第328行,在unique_connection中返回_ConnectionFairy._checkout(self)文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py”,第766行,在_checkout fairy = _ConnectionRecord.checkout(pool)文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py”中的第516行,在checkout rec = pool._do_get()文件“ / usr / local / lib / python3.6 / site-packages / sqlalchemy / pool.py“,第1138行,位于_do_get self._dec_overflow()文件” / usr / local /lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 60, in exit compat.reraise(exc_type, exc_value, exc_tb) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise raise value File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1135, in _do_get return self._create_connection() File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 333, in _create_connection return _ConnectionRecord(self) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 461, in init self. /lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py“,第60行,位于出口 compat.reraise(exc_type,exc_value,exc_tb)文件“ /usr/local/lib/python3.6/site- “ packages / sqlalchemy / util / compat.py”,第187行,重新提高价值_do_get中的“ /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py”,行1135,返回自身。 _create_connection()文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py”,行333,在_create_connection中返回_ConnectionRecord(self)文件“ /usr/local/lib/python3.6/ init自我中的“ site-packages / sqlalchemy / pool.py”行461。 connect(first_connect_check=True) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 651, in __connect connection = pool._invoke_creator(self) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 105, in connect return dialect.connect(*cargs, **cparams) File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 393, in connect return self.dbapi.connect(*cargs, **cparams) File "/usr/local/lib/python3.6/site-packages/psycopg2/__init .py", line 164, in connect conn = _connect(dsn, connection_factory=connection_factory, async=async) sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused Is the server running on host "172.17.0.2" and accepting TCP/IP connections on port 5432? connect(first_connect_check = True)文件“ /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py”,第651行,位于__connect connection = pool._invoke_creator(self)文件“ / usr / local / lib / python3.6 / site-packages / sqlalchemy / engine / strategies.py“,第105行,在connect返回方言.connect(* cargs,** cparams)文件” /usr/local/lib/python3.6/site -packages / sqlalchemy / engine / default.py“,连接中的第393行返回self.dbapi.connect(* cargs,** cparams)文件” /usr/local/lib/python3.6/site-packages/psycopg2/ __init .py“,行164,在connect conn = _connect(dsn,connection_factory = connection_factory,async = async)sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)无法连接到服务器:连接被拒绝服务器在主机上运行吗? 172.17.0.2”并接受端口5432上的TCP / IP连接?

below is the code in my flask app leading up to the exception: 以下是我的烧瓶应用程序中导致异常的代码:

import os, logging, json
from datetime import datetime
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import BigInteger, Integer, String, DateTime, Column, Table, ForeignKey, create_engine
from sqlalchemy.dialects.postgresql import JSON
from sqlalchemy.orm import scoped_session, sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base

user = os.environ.get('DB_USER','user')
password = os.environ.get('DB_PASSWORD','password')
address = os.environ.get('DB_ADDR','0.0.0.0')
port = os.environ.get('DB_PORT','')
name = os.environ.get('DB_NAME','')

db_url = "postgresql://%s:%s@%s:%s/%s" % (user, password, address, port, name) 

engine = create_engine(db_url, convert_unicode=True, client_encoding='utf8')

print (engine)

engine.connect()

when I log the "engine" I get this: 当我登录“引擎”时,我得到以下信息:

Engine(postgresql://user:***@172.17.0.2:5432/mydb)

which again matches the parameters used in succesful database connection with the database from my host computer. 它再次匹配成功数据库连接到我的主机上的数据库所使用的参数。

I'm assuming there must be something wrong with my python code since the docker containers appear by all accounts to be working correctly. 我假设我的python代码一定有问题,因为所有帐户都显示docker容器正常工作。

any help would be much appreciated, thank you. 任何帮助将不胜感激,谢谢。

所以我的代码实际上没问题,发生的是一种竞争情况,其中python在数据库完全初始化并接受连接之前发送连接请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker- django 在连接到 postgres 时抛出错误:psycopg2.OperationalError:无法连接到服务器:连接被拒绝 - Docker- django throws error while connecting to postgres: psycopg2.OperationalError: could not connect to server: Connection refused (psycopg2.OperationalError) 连接到位于“本地主机”(::1)、端口 5432 的服务器失败:致命:数据库“玩家”不存在 - (psycopg2.OperationalError) connection to server at "localhost" (::1), port 5432 failed: FATAL: database "players" does not exist sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) 连接到服务器 at127.0.0.1,端口 5432 失败:致命:密码验证失败 - sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at127.0.0.1, port 5432 failed:FATAL:password authentication failed 流浪者上的Django,psycopg2 OperationalError:无法连接到服务器:没有这样的文件或目录 - Django on vagrant, psycopg2 OperationalError: could not connect to server: No such file or directory Docker/Django 应用程序中的 Postgres 不起作用 - OperationalError:无法连接到服务器:连接被拒绝 - Postgres in Docker/ Django app does not work - OperationalError: could not connect to server: Connection refused 得到 django.db.utils.OperationalError:无法连接到服务器:连接被拒绝 - Got django.db.utils.OperationalError: could not connect to server: Connection refused psycopg2.OperationalError 错误 - psycopg2.OperationalError errors Docker Django 无法连接到服务器:连接被拒绝 - Docker Django could not connect to server: Connection refused psycopg2.OperationalError:致命:不支持的前端协议 1234.5679:服务器支持 2.0 到 3.0 - psycopg2.OperationalError: FATAL: unsupported frontend protocol 1234.5679: server supports 2.0 to 3.0 (psycopg2.OperationalError)SSL SYSCALL错误:软件导致连接中止 - (psycopg2.OperationalError) SSL SYSCALL error: Software caused connection abort
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM