Photo by Tobias Fischer on Unsplash

How to connect Jupyter Notebook with a database using MySQL Connector

Alpar Gür

--

Jupyter Notebook is very handy and it’s fun to use when we’re working with data. In some cases we may want to connect to a database rather than loading a file every single time especially if the dataset is constantly growing.

In this post i will be showing you how to connect your Jupyter Notebook with a local / remote database using MySQL Connector. The only pre-requisites are having your Jupyter environment installed and a database to connect :)
So let’s get started!

First, you’ll need open terminal and install the mysql-connector package:

pip3 install mysql-connector

After installing the package open a Jupyter Notebook and follow the steps below.

import os
import mysql.connector

I would suggest you to declare your credentials and other connection informations as environment variables and then use them for a much more secure connection. Once you have your variables ready, you can enter the name of corresponding environment variables in brackets in the code below.

username = os.environ.get('')
password = os.environ.get('')
host = os.environ.get('')

--

--