通过SSH隧道连接到MySQL | MySQL连接不可用

时间:2020-07-19 12:54:12

标签: python mysql ssh

我正在使用以下python代码段在共享托管服务器上连接我的MySQL数据库。

import mysql.connector
import sshtunnel
with sshtunnel.SSHTunnelForwarder(
    ('server.web-hosting.com', 21098),
    ssh_username = 'ssh_username',
    ssh_password = 'ssh_pass!23',
    remote_bind_address = ('127.0.0.1', 3306)
) as tunnel:
connection = mysql.connector.MySQLConnection(
        user = 'db_user',
        password = 'db_pass',
        host = '127.0.0.1',
        port = tunnel.local_bind_port,
        database = 'demo',
    )
mycursor = connection.cursor()
query = "SELECT * FROM sample_table"
mycursor.execute(query)

我收到以下错误。我仍然可以使用DBeaver连接到数据库。

MySQL Connection not available.

0 个答案:

没有答案