Connecting to Google Cloud SQL from App Engine: access denied
<p>Trying to connect to Google Cloud SQL from App Engine. Getting an error 500 and the logs say: "Access denied for user 'root'@'cloudsqlproxy~' (using password: NO)"). The App Engine and cloud SQL are in the same project. The code I use to connect:</p>

<pre><code>class MainPage(webapp2.RequestHandler):

def get(self):

    self.response.headers['Content-Type'] = 'text/plain'

    env = os.getenv('SERVER_SOFTWARE')
    if (env and env.startswith('Google App Engine/')):
        # Connecting from App Engine
        db = MySQLdb.connect(
            unix_socket='/cloudsql/&lt;project-id&gt;:europe-west1:&lt;instance-name&gt;',
            user='root')

    cursor = db.cursor()
    cursor.execute('SELECT 1 + 1')

    self.response.write('pong')

app = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)
</code></pre>

<p>As I understand, I am doing everything correctly. The code above I effectively copy-pasted a snippet that you can view in Google Dev Console. Everywhere on StackExchange it is said, that I should not pass the password, so I am not doing that either. I have changed root user password when I created the instance. I don't see why would database deny acess to App Engine, they are on the same project! Is there a way to check what are the permissions for the database? </p>

<p>I would say this part is not well documented on <code>Google Developer Console</code>.</p>

<p><code>Google Cloud SQL 2nd Gen</code> comes with a little modification required in the connection string which is not provided in your code.</p>

<p>Please have a look at the original code after some modifications from my side:</p>

<pre><code>class MainPage(webapp2.RequestHandler):

def get(self):

self.response.headers['Content-Type'] = 'text/plain'

#db connection
if (os.getenv('SERVER_SOFTWARE') and
    os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
    db = MySQLdb.connect(unix_socket='/cloudsql/&lt;project-id&gt;:europe-west1&lt;instance-name&gt;, db = 'your-db', user= 'root', passwd='your-root-password')
else:
    db = MySQLdb.connect(host = '10.10.0.1', port=3306,
                                    db = 'your-db', user='root', passwd='root-password')

cursor = db.cursor()
cursor.execute('SELECT 1 + 1')

self.response.write('pong')

app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
</code></pre>

<p>Regards.</p>

python google-app-engine google-cloud-sql