【问题标题】:Cloud SQL Access deniedCloud SQL 访问被拒绝
【发布时间】:2014-08-02 01:05:01
【问题描述】:
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)

当我将此应用程序部署到应用程序引擎时,我收到错误消息 "(1045, "访问被拒绝用户'root'@'localhost' (使用密码: YES)")

【问题讨论】:

    标签: mysql python-2.7 google-app-engine google-cloud-sql


    【解决方案1】:

    我遇到了同样的问题,并解决了。 这个问题出在 Google Cloud SQL 上。

    在提示控制台中,重新启动 Cloud SQL,如下所示,

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

    【讨论】:

      【解决方案2】:

      从 AppEngine 连接时,您不应指定 root 密码(即使您已设置)。

      从代码的第 9 行删除 passwd 参数,使其看起来像:

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

      this article 中的示例代码也显示了这一点。

      【讨论】:

        【解决方案3】:

        当我没有在数据库 URI 中指定主机名时,我看到了这个问题:

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

        将其更改为以下修复它:

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

        【讨论】:

          猜你喜欢
          • 2020-05-29
          • 2017-01-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-10
          • 1970-01-01
          • 2023-03-18
          相关资源
          最近更新 更多