PythonAnywhere中SSH到MySQL数据库无限期挂起,SSH正确,workbench可以完美连接

问题描述 投票:0回答:1

我正在尝试使用 python 文件连接到 PythonAnywhere 上的 MySQL 8 数据库。我已经按照他们的帮助页面上列出的完全设置了所有内容。我有一个允许外部连接的高级帐户。

使用相同的凭据,我已将 MySQL Workbench 连接到数据库,一切正常。 然而,使用我的脚本中的相同凭据和日志记录,只会挂在一行上。如果我将用户名、密码、端口号等更改为不正确的内容,那么它会意识到并引发异常。

完整代码为:

import mysql.connector
import sshtunnel
import logging
import sys

log_format = "%(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(
    stream=sys.stdout, filemode="w", format=log_format, level=logging.DEBUG
)

logger = logging.getLogger()
logger.propagate = True

sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0

# SSH tunnel configuration
ssh_host = "ssh.pythonanywhere.com"
ssh_username = "my_pythonanywhere_username"
ssh_password = "my_pythonanywhere_password"

# Database configuration
db_host = "my_pythonanywhere_username.mysql.pythonanywhere-services.com"
db_port = 3306
db_username = "my_pythonanywhere_username"
db_password = "my_password"

logger.info("Hello before tunnel")

# Establish SSH tunnel
try:
    with sshtunnel.SSHTunnelForwarder(
        (ssh_host),  # , ssh_port),
        ssh_username=ssh_username,
        ssh_password=ssh_password,
        remote_bind_address=(db_host, db_port),
    ) as tunnel:
        logger.info("Hello mid tunnel")
        db_config = {
            "user": db_username,
            "password": db_password,
            "host": "127.0.0.1",
            "port": tunnel.local_bind_port,
        }
        logger.info(tunnel.local_bind_port)
        db = mysql.connector.connect(**db_config)
        logger.info("Hello after tunnel")

        # Execute SQL command
        query = "SHOW DATABASES;"
        cursor = db.cursor()
        cursor.execute(query)

        # Fetch and print the result
        databases = cursor.fetchall()
        for database in databases:
            print(database[0])

        # Close cursor and connection

except Exception as e:
    logger.error("An error occurred: %s", str(e))
finally:
    if "cursor" in locals() and cursor:
        cursor.close()
    if "db" in locals() and db:
        db.close()
    if "tunnel" in locals() and tunnel:
        tunnel.close()

logger.info("Hello end of file")

当我运行此命令时,我看到“隧道前您好”和“隧道中您好”,但没有其他内容。凭证中的任何错误和 try/ except 块都会捕获它,并打印“Hello end of file”行。

挂起时打印的包含日志语句的完整列表是:

