【问题标题】:Why does manage.py syncdb fail to connect to google cloud sql database?为什么manage.py syncdb无法连接谷歌云sql数据库?
【发布时间】:2014-09-22 06:27:50
【问题描述】:

在最近一次更新 Google App Engine 上的应用时,我正在更新数据库:

SETTINGS_MODE='prod' ./manage.py syncdb

这在我上次运行时有效,但现在我收到以下错误:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/c/clients/green_rentals/code/green-rental/south/management/commands/syncdb.py", line 92, in handle_noargs
    syncdb.Command().execute(**options)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/Library/Python/2.7/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
    cursor = connection.cursor()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 159, in cursor
    cursor = util.CursorWrapper(self._cursor(), self)
  File "/c/downloads/python/google_appengine/google/storage/speckle/python/django/backend/base.py", line 263, in _cursor
    if not self._valid_connection():
  File "/c/downloads/python/google_appengine/google/storage/speckle/python/django/backend/base.py", line 258, in _valid_connection
    return super(DatabaseWrapper, self)._valid_connection()
AttributeError: 'super' object has no attribute '_valid_connection'

我尝试将 Google App Engine SDK 更新到最新版本 (1.8.8),还尝试使用 oauth 重新进行身份验证(有效)。但仍然收到相同的消息。关于为什么的任何线索?

提前致谢!

编辑:

我用于数据库连接的设置最近似乎发生了变化。我以前用过:

DATABASES = {
    'default': {
        'ENGINE': 'google.appengine.ext.django.backends.rdbms',
        'INSTANCE': 'project-id:instance-id',
        'NAME': 'name',
    }
}

但现在的建议是:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': '/cloudsql/your-project-id:your-instance-name',
        'NAME': 'database-name',
        'USER': 'mysql-user',
    }
}

更新到那个之后,我现在收到一个新错误:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/c/clients/green_rentals/code/green-rental/south/management/commands/syncdb.py", line 89, in handle_noargs
    db.connection_init() 
  File "/c/clients/green_rentals/code/green-rental/south/db/mysql.py", line 180, in connection_init
    cursor = self._get_connection().cursor()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 159, in cursor
    cursor = util.CursorWrapper(self._cursor(), self)
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 129, in _cursor
    self.ensure_connection()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 124, in ensure_connection
    self.connect()
  File "/Library/Python/2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 124, in ensure_connection
    self.connect()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 112, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 435, in get_new_connection
    conn = Database.connect(**conn_params)
  File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/cloudsql/your-project-id:your-instance-name' (2)")

(your-project-id:your-instance-name 已适当更改)。

【问题讨论】:

    标签: python google-app-engine google-cloud-sql


    【解决方案1】:

    简介

    我将在这里为那些也被此绊倒的人留下更详细的答案,我意识到另一个已经被接受,但希望这会对某人有所帮助。在撰写本文时,我们使用的是 Django 1.6。

    基本上可以归结为:Google App Engine 的Django <-> CloudSQL docs 目前已过时。他们利用 OAuth2 连接机制,如果您在无头 Vagrant/虚拟机内部使用 syncdb 管理命令,这充其量是笨拙的,并且完全是痛苦的,因为它要求您拥有启用 Javascript 的浏览器才能实际检索令牌(除非您使用 gflags 库的全局状态,但这是另一天的故事),这显然在命令行上很麻烦(我相信只有 elinks 有 CLI JS 支持,而 Ubuntu repo 版本不支持) t 包括 js 支持..)

    在 Google App Engine Web 控制台中设置

    如果您访问此link,您将找到有关连接到实例的新方法的说明。这可以通过在 Web 控制台中单击一下来实现:

    1. 将公共 IP 分配给 Cloud SQL 实例(如果有帮助,请根据 AWS 弹性 IP 考虑这些)。
    2. 允许您的本地 IP 地址/范围访问 Cloud SQL 实例。

    更正您的 Django 设置

    对于在 App Engine 上运行本身的生产实例:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '/cloudsql/project-name:instance-name',
            'NAME': 'name_of_pre_created_database',
            'USER': 'root_or_pre_created_user',
        }
    }
    

    对于连接到生产 Cloud SQL 实例的开发实例

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '198.198.88.88', 
            'NAME': 'name_of_pre_created_database',
            'USER': 'root_or_pre_created_user',
            'PASSWORD': 'password_of_pre_created_or_root_user',
        }
    }
    

    其中 198.198.88.88 是 Google 在您请求后自动分配给您的 Cloud SQL 实例的 IP 地址(见上文)。请记住,您需要从您允许访问该特定 Cloud SQL 实例的 IP 进行连接。

    有了这个,google.appengine.ext.django.backends.rdbms 可以咬住灰尘(松了一口气)。

    【讨论】:

    • 非常感谢,我为这个问题苦苦挣扎了很久。不知道为什么不维护 AppEngine 文档。
    • 登录只是为了支持这篇文章。您先生很光荣,这完全符合需要。
    • 不客气。我希望我能回答为什么不维护它们;他们至少可以删除损坏的过时文档。或者地狱,开源努力!
    • 这很有帮助。顺便问一下,你是如何让 Django 1.6 为 GAE 工作的?您能否指出有关如何让 Django >1.5 在 Google App Engine 上运行的说明?
    【解决方案2】:

    从开发应用服务器连接到 Cloud SQL 的推荐方法是请求实例的 IP 地址docs,然后将其用作普通 IP 连接。

    【讨论】:

    • 这是要走的路。我还必须授权我的本地网络地址并确保在实例上设置了 root 密码。根据此链接,看起来原始的 OAuth2 身份验证方法现在已被弃用:developers.google.com/cloud-sql/docs/external djago 页面尚未反映这一点。感谢您的帮助!
    【解决方案3】:

    很确定会发生这种情况,因为 Django API 发生了变化,而 App Engine SDK 从未更新过。我做了一些调整并让它工作: https://gist.github.com/pizzapanther/fe8c29a912a109806bd8

    主要变化:

    1. _valid_connection() 现在是 is_usable
    2. Connect(**kwargs) 之后添加了self.set_autocommit(self.settings_dict['AUTOCOMMIT'])

    可能需要进行更适当的更改,但由于此驱动程序通常仅用于迁移等部署,因此可行。

    【讨论】:

      猜你喜欢
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 2019-04-01
      • 1970-01-01
      相关资源
      最近更新 更多