为什么manage.py syncdb无法连接到Google Cloud SQL数据库? [英] Why does manage.py syncdb fail to connect to google cloud sql database?

查看:74
本文介绍了为什么manage.py syncdb无法连接到Google Cloud SQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近一次对Google App Engine上的应用程序进行更新的过程中,我正在使用以下方法更新数据库:

During a recent update of an app on Google App Engine I was in the process of updating the database with:

SETTINGS_MODE='prod' ./manage.py syncdb

这是我上次运行时有效的方法,但是现在出现以下错误:

This worked last time I ran it, but now I am getting the following error:

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(有效)进行重新身份验证.虽然仍然收到相同的消息.为什么有任何线索?

I tried updating the Google App Engine SDK to the latest (1.8.8) and I also tried re-authenticating with oauth (that worked). Still getting the same message though. Any clues as to why?

提前谢谢!

看来我最近用于数据库连接的设置已更改.我曾经用过:

It looks like the settings I was using for the database connection have changed recently. I used to use:

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',
    }
}

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

After updating to that, I now get a new error:

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已适当更改的地方).

(Where your-project-id:your-instance-name have been changed appropriately).

推荐答案

简介

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

Intro

I'm going to leave a more detailed answer here for those that have also been tripped up by this, I realise the other one has already been accepted but hopefully this will help someone. We are on Django 1.6 at the time of writing.

基本上可以归结为: Django<-> Google App Engine的CloudSQL文档当前已过期.如果您从无头的Vagrant/虚拟机内部使用syncdb管理命令,则它们利用了OAuth2连接机制,该机制充其量是笨拙的,而且会很麻烦,因为它要求您具有启用Javascript的浏览器才能实际检索令牌(除非您使用gflags库的全局状态,否则这是另一回事了.这显然在命令行上很麻烦(我相信只有elinks支持CLI JS,而该版本的Ubuntu repo版本却没有).包括js支持..

It basically comes down to this: the Django <-> CloudSQL docs for Google App Engine are currently out of date. They utilise the OAuth2 connection mechanism which is clunky at best and a complete pain if you're using the syncdb management command from inside a headless Vagrant/virtual machine as it requires you to have a Javascript enabled browser to actually retrieve the token (unless you play about with the gflags library's global state, but that's a story for another day) and this is obviously quite a bother on the command line (I believe only elinks has CLI JS support and the Ubuntu repo versions of that don't include the js support..)

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

If you go to this link you will find instructions on the new way to connect to instances. This works by doing some clicking about in the web console:

  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',
    }
}

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

For development instances that connect to a production Cloud SQL instance:

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连接.

Where 198.198.88.88 is the IP address that Google automatically assign to your Cloud SQL instance after you request it (see above). Remember that you'll need to connect from an IP that you have allowed to access that specific Cloud SQL instance.

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

And with this, google.appengine.ext.django.backends.rdbms can bite the dust (breathe sigh of relief).

这篇关于为什么manage.py syncdb无法连接到Google Cloud SQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