Connect with mysql.connector with ssh tunnel in Python 3.7

Overview

When I try to SSH to MySQL with Python 3.7, it hangs and Mahatter, so a memo at that time

What I tried

I hung up when I tried to log in to MySQL running on an Ubuntu server using Paramiko. When I searched the Web, it was written that sshtunnel could be easily done by using mysql.connector, so I tried it. I tried the sample code but it hangs on a MySQL connection.

After investigating the cause of the hang, it was written that "server.daemon_forward_servers = True" should be set. However, it hangs even if it is set. In addition, the following is written on the page of the sshtunnel project. In short, this setting is unnecessary.

Remove useless daemon_forward_servers = True hack for hangs prevention (is not fully backward compatible)

Furthermore, when I searched the Web, it was written that pymysql can connect normally. However, there are other problems with pymysql, so I decided to use mysql.connector this time.

Countermeasures

I was able to connect after specifying the "use_pure = True" parameter in mysql.connector.connect ().

import mysql.connector
from sshtunnel import SSHTunnelForwarder

    with SSHTunnelForwarder(
            ('<Server IP>', 22),
            ssh_username="<User name for server login>",
            ssh_password="<Password for server login>",
            remote_bind_address=('127.0.0.1', 3306)) as server:
        db_connect = mysql.connector.connect(
            host='127.0.0.1',
            port=server.local_bind_port,
            user='<User name for MySQL>',
            password='<Password for MySQL>',
            database='<Database to connect>',
            use_pure=True
        )
        db_curs = db_connect.cursor()
        sql_cmd = f"SELECT * FROM nic_devices WHERE mac_address='{mac_address}'"
        db_curs.execute(sql_cmd)
        result = db_curs.fetchall()
        db_curs.close()
        db_connect.close()
        print(result)

that's all

Recommended Posts

Connect with mysql.connector with ssh tunnel in Python 3.7
Connect to BigQuery with Python
Scraping with selenium in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Connect to Wikipedia with Python
Working with sounds in Python
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Create a child account for connect with Stripe in Python
Number recognition in images with Python
Testing with random numbers in Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Password generation in texto with python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Get started with Python in Blender
Working with DICOM images in Python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Try logging in to qiita with Python
Stress Test with Locust written in Python
Python3> in keyword> True with partial match?
Manually ssh registration for coreserver with python
Device monitoring with On-box Python in IOS-XE
Try working with binary data in Python
Draw Nozomi Sasaki in Excel with python
Tips for dealing with binaries in Python
Display Python 3 in the browser with MAMP
Page cache in Python + Flask with Flask-Caching
Connect to MySQL with Python within Docker
Post Test 3 (Working with PosgreSQL in Python)
How to work with BigQuery in Python
Playing card class in Python (with comparison)
Process multiple lists with for in Python
One liner webServer (with CGI) in python
Get Started with TopCoder in Python (2020 Edition)
Debug for mysql connection with python mysql.connector
Easy image processing in Python with Pillow
Connect to s3 with AWS Lambda Python
Connect to pepper with PEPPER Mac's python interpreter
To work with timestamp stations in Python
Call APIGateWay with APIKey in python requests
[ev3dev × Python] SSH Control (remote control with keyboard)
Read text in images with python OCR
Introduced sip-4.14 in python3.2.2 environment with MacOS 10.7.4
Quadtree in Python --2
Python in optimization
CURL in python
FizzBuzz with Python3
Metaprogramming in Python