I am trying to connect MySQL(8.0.25) and python(3.9.6) using mysql.connector but getting error.
Error:
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Code:
import mysql.connector as sql
s=sql.connect(host='localhost' ,user='root', password='<correct password>',database='empl')
The password I entered is the one I use to successfully login to MySQL 8.0 Command line client.
Same code is working for pymysql.connect without any error.
I checked out the Host and User Name by logging in to MYSQL 8.0 Command Line client with my password and typing:
mysql>Select current_user;
And got the Output:
Output:
+----------------+
| current_user |
+----------------+
| root@localhost |
+----------------+
Thanks in advance.
Kindly help me out as I tried everything but couldn't solve it.
Looking forward for a reply.
Suspect from the documents that you must specify a database. See the MySQL connector for Python documents
HERE
(Jul-12-2021, 05:30 PM)jefsummers Wrote: [ -> ]Suspect from the documents that you must specify a database. See the MySQL connector for Python documents HERE
I even tried that out, but, unfortunately, that doesn't make any difference.
Eagerly waiting for someone to help me out.
You could try
pymysql. MYSQL connector seems to not play well with python 3
I second menator01's answer.
I use pymsql to collect data from my webpage database. Works amazingly fast, just 1 or 2 seconds to collect data from 168 students and write it to a results Excel file!! I am always flabbergasted! Used to take me a long time copying and pasting and sorting stuff!
import pymysql
import openpyxl
import os
def mysqlRemote(clas, weeknr):
# To connect to remote MySQL database
conn = pymysql.connect(
host='123.456.789.123',
user='allstudents',
password = 'blurb',
db='mydb',
)
cur = conn.cursor()
# Select query
cur.execute(f"SELECT studentnr, score FROM allstudentsAnswers{clas} WHERE weeknr = '{weeknr}'")
output = cur.fetchall()
#for i in output:
#print(i)
# To close the connection
conn.close()
return output
(Jul-18-2021, 08:11 PM)menator01 Wrote: [ -> ]You could try pymysql. MYSQL connector seems to not play well with python 3
pymysql is working very well for me too. But, as per my school course, I am bound to use mysql.connector.
(Jul-18-2021, 10:24 PM)Pedroski55 Wrote: [ -> ]I second menator01's answer.
I use pymsql to collect data from my webpage database. Works amazingly fast, just 1 or 2 seconds to collect data from 168 students and write it to a results Excel file!! I am always flabbergasted! Used to take me a long time copying and pasting and sorting stuff!
import pymysql
import openpyxl
import os
def mysqlRemote(clas, weeknr):
# To connect to remote MySQL database
conn = pymysql.connect(
host='123.456.789.123',
user='allstudents',
password = 'blurb',
db='mydb',
)
cur = conn.cursor()
# Select query
cur.execute(f"SELECT studentnr, score FROM allstudentsAnswers{clas} WHERE weeknr = '{weeknr}'")
output = cur.fetchall()
#for i in output:
#print(i)
# To close the connection
conn.close()
return output
pymysql is working very well for me too. But, as per my school course, I am bound to use mysql.connector.
(Jul-19-2021, 03:40 AM)DB1 Wrote: [ -> ] (Jul-18-2021, 10:24 PM)Pedroski55 Wrote: [ -> ]I second menator01's answer.
I use pymsql to collect data from my webpage database. Works amazingly fast, just 1 or 2 seconds to collect data from 168 students and write it to a results Excel file!! I am always flabbergasted! Used to take me a long time copying and pasting and sorting stuff!
import pymysql
import openpyxl
import os
def mysqlRemote(clas, weeknr):
# To connect to remote MySQL database
conn = pymysql.connect(
host='123.456.789.123',
user='allstudents',
password = 'blurb',
db='mydb',
)
cur = conn.cursor()
# Select query
cur.execute(f"SELECT studentnr, score FROM allstudentsAnswers{clas} WHERE weeknr = '{weeknr}'")
output = cur.fetchall()
#for i in output:
#print(i)
# To close the connection
conn.close()
return output
pymysql is working very well for me too. But, as per my school course, I am bound to use mysql.connector.
Looking forward for a reply to help me out.
Someone kindly help me out!