简体   繁体   中英

Django on Google AppEngine with CloudSQL: How to connect database (Error 2002, Can't connect to local MySQL server..)

I'm trying to start a Django app to be hosted on GAE, using CloudSQL. Locally, I'm on Mac OSX Maverics, working within a virtualenv (via virtualenvwrapper).

After installing the GAE SDK for Python, I started my virtual environment, installed Django 1.5 from /usr/local/google_appengine/lib/django-1.5/

Also, on appengine.google.com I created a new app, and connected a CloudSQL instance to it (enable billing).

I'm able to create a new Django project, eg django-admin.py startproject test01 , then I edit its settings.py to change the DATABASES definition per Google's instructions, eg:

DATABASES = {
   'default': {
      'ENGINE': 'django.db.backends.mysql',
      'HOST': '/cloudsql/myapp-test01:myapp-db-test01',
      'NAME': 'test01',
      'USER': 'test01',
}

}

I also added app.yaml to the root of the project folder, per Google's docs:

application: test01
version: 1
runtime: python27
api_version: 1
threadsafe: true

libraries:
- name: django
  version: "1.5"

builtins:
- django_wsgi: on

This is where I hit roadblocks.

First: What exactly should be entered into the DATABASES for NAME and USER fields? The docs do not go into any detail.

Second, when I run: python manage.py syncdb to initialize the app, I get:

OperationalError: (2002, "Can't connect to local MySQL server through socket '/cloudsql/desgn-test-01:db-test-01' (2)") 

I do have MySQL installed, via brew install mysql (although I didn't do that inside the virtual environment), and I also have MySQL-python.

I'm new to GAE and fairly inexperienced with setting up databases, so I'm not sure what do try next. I'm not sure if the issue is with my local MySQL, or the CloudSQL connection settings?

(A more general Django on GAE question: What is the workflow exactly? If I get the connection to work, does it mean that I am using CloudSQL even when developing my Django app locally? How do I subsequently "push" the app to the AppEngine, or make updates? I'm assuming this is done with the Launcher but what is the correlation between creating (adding) an app using the appengine.google.com dashboard, versus adding a new app in the local launcher? Quite confused by this -- are these two one and the same app, need to have identical names, or..?)

Looks like at the time, you were missing the password field as well...

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test01',
        'USER': 'test01',
        'PASSWORD': '[password]',
        'HOST': '/cloudsql/myapp-test01:myapp-db-test01',
        'PORT': '3306',
    }

Using the GUI for Cloud SQL, you can always add new Super Admin level users to the Cloud SQL instance in case you're unsure what user to use as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM