繁体   English   中英

在 Python 中从计算引擎连接到数据库时出现 Google Cloud aiohttp 错误

[英]Google Cloud aiohttp error when connecting from compute engine to database in Python

我正在尝试将我的 Compute Engine 连接到 MySQL 数据库,这两个数据库都通过 Google Cloud 托管。 获取数据的程序在 Compute Engine 中运行,但是当我尝试将信息存储到数据库中时,出现以下错误:

Traceback (most recent call last):
  File "main.py", line 81, in <module>
    db="products"
  File "/home/jasper_wijnhoven/supermarktspider/googlec/cloud/sql/connector/connector.py", line 95, in connect
    return icm.connect(driver, timeout, **kwargs)
  File "/home/jasper_wijnhoven/supermarktspider/googlec/cloud/sql/connector/instance_connection_manager.py", line 328, in connect
    connection = connect_future.result(timeout)
  File "/usr/lib/python3.7/concurrent/futures/_base.py", line 432, in result
    return self.__get_result()
  File "/usr/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/home/jasper_wijnhoven/supermarktspider/googlec/cloud/sql/connector/instance_connection_manager.py", line 356, in _connect
    instance_data: InstanceMetadata = await self._current
  File "/home/jasper_wijnhoven/supermarktspider/googlec/cloud/sql/connector/instance_connection_manager.py", line 251, in _get_instance_data
    metadata, ephemeral_cert = await asyncio.gather(metadata_task, ephemeral_task)
  File "/home/jasper_wijnhoven/supermarktspider/googlec/cloud/sql/connector/refresh_utils.py", line 88, in _get_metadata
    resp = await client_session.get(url, headers=headers, raise_for_status=True)
  File "/home/jasper_wijnhoven/.local/lib/python3.7/site-packages/aiohttp/client.py", line 625, in _request
    resp.raise_for_status()
  File "/home/jasper_wijnhoven/.local/lib/python3.7/site-packages/aiohttp/client_reqrep.py", line 1005, in raise_for_status
    headers=self.headers,
aiohttp.client_exceptions.ClientResponseError: 403, message='Forbidden', url=URL('hidden for obvious reasons')

我不确定出了什么问题,因为当我自己尝试通过 URL 连接时,我收到 401 错误。 我按照这些步骤将我的 CE IP 列入白名单,以便与数据库连接。 有人可以给我指点吗?

这是用于设置连接器的代码:

connection = connector.connect(
    "single-router-309308:europe-west4:supermarkt-database",
    "mysql-connector",
    user="root",
    password="hidden again :)",
    db="products"
)

检查以确保您使用的服务帐户具有正确的权限:

  1. 它应该具有Cloud SQL client角色或更高级别
  2. 应启用云 SQL 管理员 API

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM