Ahmed Lotfy
Ahmed Lotfy

Reputation: 1145

Cloud SQL Access denied

import webapp2
import MySQLdb
import os

class MainPage(webapp2.RequestHandler):
def get(self):
    if (os.getenv('SERVER_SOFTWARE') and
        os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
        db = MySQLdb.connect(unix_socket='/cloudsql/fluent-outlet-604:test-db' , db='guestbook', user='root',passwd='root')
        # connect to the cloud SQL
    else:
        db = MySQLdb.connect(host='173.194.248.221', port=3306, db='guestbook', user='root',passwd='root')

    cursor = db.cursor()
    cursor.execute('SELECT guestName, content, entryID FROM entries')

    data = cursor.fetchall()
    db.close()
    self.response.write(data)

    application = webapp2.WSGIApplication([
         ('/',MainPage),
    ],debug=True)

when i deploy this app to the app engine i gain error says "(1045, "Access denied for user 'root'@'localhost' (using password: YES)")

Upvotes: 0

Views: 3168

Answers (3)

kip2
kip2

Reputation: 6913

I saw this problem when I didn't specify the hostname in the database URI:

SQLALCHEMY_DATABASE_URI = (
            'mysql+pymysql://<user-name>:<password>@/<database-name>'
            '?unix_socket=/cloudsql/<connection_name>'

Changing it to the following fixed it:

SQLALCHEMY_DATABASE_URI = (
            'mysql+pymysql://<user-name>:<password>@<host-name>/<database-name>'
            '?unix_socket=/cloudsql/{connection_name}'

Upvotes: 0

user3297763
user3297763

Reputation: 31

I had the same problem, and solved it. This problem is on Google Cloud SQL.

In the prompt console, re-start Cloud SQL like below,

gcloud sql instances --project [app engine project name] restart [sql instance name]

Upvotes: 3

IanGSY
IanGSY

Reputation: 3714

You should not specify the root password (even if you have set), when connecting from AppEngine.

Remove the passwd param from line 9 of your code so that it looks like:

db = MySQLdb.connect(unix_socket='/cloudsql/fluent-outlet-604:test-db' , db='guestbook', user='root')

The example code in this article also shows this.

Upvotes: 0

Related Questions