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

时间:2017-03-02 02:54:01

标签: python postgresql docker flask-sqlalchemy psycopg2

我正在尝试连接两个docker容器,一个posgresql和另一个python flask应用程序。两者都正确链接,python应用程序中的所有连接变量都直接来自postgres容器中通过链接公开的那些变量,并且与检查postgresql容器时发现的变量相同。当我使用psql与连接字符串中的确切参数,即:

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

连接到postgres容器中的数据库是成功的,所以我知道postgres正在通过指定的端口等正确通信。但是,当我尝试通过具有相同连接变量的烧瓶连接到数据库时,我收到此错误:

  

Traceback(最近一次调用最后一次):文件   " /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;, line   1122,在_do_get中       return self._pool.get(wait,self._timeout)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/util/queue.py",   第145行,在得到       提升空sqlalchemy.util.queue.Empty

     

在处理上述异常期间,发生了另一个异常:

     

Traceback(最近一次调用最后一次):文件   " /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py" ;,   第2138行,在_wrap_pool_connect中       return fn()File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;,第328行,   在unique_connection中       return _ConnectionFairy._checkout(self)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;,第766行,   在_checkout中       fairy = _ConnectionRecord.checkout(pool)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;,第516行,   在结帐时       rec = pool._do_get()File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;, line   1138,在_do_get中       self._dec_overflow()File" /usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py",   第60行,在退出       compat.reraise(exc_type,exc_value,exc_tb)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py",   第187行,重新加入       提升值文件" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;, line   1135,在_do_get中       return self._create_connection()File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;,第333行,   在_create_connection中       return _ConnectionRecord(self)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;,第461行,   在 init       self。 connect(first_connect_check = True)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;,第651行,   在__connect中       connection = pool._invoke_creator(self)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py",   第105行,连接中       return dialect.connect(* cargs,** cparams)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py",   第393行,在连接中       return self.dbapi.connect(* cargs,** cparams)File" /usr/local/lib/python3.6/site-packages/psycopg2 / __ init .py",line   164,在连接中       conn = _connect(dsn,connection_factory = connection_factory,async = async)psycopg2.OperationalError:无法连接到服务器:   连接被拒绝服务器是否在主机上运行" 172.17.0.2"和   接受端口5432上的TCP / IP连接?

     

上述异常是导致以下异常的直接原因:

     

回溯(最近一次呼叫最后一次):文件" ./ wsgi.py",第14行,在          app = getModule(" / var / www",name).app文件" ./ wsgi.py",第5行,在getModule中       return imp.load_source(module,("%s /%s.py"%(path,name)))File" /usr/local/lib/python3.6/imp.py&# 34;,第172行,在load_source中       module = _load(spec)File" /var/www/backend.py" ;,第3行,in       import json,logging,db,os File" /var/www/db.py" ;,第36行,in       engine.connect()File" /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py",   第2082行,在连接中       return self._connection_cls(self,** kwargs)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py",   第90行,在 init 中       如果连接不是其他无引擎engine.raw_connection()文件" /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py",   第2168行,在raw_connection中       self.pool.unique_connection,_connection)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py",   第2142行,在_wrap_pool_connect中       e,dialect,self)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py",   第1456行,在_handle_dbapi_exception_noconnection中       exc_info文件" /usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py",   第203行,在raise_from_cause中       reraise(type(exception),exception,tb = exc_tb,cause = cause)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py",   第186行,重新加入       提高value.with_traceback(tb)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py",   第2138行,在_wrap_pool_connect中       return fn()File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;,第328行,   在unique_connection中       return _ConnectionFairy._checkout(self)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;,第766行,   在_checkout中       fairy = _ConnectionRecord.checkout(pool)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;,第516行,   在结帐时       rec = pool._do_get()File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;, line   1138,在_do_get中       self._dec_overflow()File" /usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py",   第60行,在退出       compat.reraise(exc_type,exc_value,exc_tb)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py",   第187行,重新加入       提升值文件" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;, line   1135,在_do_get中       return self._create_connection()File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;,第333行,   在_create_connection中       return _ConnectionRecord(self)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;,第461行,   在 init       self。 connect(first_connect_check = True)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py" ;,第651行,   在__connect中       connection = pool._invoke_creator(self)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py",   第105行,连接中       return dialect.connect(* cargs,** cparams)File" /usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py",   第393行,在连接中       return self.dbapi.connect(* cargs,** cparams)File" /usr/local/lib/python3.6/site-packages/psycopg2 / __ init .py",line   164,在连接中       conn = _connect(dsn,connection_factory = connection_factory,async = async)sqlalchemy.exc.OperationalError:   (psycopg2.OperationalError)无法连接到服务器:Connection   拒绝服务器是否在主机上运行" 172.17.0.2"并接受     端口5432上的TCP / IP连接?

下面是我的烧瓶应用程序中的代码,导致异常:

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()

当我记录"引擎"我明白了:

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

再次匹配成功数据库连接中使用的参数与来自我的主机的数据库。

我假设我的python代码一定有问题,因为所有帐户都显示docker容器才能正常工作。

任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:4)

所以我的代码实际上是正常的,发生的事情是竞争条件,其中python在数据库完全初始化并接受连接之前发送连接请求。