Skip to content

Instantly share code, notes, and snippets.

@amirziai
Last active October 13, 2021 04:08
Show Gist options
  • Save amirziai/9cc792e41d3241a17a18 to your computer and use it in GitHub Desktop.
Save amirziai/9cc792e41d3241a17a18 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Query your database over an SSH tunnel with Pandas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Blog post related to this notebook is here:<br>\n",
"https://medium.com/@amirziai/query-your-database-over-an-ssh-tunnel-with-pandas-603ce49b35a1#.4dzdcrl48"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### SSH and database information"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# ssh variables\n",
"host = '52.xx.xx.xx'\n",
"localhost = '127.0.0.1'\n",
"ssh_username = 'ubuntu'\n",
"ss_private_key = '/path/to/key.pem'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# database variables\n",
"user='user'\n",
"password='verysecurepassword'\n",
"database='database'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Install sshtunnel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"!pip install sshtunnel"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Import packages"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from sshtunnel import SSHTunnelForwarder\n",
"import MySQLdb as db\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The query function"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def query(q):\n",
" with SSHTunnelForwarder(\n",
" (host, 22),\n",
" ssh_username=ssh_username,\n",
" ssh_private_key=private_key,\n",
" remote_bind_address=(localhost, 3306)\n",
" ) as server:\n",
" conn = db.connect(host=localhost,\n",
" port=server.local_bind_port,\n",
" user=user,\n",
" passwd=password,\n",
" db=database)\n",
"\n",
" return pd.read_sql_query(q, conn)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Query time"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df = query('select * from titanic limit 5')"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>PassengerId</th>\n",
" <th>Pclass</th>\n",
" <th>Name</th>\n",
" <th>Sex</th>\n",
" <th>Age</th>\n",
" <th>SibSp</th>\n",
" <th>Parch</th>\n",
" <th>Ticket</th>\n",
" <th>Fare</th>\n",
" <th>Cabin</th>\n",
" <th>Embarked</th>\n",
" <th>Survived</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>Braund, Mr. Owen Harris</td>\n",
" <td>male</td>\n",
" <td>22</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>A/5 21171</td>\n",
" <td>7.2500</td>\n",
" <td>NaN</td>\n",
" <td>S</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
" <td>female</td>\n",
" <td>38</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>PC 17599</td>\n",
" <td>71.2833</td>\n",
" <td>C85</td>\n",
" <td>C</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>3</td>\n",
" <td>Heikkinen, Miss. Laina</td>\n",
" <td>female</td>\n",
" <td>26</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>STON/O2. 3101282</td>\n",
" <td>7.9250</td>\n",
" <td>NaN</td>\n",
" <td>S</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4</td>\n",
" <td>1</td>\n",
" <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
" <td>female</td>\n",
" <td>35</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>113803</td>\n",
" <td>53.1000</td>\n",
" <td>C123</td>\n",
" <td>S</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5</td>\n",
" <td>3</td>\n",
" <td>Allen, Mr. William Henry</td>\n",
" <td>male</td>\n",
" <td>35</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>373450</td>\n",
" <td>8.0500</td>\n",
" <td>NaN</td>\n",
" <td>S</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" PassengerId Pclass Name \\\n",
"0 1 3 Braund, Mr. Owen Harris \n",
"1 2 1 Cumings, Mrs. John Bradley (Florence Briggs Th... \n",
"2 3 3 Heikkinen, Miss. Laina \n",
"3 4 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) \n",
"4 5 3 Allen, Mr. William Henry \n",
"\n",
" Sex Age SibSp Parch Ticket Fare Cabin Embarked \\\n",
"0 male 22 1 0 A/5 21171 7.2500 NaN S \n",
"1 female 38 1 0 PC 17599 71.2833 C85 C \n",
"2 female 26 0 0 STON/O2. 3101282 7.9250 NaN S \n",
"3 female 35 1 0 113803 53.1000 C123 S \n",
"4 male 35 0 0 373450 8.0500 NaN S \n",
"\n",
" Survived \n",
"0 0 \n",
"1 1 \n",
"2 1 \n",
"3 1 \n",
"4 0 "
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@AolingHmlet
Copy link

Hi May I know how to write the dataframe into the database through sshtunnel? It seems that I am only able to read but not write. Would you have any clues? Thanks a lot.

@AntonLohner
Copy link

Hi May I know how to write the dataframe into the database through sshtunnel? It seems that I am only able to read but not write. Would you have any clues? Thanks a lot.

You should be able to use the same method, replacing SELECT with INSERT or UPDATE sql statements to write the information back in, what isn't working?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment