我正在try 使用一个python文件连接到PythonAnywhere上的MySQL8数据库.我已经把所有东西都设置好了,就像their help page号上面所列的那样.我有一个允许外部连接的高级帐户.

我使用相同的凭据将MySQL工作台连接到数据库,一切都正常工作. 然而,使用我的脚本中相同的凭据,使用日志(log)记录,只会挂在一行上.如果我将用户名、密码、端口号等更改为不正确,则它会意识到并引发异常.

完整的代码是:

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")

当我运行这个程序时,我看到"Hello Beer Tunes"和"Hello Mid Tunes",但没有进一步的内容.凭据和try/Except块中的任何错误都会捕获它,并打印"Hello end of file"行.

挂起时打印的包含日志(log)语句的完整列表如下:

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, curve25519-sha256@libssh.org, 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: chacha20-poly1305@openssh.com, aes128-ctr, aes192-ctr, aes256-ctr, aes128-gcm@openssh.com, aes256-gcm@openssh.com
DEBUG 2023-08-23 19:58:31,474 - server encrypt: chacha20-poly1305@openssh.com, aes128-ctr, aes192-ctr, aes256-ctr, aes128-gcm@openssh.com, aes256-gcm@openssh.com
DEBUG 2023-08-23 19:58:31,481 - client mac: umac-64-etm@openssh.com, umac-128-etm@openssh.com, hmac-sha2-256-etm@openssh.com, hmac-sha2-512-etm@openssh.com, hmac-sha1-etm@openssh.com, umac-64@openssh.com, umac-128@openssh.com, hmac-sha2-256, hmac-sha2-512, hmac-sha1
DEBUG 2023-08-23 19:58:31,481 - server mac: umac-64-etm@openssh.com, umac-128-etm@openssh.com, hmac-sha2-256-etm@openssh.com, hmac-sha2-512-etm@openssh.com, hmac-sha1-etm@openssh.com, umac-64@openssh.com, umac-128@openssh.com, hmac-sha2-256, hmac-sha2-512, hmac-sha1
DEBUG 2023-08-23 19:58:31,483 - client compress: none, zlib@openssh.com
DEBUG 2023-08-23 19:58:31,484 - server compress: none, zlib@openssh.com
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: curve25519-sha256@libssh.org
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,sk-ssh-ed25519@openssh.com,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com'}
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 "hostkeys-00@openssh.com"
DEBUG 2023-08-23 19:58:33,380 - Rejecting "hostkeys-00@openssh.com" 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不会导致它停止,只有垃圾箱似乎会停止.

我正在使用VSCode.我try 以管理员身份运行,没有任何变化.我已经try 了我能想到的所有方法,但除了错误,我无法得到任何不同的事情发生.我已经尽我所能地使用文档,但我对此还很陌生,我可能没有充分利用其中的信息. 我甚至试过ChatGPT,它建议我"咨询专家"…… 它检测到不正确的数据库密码这一事实告诉我们,SSH和可能的数据库连接工作正常.

任何建议都将不胜感激.

推荐答案

这是用与this question相同的解决方案解决的,默认情况下,use_pure设置为False,将其更改为True会使代码正常工作.如果代码有问题,应该自动从False恢复到True的东西似乎不起作用.

MySQL连接器文档中有更多信息here.

Mysql相关问答推荐

MySQL在使用SELECT*时不使用索引

不分组寻呼

MySQL-带有用户定义变量的IF-THEN语句

根据日期重叠转换表格

MySQL MDL(元数据锁)为什么会导致死锁?

在两个日期之间生成每个月的1行数据.

将 JSON 类型的列与特定字符串值进行比较

如何使用等于、喜欢和不等于在 json 字段上编写查询?

关联同一个表中的两列

为什么这个 NOT NULL 到 NULL 迁移会触发大量 I/O 操作?

限制正则​​表达式中多字符通配符的范围

插入重复键查询以在每一行上进行自定义更新

在 MySQL 中为多列创建索引以进行查询优化

如何通过未知列中的唯一值有效地更新 MySQL 行

估计行数 SQL

脚本超时,如果要完成导入,请重新提交相同的文件,导入将恢复

MySQL - 如何在 INSERT 语句中将字符串值解析为 DATETIME 格式?

在 Yii2 中执行原始 SQL 查询?

MySQL:唯一字段需要是索引吗?

匹配 IN 子句中的所有值