Question: I am trying to use python to query a database I set up in MySQL workbench. I am using the following code in a jupyter

I am trying to use python to query a database I set up in MySQL workbench. I am using the following code in a jupyter notebook via a web browser (https://jupyter.org/try-jupyter/lab/):

%pip install mysql-connector-python

import mysql.connector from mysql.connector import Error

connection = None # Initialize connection to None

try: # Establishing the connection connection = mysql.connector.connect( host='127.0.0.1', # Local MySQL server database='project', # Ensure this is the correct database name user='root', password='', # No password if not set (or put the password here) port=3306, # Default MySQL port, change if necessary auth_plugin='mysql_native_password', # Optional, depending on your MySQL version connection_timeout=10 # Set a timeout for the connection (in seconds) )

if connection.is_connected(): print("Connected to the database")

except Error as e: # Catch all MySQL-related errors print(f"Error while connecting to MySQL: {e}")

finally: # Ensure that the connection is closed if it was successfully established if connection and connection.is_connected(): connection.close() print("Connection closed")

I receive the following error:

Error while connecting to MySQL: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (timed out)

Please help me understand why the code cannot connect to the server. I have re-verified that the server is correct and running multiple times at this juncture.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Law Questions!