Google App Engine Flexible Environment Django Example fails to connect to CloudSQL database if MySQL is used instead of PostgreSQL · Issue #1056 · GoogleCloudPlatform/python-docs-samples · GitHub
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DATABASES = {
'default': {
# If you are using Cloud SQL for MySQL rather than PostgreSQL, set
# 'ENGINE': 'django.db.backends.mysql' instead of the following.
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'polls',
'USER': '<your-database-user>',
'PASSWORD': '<your-database-password>',
}
}
As such, the comments in the code imply that the correct configuration for using MySQL on CloudSQL instead of PostgreSQL is this:
The line specifying 'ENGINE' is 'django.db.backends.mysql' is the only line that I changed from the original example.
Describe the issue
According to Google's tutorial on https://cloud.google.com/python/django/flexible-environment, section 'Run the app on your local computer', step 3 'Run the Django migrations to set up your models', we need to run this line: python manage.py makemigrations
Unfortunately, this causes the following error statement to be printed out on the terminal:
Traceback (most recent call last):
File "manage.py", line 24, in <module>
execute_from_command_line(sys.argv)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_f
rom_command_line
utility.execute()
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 327, in execute
self.check()
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/core/checks/model_checks.py", line 30, in check_all_
models
errors.extend(model.check(**kwargs))
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 1283, in check
errors.extend(cls._check_fields(**kwargs))
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 1358, in _check_fields
errors.extend(field.check(**kwargs))
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 913, in check
errors = super(AutoField, self).check(**kwargs)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 219, in check
errors.extend(self._check_backend_specific_checks(**kwargs))
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 322, in _check_b
ackend_specific_checks
return connections[db].validation.check_field(self, **kwargs)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/validation.py", line 49, in check_field
field_type = field.db_type(self.connection)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 644, in db_type
return connection.data_types[self.get_internal_type()] % data
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 174, in data_types
if self.features.supports_microsecond_precision:
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/features.py", line 53, in supports_microsecond_precision
return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 385, in mysql_version
with self.temporary_connection() as cursor:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 591, in temporary_connection
cursor = self.cursor()
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 254, in cursor
return self._cursor()
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 229, in _cursor
self.ensure_connection()
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 274, in get_new_connection
conn = Database.connect(**conn_params)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 86, in Connect
return Connection(*args, **kwargs)
File "/home/google/python-docs-samples/appengine/flexible/django_cloudsql/venv/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1045, "Access denied for user 'user1'@'localhost' (using password: YES)")
This shows that Django failed to connect to the database.
I have followed the steps to insert the database user string and database user password string as required in mysite/settings.py. I have also followed the steps to get the value of 'connectionName' and use it in <your-cloudsql-connection-string> in mysite/settings.py.
Since ./cloud_sql_proxy is simultaneously running, this is the output that it displays from the failed attempt to run python manage.py makemigrations:
./cloud_sql_proxy -instances="<your-cloudsql-connection-string>"=tcp:5432
2017/08/06 15:22:52 Listening on 127.0.0.1:5432 for <your-cloudsql-connection-string>
2017/08/06 15:22:52 Ready for new connections
2017/08/06 15:26:55 New connection for "<your-cloudsql-connection-string>"
2017/08/06 15:26:56 Reading data from local connection on 127.0.0.1:5432 had error: read tcp 127.0.0.1:5432->127.0.0.1:57412: read: connection reset by peer
Solution
This error disappears (i.e. python manage.py makemigrations works as per normal) when we specify the database port in mysite/settings.py. In other words, by adding one key-value pair in 'DATABASES' in mysite/settings.py such that it looks like this:
When I comment out the line specifying the port and run python manage.py makemigrations , the error occurs again. This implies that adding the line specifying the port fixes the error.
The comments in the original code should be changed to specify that if you want to use MySQL for CloudSQL, you also need to include 'PORT' as '5432' in addition to changing the value of 'ENGINE'. Otherwise, the attempt to run makemigrations locally would fail for MySQL.
I feel this is a necessary change because it is likely that developers might want to use Google App Engine Flexible Environment with a MySQL backend instead of PostgreSQL, especially since the PostgreSQL o
8000
ption is currently still in beta.
The text was updated successfully, but these errors were encountered:
Thank you for your detailed report. I think I understand the situation; the port is specified explicitly in the Cloud SQL Proxy setup, but it is specified only by default in the Django setup. If you change the protocol from Postgres to MySQL, the default port changes, but the explicit port for the Cloud SQL Proxy remains the same. I'll look at how to amend the comments to fix this issue.
In which file did you encounter the issue?
mysite/settings.py
Did you change the file? If so, how?
Original settings in
mysite/settings.py
specify:As such, the comments in the code imply that the correct configuration for using MySQL on CloudSQL instead of PostgreSQL is this:
The line specifying 'ENGINE' is 'django.db.backends.mysql' is the only line that I changed from the original example.
Describe the issue
According to Google's tutorial on https://cloud.google.com/python/django/flexible-environment, section 'Run the app on your local computer', step 3 'Run the Django migrations to set up your models', we need to run this line:
python manage.py makemigrations
Unfortunately, this causes the following error statement to be printed out on the terminal:
This shows that Django failed to connect to the database.
I have followed the steps to insert the database user string and database user password string as required in mysite/settings.py. I have also followed the steps to get the value of 'connectionName' and use it in
<your-cloudsql-connection-string>
in mysite/settings.py.Since ./cloud_sql_proxy is simultaneously running, this is the output that it displays from the failed attempt to run
python manage.py makemigrations
:Solution
This error disappears (i.e.
python manage.py makemigrations
works as per normal) when we specify the database port in mysite/settings.py. In other words, by adding one key-value pair in 'DATABASES' in mysite/settings.py such that it looks like this:When I comment out the line specifying the port and run
python manage.py makemigrations
, the error occurs again. This implies that adding the line specifying the port fixes the error.The comments in the original code should be changed to specify that if you want to use MySQL for CloudSQL, you also need to include 'PORT' as '5432' in addition to changing the value of 'ENGINE'. Otherwise, the attempt to run makemigrations locally would fail for MySQL.
I feel this is a necessary change because it is likely that developers might want to use Google App Engine Flexible Environment with a MySQL backend instead of PostgreSQL, especially since the PostgreSQL o 8000 ption is currently still in beta.
The text was updated successfully, but these errors were encountered: