Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
允许Python通过SSH连接到MySQL_Python_Mysql_Ssh - Fatal编程技术网

允许Python通过SSH连接到MySQL

允许Python通过SSH连接到MySQL,python,mysql,ssh,Python,Mysql,Ssh,我将pymysql与Python3.8结合使用,以允许Python连接到另一台MySQL服务器。我得到一个错误: with pymysql.connect('127.0.0.1', user = '123', password = '123', port = server.local_bind_port) as connection: AttributeError: __enter__ python: from sshtunnel import SSHTunnelForwarder

我将pymysql与Python3.8结合使用,以允许Python连接到另一台MySQL服务器。我得到一个错误:

with pymysql.connect('127.0.0.1', user = '123', password = '123', port = server.local_bind_port) as connection:
AttributeError: __enter__
python:

    from sshtunnel import SSHTunnelForwarder
    import pymysql
    
    
    with SSHTunnelForwarder(
        ('76.21.187.192', 2222),
        ssh_username = '123', 
        ssh_password = '123',
        remote_bind_address = ('127.0.0.1', 3306)) as server: 
    
        with pymysql.connect('127.0.0.1', user = '123', password = '123', port = server.local_bind_port) as connection:
            print('OK')

如何正确连接到MySQL?

在python中使用with块时,with语句中的对象调用其enter方法,with中的块运行,然后调用exit(如果引发异常信息,则可以选择使用异常信息)。因此,如果您的类中没有定义enter,您将看到此错误

或者,您可以使用以下语句代替上述语句:

connection=pymysql.connect('127.0.0.1',user='123',password='123',port=server.local\u bind\u port)