【问题标题】:Connecting to Google Cloud SQL from App Engine: access denied从 App Engine 连接到 Google Cloud SQL:访问被拒绝
【发布时间】:2017-01-12 17:20:40
【问题描述】:

尝试从 App Engine 连接到 Google Cloud SQL。收到错误 500 并且日志显示:“用户 'root'@'cloudsqlproxy~' 的访问被拒绝(使用密码:否)”)。 App Engine 和云 SQL 在同一个项目中。我用来连接的代码:

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/<project-id>:europe-west1:<instance-name>',
            user='root')

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

    self.response.write('pong')

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

据我了解,我所做的一切都是正确的。上面的代码我有效地复制粘贴了一个可以在 Google Dev Console 中查看的 sn-p。 StackExchange 上到处都说我不应该传递密码,所以我也不这样做。我在创建实例时更改了 root 用户密码。我不明白为什么数据库会拒绝访问 App Engine,他们在同一个项目上!有没有办法检查数据库的权限是什么?

【问题讨论】:

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


    【解决方案1】:

    好的,事实证明发生了一些错误信息。添加密码参数解决了我的问题。但是由于某种原因,Google 文档中没有提及这一点,StackOverflow 上的许多帖子实际上直接告诉您相反的情况,说如果您从 AppEngine 登录时传递密码,Cloud SQL 会抛出错误。也许最近的一些更新是造成这种情况的原因,但就我而言,传递密码解决了这个问题。希望对您有所帮助!

    【讨论】:

      【解决方案2】:

      我想说这部分在 Google Developer Console 上没有很好的记录。

      Google Cloud SQL 2nd Gen 需要对连接字符串进行一些修改,而您的代码中没有提供。

      请在我这边修改后查看原始代码:

      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/<project-id>:europe-west1<instance-name>, 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)
      

      问候。

      【讨论】:

        猜你喜欢
        • 2020-05-29
        • 1970-01-01
        • 2019-03-29
        • 2014-10-29
        • 2020-07-27
        • 1970-01-01
        • 2017-06-17
        • 2020-06-07
        • 2014-08-02
        相关资源
        最近更新 更多