【问题标题】:Issue with django/postgresql when django started from cron当 django 从 cron 启动时,django/postgresql 出现问题
【发布时间】:2018-02-14 21:57:49
【问题描述】:

编辑询问谁认为这个问题与“可能相关”的问题有关。

我有一个使用 rest_framework 的相当简单的 django (1.11) 项目,当我从命令行启动它时工作正常,键入

nohup python manage.py runserver 0.0.0.0:4448 &

在centos上。连接到 postgresql 数据库,使用

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'my_database',
    'USER': 'my_user',
     #'PASSWORD': 'mypassword',
     'HOST': '127.0.0.1',
    ' PORT': '5432',
  }
}

在我的settings.py 文件中。但是,如果我将 runserver 命令设置为在启动时从 cron 运行,我会在向应用程序发送请求时得到以下信息:

... lot of stuff
django.db.utils.OperationalError: could not connect to server: Connection 
refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?

在我的 pg_hba 文件中有

IPv4 本地连接:

host    all                     all                     127.0.0.1/32               
trust                     # local host
host    all                     all                     10.2.11.53/32                    
trust
host    all                     my_user                 0.0.0.0/0                       trust

我也有

netstat -plunt | grep post
tcp        0      0 10.2.11.53:5432         0.0.0.0:*               LISTEN      
        867/postmaster      
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      
        867/postmaster  

有什么建议吗?

谢谢,

一个

PS 完整的追溯:

Unhandled exception in thread started by <function wrapper at 0x1a11230>
Performing system checks...

[<RegexURLPattern batch-batch-done ^batch/batch-done/$>, <RegexURLPattern batch-load-urls ^batch/load-urls/$>, <RegexURLPattern batch-request-batch ^batch/request-batch/$>, <RegexURLPattern batch-schedule-job ^batch/schedule-job/$>]
System check identified no issues (0 silenced).
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 128, in inner_run
    self.check_migrations()
  File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 422, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/lib/python2.7/site-packages/django/db/migrations/loader.py", line 52, in __init__
    self.build_graph()
  File "/usr/lib/python2.7/site-packages/django/db/migrations/loader.py", line 209, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
    self.ensure_schema()
  File "/usr/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 254, in cursor
    return self._cursor()
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 229, in _cursor
    self.ensure_connection()
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/usr/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/lib/python2.7/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/lib64/python2.7/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
        Is the server running on host "127.0.0.1" and accepting
        TCP/IP connections on port 5432?

【问题讨论】:

  • 为什么要从 cron 运行开发服务器?在生产环境中,您应该使用真正的 wsgi 服务器,由系统的进程管理器(例如 systemd)管理。

标签: django postgresql


【解决方案1】:

您能否发布完整的回溯以及 cron 作业本身?

另外,如果我正确理解了您的问题,您是否正在尝试使用 cron 在启动时启动服务器?如果是这种情况,最好使用 supervisord 之类的东西来管理您的服务器进程 IMO。

【讨论】:

  • 完整回溯:看起来我无法删除评论。发布回溯的最佳方式是什么。评论区太大了。
  • 您可能需要编辑您的原始帖子(cmets 太小,无法进行回溯)
  • 不知道为什么我的设置不起作用(我实际上是从其他人那里继承的),但是使用 supervisor 是一个巨大的改进,我认为它会更容易。并解决了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
  • 2023-03-14
  • 1970-01-01
  • 2013-02-16
  • 2021-08-19
  • 2011-07-28
  • 2014-08-26
相关资源
最近更新 更多