Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to MySQL db from Jupyter notebook

I am using Jupyter Notebooks to learn Python. I would like to connect to a MySQL db hosted locally hosted through MAMP. How would I approach this?

like image 278
coderatlarge Avatar asked Jun 21 '18 16:06

coderatlarge


Video Answer


1 Answers

import os
import pymysql
import pandas as pd

host = os.getenv('MYSQL_HOST')
port = os.getenv('MYSQL_PORT')
user = os.getenv('MYSQL_USER')
password = os.getenv('MYSQL_PASSWORD')
database = os.getenv('MYSQL_DATABASE')

conn = pymysql.connect(
    host=host,
    port=int(3306),
    user="root",
    passwd=password,
    db="[YOUR_DB_NAME]",
    charset='utf8mb4')

df = pd.read_sql_query("SELECT * FROM YOUR_TABLE",
    conn)
df.tail(10)
like image 113
tammuz Avatar answered Sep 21 '22 07:09

tammuz