1

So I am having a super hard time connecting to a local database using the python mysql.connector module. MySQL Workbench screenshot

So I am trying to connect using the highlighted connection. I use the password abcdefghijkl to log into the SQL environment. I am trying to connect to a database named flight_school.

My python script looks like so.

import mysql.connector

mydb = mysql.connector.connect("localhost", "root", "abcdefghijkl", "flight_school")

print(mydb.is_connected())

This above code in the arguments in the following order i.e.

  • hostname = localhost,
  • user = 'root',
  • password = 'abcdefghijkl', and
  • database name = 'flight_school'.

It's just not working. I get the following error. Image of the error

I would really appreciate some advice, please.

3 Answers 3

3

Please read always the official documentation

Your connection string has to have this form(if you do it this way)

 mydb = mysql.connector.connect(
     host="localhost",
     user="root",
     passwd="testpaaword",
     database="testdb"
 )
Sign up to request clarification or add additional context in comments.

3 Comments

I tried implementing the way you stated like so. mydb = mysql.connector.connect(host="localhost", user="root", passwd="abcdefghijkls", database="flight_school") But I keep on getting an error like so mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported
Any clue what that might mean
yes you have to enable in the mysql_native_password on your mysql server or for the user that logs in
2

Please read always the official documentation: https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html

import mysql.connector
from mysql.connector import errorcode, MySQLConnection

try:
    db_connection = MySQLConnection(user='root', password='', port='3306', database='your_database')
    print("Database connection made!")
except mysql.connector.Error as error:
    if error.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database doesn't exist")
    elif error.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("User name or password is wrong")
    else:
        print(error)
else:
    db_connection.close()

cursor = db_connection.cursor()

sql = ("commands")
cursor.execute(sql)

Comments

-1

Check out SQL-Alchemy module, works wonders from my experience.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.