-1
import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", password="123456")
if mysql.connector.is_connected():
    print("connected")

I recently installed mysql.connector module (im a newb at python) and whilst trying to connect to the mysql database( i have everything installed and the database seems to be working perfectly fine on its own). This is the output that I get any time i try to run any code related to mysql.connector module.

this is the error : -->

========================================= RESTART: C:\\Users\\niles\\AppData\\Local\\Programs\\Python\\Python312\\hi.py =========================================

===================================================================== RESTART: Shell ====================================================================

i have searched the web for anything similar but found nothing i also tried is_connected variant to see if the connection's established, but it yielded the same result. any help appreciated

i looked for this on w3schools and yt videos and i expected to see something along the lines of this :

<mysql.connector.connection.MySQLConnection object ar 0x016645F0>

same -

========================================= RESTART: C:\\Users\\niles\\AppData\\Local\\Programs\\Python\\Python312\\hi.py =========================================

===================================================================== RESTART: Shell ====================================================================

output appears

2

1 Answer 1

1

I have been fighting with that recently as well for a project i am working on, I have finally succeeded in connecting, must be something to do with windows 11 anyway, instead of the mysql.connector.connect() method use the class mysql.connector.MySQLConnection() so

old code

mydb = mysql.connector.connect(host="localhost", user="root", password="123456")

new code

mydb = mysql.connector.MySQLConnection(host="localhost", user="root", password='123456', database='databasename')

hope that helps happy coding !!!

Sign up to request clarification or add additional context in comments.

1 Comment

Here is the relevant documentation. mysql.connector.connect() worked for me for a long time (Windows 10), but when doing a new install on a new computer it stopped working. Changing to mysql.connector.MySQLConnection() worked for me on Windows Server 2022 Standard, with the same version of mysql-connector-python, 9.1.0. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.