无法在Google CloudSQL上运行syncdb,操作错误1045,用户'root'@'localhost'访问被拒绝,未使用密码。

0 投票
1 回答
742 浏览
提问于 2025-04-18 17:46

根据以下的谷歌文档教程,我有了这样的设置。

我从来没有看到谷歌提示我输入oauth令牌的URL。按照他们的文档应该是会有的。我不太确定哪里出了问题。
也许有人可以尝试在syncdb时使用--traceback选项和-v 3,告诉我在使用syncdb时调用了哪些路径和库。 https://accounts.google.com/o/oauth2/auth.

这是我在尝试使用syncdb连接时遇到的错误。

$ SETTINGS_MODE='prod' python manage.py  syncdb -v 3 --traceback  
Traceback (most recent call last):  
  File "/home/user/bin/lib/google_appengine/lib/django-1.5/django/core/management/base.py", line 222, in run_from_argv  
    self.execute(*args, **options.__dict__)  
  File "/home/user/bin/lib/google_appengine/lib/django-1.5/django/core/management/base.py", line 255, in execute  
    output = self.handle(*args, **options)  
  File "/home/user/bin/lib/google_appengine/lib/django-1.5/django/core/management/base.py", line 385, in handle  
    return self.handle_noargs(**options)  
  File "/home/user/bin/lib/google_appengine/lib/django-1.5/django/core/management/commands/syncdb.py", line 56, in handle_noargs  
    cursor = connection.cursor()  
  File "/home/user/bin/lib/google_appengine/lib/django-1.5/django/db/backends/__init__.py", line 324, in cursor  
    cursor = self.make_debug_cursor(self._cursor())  
  File "/home/user/bin/lib/google_appengine/lib/django-1.5/django/db/backends/mysql/base.py", line 406, in _cursor  
    self.connection = Database.connect(**kwargs)  
  File "/home/user/projects/my-app/src/my-app/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect  
    return Connection(*args, **kwargs)  
  File "/home/user/projects/my-app/src/my-app/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__  
    super(Connection, self).__init__(*args, **kwargs2)  
OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)")  

当我尝试使用GAE自带的特定版本的django,并使用manage.py提供的设置来设置PATH时,我得到了完全相同的错误。

SETTINGS_MODE='prod' python ../lib/google_appengine/lib/django-1.5/django/bin/django- admin.py  syncdb -v 3 --traceback --settings=opexdash.settings --pythonpath=. 

不过,我可以毫无问题地使用dbshell。

$ SETTINGS_MODE='prod' python manage.py dbshell  
Reading table information for completion of table and column names  
You can turn off this feature to get a quicker startup with -A  

Welcome to the MySQL monitor.  Commands end with ; or \g.  
Your MySQL connection id is 54  
Server version: 5.5.39 (Google)  

Copyright (c) 2000, 2014, 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 @@hostname;  
+------------+  
| @@hostname |  
+------------+  
| localhost  |  
+------------+  
1 row in set (0.40 sec)  

mysql> select database();  
+------------+  
| database() |  
+------------+  
| my_db      |  
+------------+  
1 row in set (0.32 sec)  

以下是我根据文档使用的额外设置。

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):  
    # Running on production App Engine, so use a Google Cloud SQL database.  
    DATABASES = {  
        'default': {  
            'ENGINE': 'django.db.backends.mysql',  
            'HOST': '/cloudsql/nth-hybrid-672:beta-dash02',  
            'NAME': 'my-app',  
            'USER': 'root',  
        }  
    }  
elif os.getenv('SETTINGS_MODE') == 'prod':  
    # Running in development, but want to access the Google Cloud SQL instance  
    # in production.  
    DATABASES = {  
        'default': {  
            #'ENGINE': 'google.appengine.ext.django.backends.rdbms',  
            'ENGINE': 'django.db.backends.mysql',  
            'INSTANCE': 'your-project-id:your-instance-name',  
            'NAME': 'my-app',  
            'USER': 'root',  
            #'HOST': '173.100.100.100',  I can synkdb using it this way with a password set, ip address has been changed for this public forum
            #'PASSWORD' : ''  
        }  
    }  
else:  
    # Running in development, so use a local MySQL database.  
    DATABASES = {  
        'default': {  
            'ENGINE': 'django.db.backends.mysql',  
            'NAME': 'gae',  
            'USER': 'gae',  
            'PASSWORD': 'gae',  
        }  
    }   

我认为问题很可能出在我的环境设置路径上。 但我认为当我在GAE/lib目录中使用manage的完整路径时,它应该可以正常工作。

$PATH
/home/user/projects/my-app/src/my-app/bin:   
/usr/local/sbin:  
/usr/local/bin:  
/usr/sbin:  
/usr/bin:   
/sbin:  
/bin:  
/usr/games:  
/usr/local/games:  
/usr/lib/jvm/default-java:  
/usr/share/javadb/bin:  
/home/user/bin:  
/home/user/bin/lib/google_appengine/:  

1 个回答

2

你不需要使用oauth,连接数据库就像连接一个普通的远程MySQL服务器一样;所以你需要

'ENGINE': 'django.db.backends.mysql',  
'NAME': 'my-app',  # your DB name
'USER': 'root',  
'HOST': '173.xxx.xxx.xxx',  # your instance's IP address
'PASSWORD' : 'your-root-password-here',

(假设你已经允许自己的IP地址连接到这个实例。)你也可以设置连接通过SSL加密。

谷歌的文档有点过时,不过这篇文章是来自谷歌云SQL的工程师,他说连接的推荐方式是把MySQL实例当作一个普通的远程服务器来处理。

撰写回答