Get data from MySQL on a VPS with Python 3 and SQLAlchemy

It is a method for. If you write only the conclusion,

from sshtunnel import SSHTunnelForwarder

It can be anything. But I forget it every time.

Setting Example

import os
from sshtunnel import SSHTunnelForwarder
from sqlalchemy import create_engine
import pandas as pd

URL = "{connector}://{user}:{password}@{host}:{port}/{db}?charset=utf8".format(
    connector='mysql+mysqlconnector',
    user='VPS Mysql username',
    password='VPS MySQL password',
    host='localhost',
    port='3307',
    db='DB name you want to use')

SSH_INFO = {
    'ssh_host': 'hoge.sakura.ne.jp',  #IP direct hit is OK
    'ssh_port': 22,  #Port that is usually used for SSH connection
    'ssh_username': 'SSH username',
    'ssh_pkey': os.path.expanduser('~/.ssh/id_rsa'),  #Path to private key
    'remote_bind_port': 3306,  #remote 3306
    'local_bind_port': 3307,  #To local port 3307
}

engine = create_engine(URL)


def with_ssh(func):
    def ssh_func(*args, **kwargs):
        with SSHTunnelForwarder(
            (SSH_INFO['ssh_host'], SSH_INFO['ssh_port']),
            ssh_username=SSH_INFO['ssh_username'],
            ssh_pkey=SSH_INFO['ssh_pkey'],
            remote_bind_address=('localhost', SSH_INFO['remote_bind_port']),
            local_bind_address=('localhost', SSH_INFO['local_bind_port']),
        ):
            return func(*args, **kwargs)
    return ssh_func


@with_ssh
def read_remote_sql(query):
    df = pd.read_sql(query, engine)
    return df


read_remote_sql('select * from {}'.format('t_piyo'))

Recommended Posts

Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Notes on importing data from MySQL or CSV with Python
Receive textual data from mysql with python
[Note] Get data from PostgreSQL with Python
A memo with Python2.7 and Python3 on CentOS
Folium: Visualize data on a map with Python
Extract data from a web page with Python
Make a decision tree from 0 with Python and understand it (4. Data structure)
Get financial data with python (then a little tinkering)
Get Youtube data with python
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Get comments on youtube Live with [python] and [pytchat]!
Get mail from Gmail and label it with Python3
Get data from database via ODBC with Python (Access)
Get a large amount of Starbucks Twitter data with python and try data analysis Part 1
[CGI] Run the Python program on the server with Vue.js + axios and get the output data
A memo that reads data from dashDB with Python & Spark
Get rid of dirty data with Python and regular expressions
Hash with python and escape from a certain minister's egosa
Get data from analytics API with Google API Client for python
Collecting information from Twitter with Python (MySQL and Python work together)
Get additional data to LDAP with python (Writer and Reader)
Get data from Quandl in Python
Get the matched string with a regular expression and reuse it when replacing on Python3
Create a decision tree from 0 with Python and understand it (3. Data analysis library Pandas edition)
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Get data from your website on a regular basis using ScraperWiki
Get OCTA simulation conditions from a file and save with pandas
Get additional data in LDAP with python
Get html from element with Python selenium
Add a Python data source with Redash
Map rent information on a map with python
Connecting from python to MySQL on CentOS 6.4
Let's do MySQL data manipulation with Python
Get data from an oscilloscope with pyVISA
2. Make a decision tree from 0 with Python and understand it (2. Python program basics)
[Data science basics] I tried saving from csv to mysql with python
Deploy a Python app on Google App Engine and integrate it with GitHub
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
Data integration from Python app on Linux to Amazon Redshift with ODBC
Set cron from 1 on Ubuntu 16.04 (Sakura VPS) and execute python program regularly
[Personal memo] Get data on the Web and make it a DataFrame
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
Send and receive binary data via serial communication with python3 (on mac)
Data integration from Python app on Windows to Amazon Redshift with ODBC
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Create a decision tree from 0 with Python and understand it (5. Information Entropy)
Install Chrome on the command line on Sakura VPS (Ubuntu) and launch Chrome with python from virtual display and selenium
[Python] A data infrastructure for acquiring and publishing tweets from Twitter API to BigQuery was built on GCP (with negative / positive score).
Get time series data from k-db.com in Python
[Python] Get the files in a folder with Python
Get a ticket for a theme park with python
Building a python environment with virtualenv and direnv
Build a python environment with ansible on centos6
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
[Python] How to read data from CIFAR-10 and CIFAR-100
Get stock price data with Quandl API [Python]
I tried to get CloudWatch data with Python
Get git branch name and tag name with python
A story stuck with handling Python binary data