INFO 2023-08-23 19:58:31,099 - Hello before tunnel
DEBUG 2023-08-23 19:58:31,272 - starting thread (client mode): 0x9d33a440
DEBUG 2023-08-23 19:58:31,273 - Local version/idstring: SSH-2.0-paramiko_3.3.1
DEBUG 2023-08-23 19:58:31,376 - Remote version/idstring: SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.5
INFO 2023-08-23 19:58:31,376 - Connected (version 2.0, client OpenSSH_8.2p1)
DEBUG 2023-08-23 19:58:31,474 - === Key exchange possibilities ===
DEBUG 2023-08-23 19:58:31,474 - kex algos: curve25519-sha256, [email protected], ecdh-sha2-nistp256, ecdh-sha2-nistp384, ecdh-sha2-nistp521, diffie-hellman-group-exchange-sha256, diffie-hellman-group16-sha512, diffie-hellman-group18-sha512, diffie-hellman-group14-sha256
DEBUG 2023-08-23 19:58:31,474 - server key: rsa-sha2-512, rsa-sha2-256, ssh-rsa
DEBUG 2023-08-23 19:58:31,474 - client encrypt: [email protected], aes128-ctr, aes192-ctr, aes256-ctr, [email protected], [email protected]
DEBUG 2023-08-23 19:58:31,474 - server encrypt: [email protected], aes128-ctr, aes192-ctr, aes256-ctr, [email protected], [email protected]
DEBUG 2023-08-23 19:58:31,481 - client mac: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], hmac-sha2-256, hmac-sha2-512, hmac-sha1
DEBUG 2023-08-23 19:58:31,481 - server mac: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], hmac-sha2-256, hmac-sha2-512, hmac-sha1
DEBUG 2023-08-23 19:58:31,483 - client compress: none, [email protected]
DEBUG 2023-08-23 19:58:31,484 - server compress: none, [email protected]
DEBUG 2023-08-23 19:58:31,485 - client lang: <none>
DEBUG 2023-08-23 19:58:31,485 - server lang: <none>
DEBUG 2023-08-23 19:58:31,486 - kex follows: False
DEBUG 2023-08-23 19:58:31,487 - === Key exchange agreements ===
DEBUG 2023-08-23 19:58:31,488 - Kex: [email protected]
DEBUG 2023-08-23 19:58:31,489 - HostKey: rsa-sha2-512
DEBUG 2023-08-23 19:58:31,489 - Cipher: aes128-ctr
DEBUG 2023-08-23 19:58:31,490 - MAC: hmac-sha2-256
DEBUG 2023-08-23 19:58:31,491 - Compression: none
DEBUG 2023-08-23 19:58:31,491 - === End of kex handshake ===
DEBUG 2023-08-23 19:58:31,601 - kex engine KexCurve25519 specified hash_algo <built-in function openssl_sha256>
DEBUG 2023-08-23 19:58:31,602 - Switch to new keys ...
DEBUG 2023-08-23 19:58:31,603 - Got EXT_INFO: {'server-sig-algs': b'ssh-ed25519,[email protected],ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected]'}
DEBUG 2023-08-23 19:58:31,603 - Attempting password auth...
DEBUG 2023-08-23 19:58:31,834 - userauth is OK
INFO 2023-08-23 19:58:31,957 - Auth banner: b'<<<<<<:>~ PythonAnywhere SSH. Help @ https://help.pythonanywhere.com/pages/SSHAccess\n'
INFO 2023-08-23 19:58:31,972 - Authentication (password) successful!
INFO 2023-08-23 19:58:31,977 - Hello mid tunnel
INFO 2023-08-23 19:58:31,978 - 49493
DEBUG 2023-08-23 19:58:32,020 - [chan 0] Max packet in: 32768 bytes
DEBUG 2023-08-23 19:58:33,380 - Received global request "[email protected]"
DEBUG 2023-08-23 19:58:33,380 - Rejecting "[email protected]" global request from server.
DEBUG 2023-08-23 19:58:33,522 - [chan 0] Max packet out: 32768 bytes
DEBUG 2023-08-23 19:58:33,522 - Secsh channel 0 opened.

打印最后一行后,程序就挂起。 Ctrl+C 不会导致它停止,只有 bin 似乎会停止。

我正在使用 VSCode。我试过以管理员身份运行,没有任何变化。我已经尝试了我能想到的一切,但除了错误之外,我无法让任何不同的事情发生。我已经对文档做了我能做的事情,但我对此还很陌生,而且我可能没有充分利用其中的信息。 我什至尝试了 ChatGPT,它建议我“咨询专家”...... 它检测到不正确的数据库密码这一事实告诉 e,SSH 和可能的数据库连接工作正常。

如有任何建议,我们将不胜感激。

mysql mysql-connector pythonanywhere ssh-tunnel
1个回答
0
投票

这个问题的解决方案与这个问题相同,

use_pure
默认设置为
False
,将其更改为
True
可以使代码正常工作。如果代码有问题,应该自动从
False
恢复到
True
的东西似乎无法正常工作。

MySQL.connector 文档有更多信息这里

© www.soinside.com 2019 - 2024. All rights reserved.