谷歌云上的sshtunnel转发和mysql连接功能

2024-09-28 21:28:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试通过我的一个谷歌云功能上的ssh隧道连接到MySQL服务器。这在我的家庭环境中效果很好。我认为这是云功能的一些端口问题

编辑:为了澄清问题,MySQL服务器位于一个共享托管web服务器上。不是谷歌云SQL

每次运行此命令时,我都会超时并显示“未知错误”。这条隧道似乎很成功。但是,我无法让mysql连接正常工作

import base64
import sshtunnel
import mysql.connector

def testing(event, context):
    """
    Testing function
    """
    with sshtunnel.SSHTunnelForwarder(
        ("server address", port),
        ssh_username="user",
        ssh_password="password",
        remote_bind_address=("127.0.0.1",3306),
    ) as server:
        print(server.local_bind_port)
        with mysql.connector.connect(
            user="user",
            password="password",
            host="localhost",
            database="database",
            port=server.local_bind_port
        ) as connection:
            print(connection)

enter image description here


Tags: import功能服务器connectorserverbindportwith
1条回答
网友
1楼 · 发布于 2024-09-28 21:28:57

要列出的步骤太多了,但我想知道“连接器”设置是否对SSH也有影响。也许您必须创建一个连接器,如图here(注意“私有IP”选项卡中的说明与本地计算机上的不同)。然后,配置云函数以使用该连接器。确保您也使用了正确的端口

A Serverless VPC Access connector handles communication to your VPC network. To connect directly with private IP, you need to:

  1. Make sure that the Cloud SQL instance created above has a private IP address. If you need to add one, see the Configuring private IP page for instructions.

  2. Create a Serverless VPC Access connector in the same VPC network as your Cloud SQL instance. Unless you're using Shared VPC, a connector must be in the same project and region as the resource that uses it, but the connector can send traffic to resources in different regions. Serverless VPC Access supports communication to VPC networks connected via Cloud VPN and VPC Network Peering. Serverless VPC Access does not support legacy networks.

  3. Configure Cloud Functions to use the connector.

  4. Connect using your instance's private IP and port 3306.

请记住,此“未知”错误也可能是由于未启用云SQL管理API here。事实上,请确保你关注整个页面,因为这是一个广泛的问题

让我们知道是什么导致了这种错误

相关问题 更多 >