为什么manage.py syncdb无法连接到google cloud sql数据库?

时间:2013-12-11 00:53:05

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

在最近更新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)")

(你的-project-id:你的实例名称已被适当更改)。

3 个答案:

答案 0 :(得分:10)

简介

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

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

在Google App Engine Web控制台中进行设置

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

  1. 将公共IP分配给Cloud SQL实例(如果有帮助,请考虑AWS弹性IP)。
  2. 允许您对Cloud SQL实例的本地IP地址/范围访问。
  3. 更正您的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',
        }
    }
    

    对于连接到生产云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可以咬住灰尘(松了一口气)。

答案 1 :(得分:3)

从dev appserver连接到Cloud SQL的推荐方法是为实例docs请求和IP,然后将其用作普通IP连接。

答案 2 :(得分:1)

非常肯定会发生这种情况,因为Django API已更改且App Engine SDK从未更新过。我做了一些调整,让它工作: https://gist.github.com/pizzapanther/fe8c29a912a109806bd8

主要变化:

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

    可能会进行更多正确的更改,但由于此驱动程序通常仅用于部署迁移等操作,因此可以使用。