MySQL
DataCamp Workspace also supports a connection to a MySQL database. Using environment variables, you can connect your own database to your workspace. Make sure you understand how you can set up and connect environment variables securely to your workspace before setting up this specific integration. You can read this article for more info.
You can set up the MySQL integration as follows. The names you choose for your environment variables should be consistent with the ones you use in your workspace.
You can now use these environment variables to define a connection to a MySQL database. Check out this code snippet as an example:
1
from sqlalchemy import create_engine
2
import os
3
import pandas as pd
4
import pymysql.cursors
5
6
# Connect to the database
7
connection = pymysql.connect(
8
host=os.environ["MYSQL_HOST"],
9
user=os.environ["MYSQL_USERNAME"],
10
password=os.environ["MYSQL_PASSWORD"],
11
database=os.environ["MYSQL_NAME"],
12
cursorclass=pymysql.cursors.DictCursor)
13
14
# Execute a query and put result in pandas dataframe
15
sql_df = pd.read_sql(
16
"SELECT * FROM employees",
17
con=connection,
18
parse_dates=[
19
'birth_date',
20
'hire_date'
21
]
22
)
Copied!

Sample database: employees

We also provide you with a sample database about employees (source). If you want to connect to this database to get more familiar with the MySQL integration, there are only two things you will need to do.
First, open a new workspace and put the code from above in your workspace. (There is a template where you can start from coming soon.)
Next, connect following set of credentials to your workspace. These will give you access to the sample database.
  • MYSQL_HOST = workspacemysqldemodb.datacamp.com
  • MYSQL_NAME = employees
  • MYSQL_PASSWORD = workspace
  • MYSQL_USERNAME = workspace
Last modified 26d ago