简体   繁体   中英

Python script unable to connect to MySQL db with no password

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?

提供空密码,请尝试此

conn = MySQLdb.connect( user='root', host = 'localhost', passwd='')

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()

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM