首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >拥有云MySQL授权的Google功能失败([Errno 111]连接被拒绝)

拥有云MySQL授权的Google功能失败([Errno 111]连接被拒绝)
EN

Stack Overflow用户
提问于 2021-04-22 12:45:03
回答 1查看 400关注 0票数 1

我试图通过Google函数连接到我的Google数据库来读取一些数据。函数构建成功,但在执行时只显示以下内容:

代码语言:javascript
运行
复制
Error: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)") (Background on this error at: http://sqlalche.me/e/e3q8)

这是我的连接代码:

代码语言:javascript
运行
复制
import sqlalchemy

# Depending on which database you are using, you'll set some variables differently. 
# In this code we are inserting only one field with one value. 
# Feel free to change the insert statement as needed for your own table's requirements.

# Uncomment and set the following variables depending on your specific instance and database:
connection_name = "single-router-309308:europe-west4:supermarkt-database"
db_name = "supermarkt-database"
db_user = "hidden"
db_password = "hidden"

# If your database is MySQL, uncomment the following two lines:
driver_name = 'mysql+pymysql'
query_string = dict({"unix_socket": "/cloudsql/{}".format(connection_name)})

# If the type of your table_field value is a string, surround it with double quotes. < SO note: I didn't really understand this line. Is this the problem?

def insert(request):
    request_json = request.get_json()
    stmt = sqlalchemy.text('INSERT INTO products VALUES ("Testid", "testname", "storename", "testbrand", "4.20", "1kg", "super lekker super mooi", "none")')
    
    db = sqlalchemy.create_engine(
      sqlalchemy.engine.url.URL(
        drivername=driver_name,
        username=db_user,
        password=db_password,
        database=db_name,
        query=query_string,
      ),
      pool_size=5,
      max_overflow=2,
      pool_timeout=30,
      pool_recycle=1800
    )
    try:
        with db.connect() as conn:
            conn.execute(stmt)
    except Exception as e:
        return 'Error: {}'.format(str(e))
    return 'ok'

我主要是从下面这个教程获得的:https://codelabs.developers.google.com/codelabs/connecting-to-cloud-sql-with-cloud-functions#0。我还使用了Python3.7,正如本教程中使用的那样。SQLAlchemy描述它不一定在程序员的控制之下。对于上下文,连接的帐户具有角色,是启用的。提前感谢您的帮助!

PS:我确实找到了这个答案:Connecting to Cloud SQL from Google Cloud Function using Python and SQLAlchemy,但是不知道在哪里可以找到使用SQL的防火墙设置。我没有在SQL > Connection /概述或防火墙中找到它们。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-23 08:01:38

好吧,所以我想出来了!在Edit Function > Runtime,Build和Connection中,转到Connection,并确保“仅通过VPC连接器将请求路由到私有IP”。VPC连接器需要不同的授权。

而且,显然我需要我的表名,而不是作为变量DB_NAME的数据库名。谢谢你的帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67213559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档