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

Cha*_*ndt 4 python google-app-engine google-cloud-sql

在最近更新Google App Engine上的应用时,我正在使用以下方法更新数据库:

SETTINGS_MODE='prod' ./manage.py syncdb
Run Code Online (Sandbox Code Playgroud)

这是我上次运行时的工作,但现在我收到以下错误:

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'
Run Code Online (Sandbox Code Playgroud)

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

提前致谢!

编辑:

看起来我用于数据库连接的设置最近发生了变化.我以前用过:

DATABASES = {
    'default': {
        'ENGINE': 'google.appengine.ext.django.backends.rdbms',
        'INSTANCE': 'project-id:instance-id',
        'NAME': 'name',
    }
}
Run Code Online (Sandbox Code Playgroud)

但现在的建议是:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': '/cloudsql/your-project-id:your-instance-name',
        'NAME': 'database-name',
        'USER': 'mysql-user',
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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)")
Run Code Online (Sandbox Code Playgroud)

(您的project-id:your-instance-name已被适当更改).

Dar*_*ody 10

介绍

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

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

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

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

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

更正您的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',
    }
}
Run Code Online (Sandbox Code Playgroud)

对于连接到生产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',
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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