我正在尝试将django 1.3应用程序移植到AppEngine + Google Cloud SQL。但是我在从本地开发django连接到我的cloudSQL实例时遇到了一些问题。
我可以使用dbshell
连接到CloudSQL。它显示了浏览器身份验证,我在提示符处被删除。
(ENV)$ SETTINGS_MODE=prod ./manage.py dbshell
Google SQL Client
Type "help" or "?" for help.
Connecting to Google SQL database "my_project:prod1|my_db" on host None.
Using readline for history management.
Loading history file "/Users/user/.googlesql/django_proj#prod1.hist"
sql> SELECT CURRENT_USER();
Execution time: 0.694 seconds
+----------------+
| CURRENT_USER() |
+----------------+
| root@localhost |
+----------------+
我认为dbshell实际上似乎以root用户身份运行很奇怪,即使我的settings.py
文件中没有root密码。但它仍然有效。
尝试运行syncdb
时失败。
(ENV)$ SETTINGS_MODE=prod ./manage.py syncdb
Traceback (most recent call last):
File "./manage.py", line 11, in <module>
execute_manager(settings)
File "/usr/local/google_appengine/lib/django-1.3/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/usr/local/google_appengine/lib/django-1.3/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/google_appengine/lib/django-1.3/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/google_appengine/lib/django-1.3/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/usr/local/google_appengine/lib/django-1.3/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/usr/local/google_appengine/lib/django-1.3/django/core/management/commands/syncdb.py", line 56, in handle_noargs
cursor = connection.cursor()
File "/usr/local/google_appengine/lib/django-1.3/django/db/backends/__init__.py", line 252, in cursor
cursor = util.CursorWrapper(self._cursor(), self)
File "/usr/local/google_appengine/google/storage/speckle/python/django/backend/base.py", line 275, in _cursor
self.connection = Connect(**kwargs)
File "/usr/local/google_appengine/google/storage/speckle/python/django/backend/base.py", line 200, in Connect
return driver.connect(**kwargs)
File "/usr/local/google_appengine/google/storage/speckle/python/api/rdbms_googleapi.py", line 183, in __init__
super(GoogleApiConnection, self).__init__(*args, **kwargs)
File "/usr/local/google_appengine/google/storage/speckle/python/api/rdbms.py", line 812, in __init__
self.OpenConnection()
File "/usr/local/google_appengine/google/storage/speckle/python/api/rdbms.py", line 835, in OpenConnection
response = self.MakeRequest('OpenConnection', request)
File "/usr/local/google_appengine/google/storage/speckle/python/api/rdbms.py", line 946, in MakeRequest
raise _ToDbApiException(response.sql_exception)
google.storage.speckle.python.api.rdbms.OperationalError: (1045, u"Access denied for user 'my_prod_user'@'localhost' (using password: YES)")
在这种情况下,您可以看到它尝试使用我的真实用户,而不是root用户。它因访问被拒绝而失败。
如果我尝试使用my_prod_user
直接连接到云SQL,那就可以了。
$ mysql -h XXX.XXX.XXX.XXX -u my_prod_user -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.36 (Google)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT CURRENT_USER();
+-----------------+
| CURRENT_USER() |
+-----------------+
| my_prod_user@% |
+-----------------+
1 row in set (0.19 sec)
有没有人碰到这个?
答案 0 :(得分:1)
我的问题是我设置settings.py文件的方式。在通过本地计算机访问您的cloudSQL数据库时,您必须使用用户root
。此外,本地密码似乎没有任何区别,因为它可能只是使用oAuth令牌来创建连接。
以下是我在本地开发机器上访问CloudSQL的工作设置。
DATABASES = {
'default': {
'ENGINE': 'google.appengine.ext.django.backends.rdbms',
'INSTANCE': 'project-name:instance',
'NAME': 'my_db',
'USER': 'root',
}
}
这是我以前使用的破碎的。请注意我的内联密码。傻我。
DATABASES = {
'default': {
'ENGINE': 'google.appengine.ext.django.backends.rdbms',
'INSTANCE': 'project-name:instance',
'NAME': 'my_db',
'USER': 'my_prod_user',
'PASSWORD' : 'myVerySecretPassword',
}
}
我认为Google讨厌密码。我很高兴我的应用程序中不再需要这些硬编码。
答案 1 :(得分:0)
使用Django == 1.10.4,这是我用来连接的设置。注意主机和密码。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': 'ip_address',
'NAME': 'db_name',
'USER': 'root',
'PASSWORD': 'password'
}
}
新命令是:
SETTINGS_MODE=prod python manage.py migrate