Step 1: Update .zshrc
File with Additional Database Details
If you want to add credentials or another database, you can simply add a new set of environment variables to your .zshrc
file.
Open your .zshrc
file in VS Code (or any text editor):
code ~/.zshrc
Add the new database credentials at the bottom of the file, just like you did for the previous database. You can name the variables differently to avoid conflicts.
For example, if the new database is called new_database
:
# MySQL credentials for classicmodels
export DB_USERNAME="root"
export DB_PASSWORD="Password1234"
export DB_HOST="localhost"
export DB_PORT="3306"
export DB_NAME="classicmodels"
# Construct the MySQL URL
export DATABASE_URL="mysql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
# MySQL credentials for student_db
export STUDENT_DB_USERNAME="root"
export STUDENT_DB_PASSWORD="Password1234"
export STUDENT_DB_HOST="localhost"
export STUDENT_DB_PORT="3306"
export STUDENT_DB_NAME="student_db"
# Construct the MySQL URL for student_db
export STUDENT_DB_URL="mysql://${STUDENT_DB_USERNAME}:${STUDENT_DB_PASSWORD}@${STUDENT_DB_HOST}:${STUDENT_DB_PORT}/${STUDENT_DB_NAME}"
Step 2: Save and Reload .zshrc
Once you've added the new environment variables, save the file and reload the .zshrc
to apply the changes:
code ~/.zshrc
Step 3: Verify the New Environment Variables
Make sure that the new environment variables have been correctly set by running:
echo $DATABASE_URL
echo $STUDENT_DB_URL
Each command should print the expected values.
Step 4: Use the New Database in Your Jupyter Notebook or Python Script
Now that you've added the new database credentials, you can use them in your Python script or Jupyter notebook by accessing the new environment variables.
For example, in Jupyter or Python:
import os
# database (classicmodels)
db_username = os.getenv('DB_USERNAME')
db_password = os.getenv('DB_PASSWORD')
db_host = os.getenv('DB_HOST')
db_port = os.getenv('DB_PORT')
db_name = os.getenv('DB_NAME')
connection_string_classicmodels = f"mysql+mysqlconnector://{db_username}:{db_password}@{db_host}:{db_port}/{db_name}"
# New database (new_database)
new_db_username = os.getenv('NEW_DB_USERNAME')
new_db_password = os.getenv('NEW_DB_PASSWORD')
new_db_host = os.getenv('NEW_DB_HOST')
new_db_port = os.getenv('NEW_DB_PORT')
new_db_name = os.getenv('NEW_DB_NAME')
connection_string_new_database = f"mysql+mysqlconnector://{new_db_username}:{new_db_password}@{new_db_host}:{new_db_port}/{new_db_name}"
# Print connection strings to verify
print(connection_string_classicmodels) # Connection string for the first database
print(connection_string_new_database) # Connection string for the second database
Step 5: Switching Between Databases in Jupyter Notebook
You can switch between the two databases by simply changing the connection string depending on which database you want to query.
For example, to use the classicmodels database:
%sql {connection_string_classicmodels}
And to switch to the new_database:
%sql {connection_string_new_database}
Step 6: Optional - Add More Databases
If you need to add more databases in the future, follow the same steps:
- Add more environment variables in
.zshrc
for each new database. - Update the connection strings in your Python or Jupyter notebook.
By doing this, you can easily manage multiple databases in your environment without exposing sensitive information.