简体   繁体   English

如何将 GCP 项目中的 MySQL 实例连接到 AWS Lambda function?

[英]How to connect MySQL instance from GCP project to AWs Lambda function?

I've hosted my MySQL instance in GCP project and I want to use it's database in AWS Lambda Function.我在 GCP 项目中托管了我的 MySQL 实例,我想在 AWS Lambda Function 中使用它的数据库。 I've tried all the ways to connect to my DB in MySQL instance in GCP but the Lambda Function give me Timeout Error even though I've kept my Timeout period enough to run the function. I've tried all the ways to connect to my DB in MySQL instance in GCP but the Lambda Function give me Timeout Error even though I've kept my Timeout period enough to run the function. I've also Zipped the Package with MySQL and pymysql installed and then uploaded to Lambda but the issues still persists.我还压缩了 Package 并安装了 MySQL 和 pymysql,然后上传到 Lambda,但问题仍然存在。

Here's the code that I've written for connecting to my DB:这是我为连接到我的数据库而编写的代码:

import json
import boto3
import mysql.connector
import MySQLdb

def lambda_handler(event, context):
    mydb = MySQLdb.connect(
    host="Public Ip of MySQL Instance",
    user="Username",
    password="Password",
    db="DbName"
    )
    cur = db.cursor()
    cur.execute("SELECT * FROM budget")
    for row in cur.fetchall():
        print(row[0])
    
    db.close()

Here's the Error that I receive:这是我收到的错误:

{
  "errorMessage": "(2003, \"Can't connect to MySQL server on '36.71.43.131' (timed out)\")",
  "errorType": "OperationalError",
  "stackTrace": [
    "  File \"/var/lang/lib/python3.8/imp.py\", line 234, in load_module\n    return load_source(name, filename, file)\n",
    "  File \"/var/lang/lib/python3.8/imp.py\", line 171, in load_source\n    module = _load(spec)\n",
    "  File \"<frozen importlib._bootstrap>\", line 702, in _load\n",
    "  File \"<frozen importlib._bootstrap>\", line 671, in _load_unlocked\n",
    "  File \"<frozen importlib._bootstrap_external>\", line 783, in exec_module\n",
    "  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\n",
    "  File \"/var/task/lambda_function.py\", line 10, in <module>\n    connection = pymysql.connect(host='36.71.43.131',\n",
    "  File \"/var/task/pymysql/connections.py\", line 353, in __init__\n    self.connect()\n",
    "  File \"/var/task/pymysql/connections.py\", line 664, in connect\n    raise exc\n"
  ]
}

Please help me to resolve this.请帮我解决这个问题。 I've tried all different ways to connect to my SQL instance but nothing works.我尝试了所有不同的方法来连接到我的 SQL 实例,但没有任何效果。

According to the error message, AWS Lambdathe tried to connect the Public IP address of MySQL instance directly.根据错误信息,AWS Lambda尝试直接连接MySQL实例的Public IP地址。

You have to configure your MySQL instance to have a public IPv4 address, and to accept connections from specific IP addresses or a range of addresses by adding authorized addresses to your instance .您必须将 MySQL 实例配置为具有公共 IPv4 地址,并通过向您的实例添加授权地址来接受来自特定 IP 地址或地址范围的连接。

To configure access to your MySQL instance:要配置对 MySQL 实例的访问权限:

  1. From the client machine, use What's my IP to see the IP address of the client machine.在客户端机器上,使用 What's my IP 查看客户端机器的 IP 地址。
  2. Copy that IP address.复制该 IP 地址。
  3. Go to the Cloud SQL Instances page in the Google Cloud Console. Go 到 Google Cloud Console 中的 Cloud SQL 实例页面。
  4. Click the instance to open its Overview page, and record its IP address.单击实例打开其概览页面,并记录其 IP 地址。
  5. Select the Connections tab. Select 连接选项卡。
  6. Under Authorized networks, click Add network and enter the IP address of the machine where the client is installed.在授权网络下,单击添加网络并输入安装客户端的机器的 IP 地址。 Note: The IP addresses must be IPv4.注意:IP 地址必须是 IPv4。 That is, the IP addresses of the instance, and of the client machine that you authorize, both must be IPv4.也就是说,实例的 IP 地址和您授权的客户端计算机的地址都必须是 IPv4。
  7. Click Done.单击完成。 Then click Save at the bottom of the page to save your changes.然后单击页面底部的保存以保存您的更改。

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

相关问题 无法通过AWS Lambda函数连接到在AWS EC2上运行的MySQL实例 - Unable to connect to MySQL instance running on AWS EC2 from AWS Lambda function 无法从AWS Lambda连接到默认VPC中的AWS RDS实例 - unable to connect to AWS RDS instance in default VPC from AWS Lambda 如何从终端连接到 AWS mysql 数据库实例 - How can I connect, from the terminal, to AWS mysql DB instance 如何从localstack中的Lambda函数连接到mysql? - How do I connect to mysql from a Lambda function in localstack? 如何将Rstudio连接到MySQL数据库的AWS实例? - How to connect Rstudio to AWS instance of mysql Database? 使用 Z3B2819DD4C24EDA2FAF2052EEF449555Z 从 AWS Lambda function 连接到 MySql 数据库 - Connecting to MySql database from AWS Lambda function using Node.js, no connect callback 如何在 AWS 中编写一个 Lamdda function 以允许我连接到运行 MySQL 的 EC2 实例 - How do I write a Lamdda function in AWS that allows me to connect to an EC2 instance that is running MySQL 我可以从 Windows MySQL Workbench 连接到 GCP CE VM 实例上的 MySQL 吗? - can I connect to MySQL on a GCP CE VM instance from Windows MySQL Workbench? 无法从Doctrine PHP连接到AWS上的MySQL实例 - Cannot connect to MySQL Instance on AWS From Doctrine PHP 如何从 gcp 应用引擎连接我自己的 pc mysql? - How to connect my own pc mysql from gcp app engine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM