I am working on a project in Python tkinter for which I have chosen MySQL as my database. i have downloaded MySQL full version so i have tried to connect MySQL using Python mysql.connector
this code the code that i am running:
import mysql.connector
print("Trying...")
try:
connection = mysql.connector.connect(
host='127.0.0.1', # Use this instead of 'localhost'
user='root',
password='Admin_123',
database='testdb'
)
if connection.is_connected():
print("Successfully connected to MySQL.")
cursor = connection.cursor()
# Fetch the current database
cursor.execute("SELECT DATABASE();")
db_name = cursor.fetchone()
print(f"Connected to database: {db_name[0]}")
# Fetch data from the 'users' table
cursor.execute("SELECT * FROM users;")
records = cursor.fetchall()
if records:
print("Fetching records from 'users' table...")
for row in records:
print(row) # Print each row
else:
print("⚠️ No records found in 'users' table.")
else:
print("Failed to connect to MySQL.")
except mysql.connector.Error as err:
print(f"❌ Error: {err}")
finally:
if 'connection' in locals() and connection.is_connected():
cursor.close()
connection.close()
print("🔌 MySQL connection closed.")
The output I am getting is Trying..., that's all, no error nothing else
- I have tried checking if
MySQL80is running. - I run
netstat -an | find "3306"to check if MySQL is accepting connections on the expected port. - I even tried changing
host='localhost'tohost='127.0.0.1' - I have edited
my.inifile by addingbind-address=127.0.0.1under[mysqld]section (all of them are ChatGPT's recommendations) - Even checked the firewall too (chatGPT)
I do not know what the problem is here, the file runs get's this Trying... and runs for 5 sec and exits. The password is right, I have checked that too multiple times.
use_pure=Trueto your connection parameters. While I don't see any talk of that parameter helping in cases where a tunnel is NOT being used, it may be worth it to toss that into your connection parameters and see if it gets you through this issue.