简体   繁体   中英

Mysql database not connecting to python

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

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