简体   繁体   English

Python mysql-connector 在通过 SSH 连接到远程 mysql 时无限期挂起

[英]Python mysql-connector hangs indefinitely when connecting to remote mysql via SSH

I am Testing out connection to mysql server with python. I need to ssh into the server and establish a mysql connection.我正在测试使用 python 连接到 mysql 服务器。我需要将 ssh 连接到服务器并建立 mysql 连接。 The following code works:以下代码有效:

from sshtunnel import SSHTunnelForwarder
import pymysql
import mysql.connector
    
with SSHTunnelForwarder((ssh_host, 22), ssh_username=ssh_user, ssh_password=ssh_password,
            remote_bind_address=("127.0.0.1", 3306)) as tunnel:

    config = {
                    'user': user,
                    'password': password,
                    'host': tunnel.local_bind_host,
                    'port': tunnel.local_bind_port,
                    'database': db
                }
    
    conn = pymysql.connect(**config)
    query = '''SELECT VERSION();'''
    data = pd.read_sql_query(query, conn)
    print(data)
    connection.close()

However, when using mysql.connector instead of pymysql such as below:但是,当使用mysql.connector而不是pymysql时,如下所示:

with SSHTunnelForwarder((ssh_host, 22), ssh_username=ssh_user, ssh_password=ssh_password,
                remote_bind_address=("127.0.0.1", 3306)) as tunnel:

        config = {
                        'user': user,
                        'password': password,
                        'host': tunnel.local_bind_host,
                        'port': tunnel.local_bind_port,
                        'database': db
                    }
        
        conn = mysql.connector.connect(**config)
        mycursor = cnx.cursor()
        mycursor.execute("SELECT VERSION()")
        myresult = mycursor.fetchall()

The code stops at conn = mysql.connector.connect(**config) .代码在conn = mysql.connector.connect(**config)处停止。 It never gives an error or stops, it just hangs on this line.它永远不会出错或停止,它只是挂在这条线上。

Why is this?为什么是这样?

Aren't the config attributes valid for this module?该模块的配置属性是否有效?

As there seems to be an aswer here I followed the comment from @André Restivo and it seems to work for me, set use_pure to True .因为这里似乎有一个答案,所以我遵循了@André Restivo 的评论,它似乎对我有用,将use_pure设置为True I'm not sure what does this exactly do:我不确定这到底是做什么的:

    def __get_db_connection(self) -> None:
        """
        Connect to the database
        """
        try:
            logger.debug(f'Connecting to mysql db "{self.config.mysql_database}"...')
            with mysql.connector.connect(
                host=self.config.mysql_ip,
                port=self.tunnel.local_bind_port,
                user=self.config.mysql_user,
                password=self.config.mysql_password,
                database=self.config.mysql_database,
                use_pure=True
            ) as connection:
                self.connection = connection
                logger.info(connection)
        except mysql.connector as e:
            logger.error(e)
            raise

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

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