MySQL Python连接器无连接,无错误

时间:2019-05-31 14:49:18

标签: python python-3.x mysql-python

当尝试使用Python MySQL连接器连接到我的家庭服务器上的远程MySQL Server时,它什么也没做。

MySQL服务器通过Docker在Ubuntu服务器18.04上运行,因此我需要一些SSH隧道才能通过,理论上它确实可以通过。我可以在MySQL工作台中看到该连接,但是它绝对不执行任何操作,并且该过程永远无法完成。

import mysql.connector
import sshtunnel


def db():
    _host = 'some_ip'
    _ssh_port = 22
    _username = 'some_user'
    _password = 'some_pass'
    _remote_bind_address = '0.0.0.0'
    _remote_mysql_port = 3306
    _local_bind_address = '127.0.0.1'
    _local_mysql_port = 3306
    _db_user = 'some_db_user'
    _db_password = 'some_db_pass'
    _db_name = 'testdb'
    with sshtunnel.SSHTunnelForwarder(
            (_host, _ssh_port),
            ssh_username=_username,
            ssh_password=_password,
            remote_bind_address=(_remote_bind_address, _remote_mysql_port),
            local_bind_address=(_local_bind_address, _local_mysql_port)
    ) as tunnel:
        print('Connecting...')
        connection = mysql.connector.connect(
            user=_db_user,
            password=_db_password,
            host=_local_bind_address,
            database=_db_name,
            port=_local_mysql_port)
        print('Connected')
        c = connection.cursor()
        c.execute('''INSERT INTO test (name, x, y, z) VALUES(asd, 1, 1, 1);''')
        connection.commit()
        connection.close()

db()

“已连接”永远不会被打印。

任何帮助将不胜感激!

0 个答案:

没有答案