Этот код, который я использую:
Код: Выделить всё
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.")
[*] Я пытался проверить, работает ли mysql80 . Найдите "3306" , чтобы проверить, является ли mysql , принимает соединения в ожидаемом порту.
[*] Я даже пытался изменить host = 'localhost' на host = '127.0.0.1'
Я редактировал мой. Bind-address = 127.0.0.1 в разделе [mysqld] (все они являются рекомендациями CHATGPT) Пароль правильный, я проверял это слишком несколько раз.
Подробнее здесь: https://stackoverflow.com/questions/796 ... -connector