1

I am having a weird issue with my python script. My script has to connect to MySQL DB. This is the code:

try:
   conn = MySQLdb.connect( user='root', host = 'localhost')
   cursor = conn.cursor()
   databases = cursor.fetchall()
   cursor.close()
except Exception as e:
   print e

when I run this script I have and error like:

(1045, "Access denied for user 'root'@'localhost' (using password: NO")

in the other hand, I can connect to MySQL just by entering MySQL (without password).

Why am I having this error with my python script when there is no password to root user?

3 Answers 3

4

Provide empty password try this

conn = MySQLdb.connect( user='root', host = 'localhost', passwd='')
Sign up to request clarification or add additional context in comments.

Comments

2

This should be the syntax. You should have the MySql connector for Python

import mysql.connector

cnx = mysql.connector.connect(user='root', password='',
                              host='127.0.0.1',
                              database='database_name')
cnx.close()

Comments

0

try this (inside the bloc try except)

import mysql.connector
conn = mysql.connector.connect(user='root', password='',host='localhost',                          
                              database='your_database_name')
conn.close()

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.