Headscratcher here for me.
I am attempting to connect to a database on my local MySQL 8.0.11.0 install from Python.
Here's the code I'm using :
conn = pymysql.connect(host='localhost', port=3306, user='root', password='placeholder', db='CustomerInfo')
Python is returning the following :
Traceback (most recent call last):
File "D:\Python\FileCheck.py", line 38, in <module>
conn = pymysql.connect(host='localhost', port=3306, user='root', password='placeholder', db='CustomerInfo')
File "C:\Program Files\Python36\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 704, in __init__
self.connect()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 974, in connect
self._request_authentication()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 1203, in _request_authentication
auth_packet = self._read_packet()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 1059, in _read_packet
packet.check_error()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 384, in check_error
err.raise_mysql_exception(self._data)
File "C:\Program Files\Python36\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.OperationalError: (1045, "Access denied for user 'root'#'localhost' (using password: NO)")
I have confirmed that I have access to the database when logging in through MySQL Workbench. The weird thing is that Python is telling me "using password: NO" even though I'm sending a password.
I've tried changing passwd to "password" in the script and creating a new user with the appropriate privileges. Neither has worked.
Not sure what to check next.
EDIT: Here are grants for root:
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `root`#`localhost` WITH GRANT OPTION
GRANT BACKUP_ADMIN,BINLOG_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SET_USER_ID,SYSTEM_VARIABLES_ADMIN,XA_RECOVER_ADMIN ON *....
GRANT ALL PRIVILEGES ON `customerinfo`.* TO `root`#`localhost` WITH GRANT OPTION
GRANT PROXY ON ''#'' TO 'root'#'localhost' WITH GRANT OPTION
EDIT 2:
Added debug lines to connect and _request_authentication in connections.py.
Here's the latest:
C:\Users\Paul Miller>python.exe D:\Python\FileCheck.py
** DEBUG 1 **
connect, line 973
host= localhost
user= root
password= placeholder
** DEBUG 2 **
_request_authentication line 1172
user= root
password= placeholder
Traceback (most recent call last):
File "D:\Python\FileCheck.py", line 38, in <module>
conn = pymysql.connect(host='localhost', port=3306, user='root', password='placeholder', db='CustomerInfo')
File "C:\Program Files\Python36\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 704, in __init__
self.connect()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 982, in connect
self._request_authentication()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 1218, in _request_authentication
auth_packet = self._read_packet()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 1067, in _read_packet
packet.check_error()
File "C:\Program Files\Python36\lib\site-packages\pymysql\connections.py", line 384, in check_error
err.raise_mysql_exception(self._data)
File "C:\Program Files\Python36\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.OperationalError: (1045, "Access denied for user 'root'#'localhost' (using password: NO)")
EDIT 3: Enabled logging. No surprises in the log, but here's what the latest attempt generated:
2018-05-15T10:27:15.197445Z 43 Connect root#localhost on CustomerInfo using TCP/IP
2018-05-15T10:27:15.197540Z 43 Connect Access denied for user 'root'#'localhost' (using password: NO)
EDIT 4: Found the problem. I added some debug statements as follows:
if self._auth_plugin_name in ('', 'mysql_native_password'):
print("** DEBUG 3 **")
print(self.password)
print("\n")
authresp = _scramble(self.password.encode('latin1'), self.salt)
The problem is this IF block fails... program flow isn't getting to the "authresp" statement. When a peer of mine runs this same program, his passwords prints in the console.
So, now I just need to figure out why I'm not going down this branch.
Resolved the issue after downgrading MySQL from 8.0.11.0 to 5.7.22.
In addition to what I listed in my original question, I also tried the following:
Installed lower version of PyMySQL (0.8.1 to 0.8.0 in my case)
Completely removed both versions of PyMySQL and installed v0.8.0
Removed MySQL 8.0 and reinstalled
When none of the above worked, I downgraded MySQL to v5.7, which is what one of my peers is using. That resolved the error.
One possible cause of this issue is a PyMySQL version 0.8.x or lower. If this is the case, the issue could be resolved by upgrading it in pip to PyMySQL==0.9.3.
That is strange. I would install pymysql in a virtual environment and then drop some debugging lines in this function:
https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/connections.py#L940
That should show what variables its actually using for the connection and make it obvious what is wrong.
Related
I'm trying to establish a connection to MySQL server from IDLE and I can't figure out why I'm getting an error.
>>> dbconfig = {'host': '127.0.0.1',
'user': 'vsearch',
'password': 'vsearchpasswd',
'database': 'vsearchlogDB'}
>>> import mysql.connector
>>> conn = mysql.connector.connect(**dbconfig)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
conn = mysql.connector.connect(**dbconfig)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/connection.py", line 95, in __init__
self.connect(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/abstracts.py", line 719, in connect
self._open_connection()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/connection.py", line 208, in _open_connection
self._do_auth(self._user, self._password,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/connection.py", line 137, in _do_auth
packet = self._protocol.make_auth(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/protocol.py", line 99, in make_auth
packet += self._auth_response(client_flags, username, password,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/protocol.py", line 58, in _auth_response
auth = get_auth_plugin(auth_plugin)(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/authentication.py", line 190, in get_auth_plugin
raise errors.NotSupportedError(
mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported
I've tried these suggestions I found on stackoverflow :
shutting down mysql.server, installing mysql-connector-python and restarting mysql.server
passing an auth_plugin argument to the connect() method (as seen below)
auth_plugin='mysql_native_password'
made sure MySQL in listening to port 3306 (answer below)
localhost:mysql (LISTEN)
Versions:
mysql Ver 8.0.32 for macos12.6 on x86_64 (Homebrew)
Python 3.9.1
MacOS 12
Managed to fix issue by uninstalling MySQL connector and reinstalling it. Had issues with permissions so make sure you have permissions to read and write in connector directories.
This tutorial might help if your trying to connect to MySQL with MySQL-connector-python :
https://pynative.com/python-mysql-database-connection/
I make a telegram bot using python and MySQL. I can enter the database in terminal, but when I run my python code, it says I have no permission.
ilyacherne#vh289 ~ $ python3 /home/i/ilyacherne/public_html/cgi-bin/bot.py
Traceback (most recent call last):
File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 239, in _open_connection
self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Access denied for user 'ilyacherne'#'77.222.61.25' (using password: YES)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/i/ilyacherne/public_html/cgi-bin/bot.py", line 3, in <module>
import config
File "/home/i/ilyacherne/public_html/cgi-bin/config.py", line 8, in <module>
database = mysql.connect(
File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/__init__.py", line 272, in connect
return CMySQLConnection(*args, **kwargs)
File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 85, in __init__
self.connect(**kwargs)
File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/abstracts.py", line 1009, in connect
self._open_connection()
File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 241, in _open_connection
raise errors.get_mysql_exception(msg=exc.msg, errno=exc.errno,
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'ilyacherne'#'77.222.61.25' (using password: YES)
I checked the password, host name, database name and user.
I connect with this code:
database = mysql.connect(
host = '77.222.61.25',
user = 'ilyacherne',
passwd = 'CENSORED',
database = 'ilyacherne'
)
support said that MySql 5.7 connects to socket by default and i connect just with 'sudo mysql', and then they reccomended me try this:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'Current-Root-Password'; FLUSH PRIVILEGES;
but it says:
ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation
ERROR 1227 (42000): Access denied; you need (at least one of) the RELOAD privilege(s) for this operation
Pip install mysql-connector-python
Then import mysql-connector.
I have MySQL ( Database: items, Table: issuelist )
I want to read and print out by python... but when I run code as below, it show error.
import mysql.connector
#from mysql.connector import Error
mydb = mysql.connector.connect(
host="localhost",
#user="yourusername",
#passwd="yourpassword",
database="items"
)
mycursor = mydb.cursor()
sql_statement = "SELECT * FROM issuelist"
mycursor.execute(sql_statement)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Traceback (most recent call last): File
"C:\Users\Administrator\Desktop\Chatbot - ReadData\ExcelMySQL.py",
line 8, in
database="items" File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector__init__.py",
line 219, in connect
return MySQLConnection(*args, **kwargs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection.py",
line 104, in init
self.connect(**kwargs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\abstracts.py",
line 960, in connect
self._open_connection() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection.py",
line 292, in _open_connection
self._ssl, self._conn_attrs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection.py",
line 212, in _do_auth
self._auth_switch_request(username, password) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection.py",
line 256, in _auth_switch_request
raise errors.get_exception(packet) mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied
for user ''#'localhost' (using password: NO)
How to do resolve ?
Thanks you so much!
The error message is quite explanatory :
Access denied for user ''#'localhost' (using password: NO)
You can't just connect to mysql with no account...
The error msg clearly shows the mistake -- The connect info is not correct. That is ,in your code, make sure the info of the host,user,passwd and database are all correct?
I am trying to connect python to mysql by this code--
(I am using the mysql.connect library)
import mysql.connector
cnx = mysql.connector.connect(user='root',
password='password',
host='127.0.0.1',
database='db')
print(cnx)
cnx.close()
But it continues to throw the error--
Traceback (most recent call last):
File "D:/ted/main.py", line 5, in <module>
database='db')
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\__init__.py",
line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\connection.py",
line 94, in __init__
self.connect(**kwargs)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\abstracts.py",
line 722, in connect
self._open_connection()
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\connection.py",
line 211, in _open_connection
self._ssl)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\connection.py",
line 141, in _do_auth
auth_plugin=self._auth_plugin)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\protocol.py",
line 102, in make_auth
auth_data, ssl_enabled)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\protocol.py",
line 58, in _auth_response
auth = get_auth_plugin(auth_plugin)(
File "C:\Program Files (x86)\Python36-
32\lib\mysql\connector\authentication.py", line 191, in get_auth_plugin
"Authentication plugin '{0}' is not supported".format(plugin_name))
mysql.connector.errors.NotSupportedError: Authentication plugin
'caching_sha2_password' is not supported
Process finished with exit code 1
I have started the mysql sever in workbench and the also checked the status.
Also database named "db" is also there
Also checked if the host is proper.
-->Removed ssl also.
It seems that your MySQL Server version is 8.x and in that case the default MySQL connector is caching_sha2_password. In the other hand your error is maybe because your python client connector does not support this Authentication Plugin and you should explicitly change the Authentication Plugin to the old one (mysql_native_password).
cnx = mysql.connector.connect(user='root', password='password',
host='127.0.0.1', database='db',
auth_plugin='mysql_native_password')
Pretty new to both Python and SQL, so please excuse my noobishness. We have a server at my company that I'm trying to connect to through some python code. I have the correct IP address and logon credentials, and I can connect to it in SQL Server Management Studio without any issues, but I can't connect to it through PyCharm's database feature or through my code. I'm running python 3.6 if that makes a difference.
This is the code that I'm running (I've omitted the server ip address and logon credentials for security):
import pymysql
db = pymysql.connect(host='server ip',
port=1433,
user='username',
passwd='password',
db='Development DB')
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print("Database version : %s" % data)
db.close()
When I run the code, after about 2 minutes of nothing happening I get this error:
Traceback (most recent call last):
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 1021, in _read_bytes
data = self._rfile.read(num_bytes)
File "C:\Program Files\Python35\lib\socket.py", line 575, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/aubrey_s/PycharmProjects/Drawings_Converter/Drawings_Converter.py", line 5, in <module>
db = pymysql.connect(host='ip address', port=1433, user='username', passwd='password', db='Development DB')
File "C:\Program Files\Python35\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 706, in __init__
self.connect()
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 931, in connect
self._get_server_information()
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 1245, in _get_server_information
packet = self._read_packet()
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 987, in _read_packet
packet_header = self._read_bytes(4)
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 1029, in _read_bytes
"Lost connection to MySQL server during query (%s)" % (e,))
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([WinError 10054] An existing connection was forcibly closed by the remote host)')
From what I can understand, it looks like a connection was established, but the server forced it to close after a while. Can anyone offer me some more insight into what the problem might be? Thanks in advance!
As I wrote in another post, you may want to try using pymssql, in case your database is not a MySQL DB but a Microsoft SQL DB.