0
votes

I am trying to connect to an instance of Google Cloud SQL that I created, but every attempt to connect has either resulted with an error message saying I could not connect or did not have access. I have used a cloud proxy connection, a private IP connection, and a Cloud shell connection.

I've used the cloudsql git repository as reference (https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard/cloudsql)

I have only changed these files in regard to my specific project and instance.

to gather the info about the connection:

CLOUDSQL_CONNECTION_NAME = os.environ.get('CLOUDSQL_CONNECTION_NAME')
CLOUDSQL_USER = os.environ.get('CLOUDSQL_USER')
CLOUDSQL_PASSWORD = os.environ.get('CLOUDSQL_PASSWORD')

to set the connection (included on git):

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
    # Connect using the unix socket located at
    # /cloudsql/cloudsql-connection-name.
    cloudsql_unix_socket = os.path.join(
        '/cloudsql', CLOUDSQL_CONNECTION_NAME)

    db = MySQLdb.connect(
        unix_socket=cloudsql_unix_socket,
        user=CLOUDSQL_USER,
        passwd=CLOUDSQL_PASSWORD)
else:
   db = MySQLdb.connect(
            host='127.0.0.1', user=CLOUDSQL_USER, passwd=CLOUDSQL_PASSWORD)

The errors I am receiving:

pymysql.err.OperationalError: (1045, "Access denied for user ''@'localhost' (using password: YES)")

and

(pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 2] No such file or directory)")

I've tried just about everything. This is only one example of the many routes I have taken. Any advice or guidance would be greatly appreciated.

1
This error means that the "username" that you are connecting to the database is either not correct or you have not "GRANT PRIVILEGES" to this user. These are MySQL issues and not GCP issues. You can modify users in the Cloud SQL console or directly in the MySQL database. chartio.com/resources/tutorials/… Start by using the mysql command-line tool to test. Then move to Python code. - John Hanley
The reference you have linked is for connecting an App Engine Standard service to a Cloud SQL instance. Is that your case? If not, where are you connecting from? - llompalles

1 Answers

0
votes

The sample you are referencing requires you to be running on App Engine Standard, or have used the proxy to create a Unix socket at /cloudsql/<INSTANCE_CONNECTION_NAME>.

In your example, I would make sure that the check you are using (SERVER_SOFTWARE env var), is working correctly.

When you are running locally, your app will attempt to connect on 127.0.0.1. For this to work, the proxy will need to be running and bound to a tcp port. Check the logs from the proxy to see if any additional errors are thrown.

When you are running in App Engine, make sure you have followed the setup on this page to enable the Cloud SQL Admin API.