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



我正在尝试将我的计算引擎连接到MySQL数据库,这两个数据库都通过谷歌云托管。获取数据的程序在计算引擎中工作,但当我试图将信息存储到数据库中时,我会收到以下错误:

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列入白名单,以便与DB连接。有人能给我一个指针吗?

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

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

相关内容

  • 没有找到相关文章

最新更新