169

I am trying to connect to a MySQL server with python connector. I created a new user lcherukuri with the authentication plugin mysql_native_password.

But I got the error

mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported

Can someone help me?

import mysql.connector

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

enter image description here

kalehmann
  • 4,821
  • 6
  • 26
  • 36
lch
  • 4,569
  • 13
  • 42
  • 75
  • 1
    [MySQL 8.x?](https://dev.mysql.com/doc/refman/8.0/en/caching-sha2-pluggable-authentication.html) – Ben May 27 '18 at 22:56
  • @Ben yes. MySQL Ver 8.0.11 – lch May 27 '18 at 22:58
  • I also had this problem (on Windows 10), and eventually found a useful tutorial at: https://www.datacamp.com/community/tutorials/mysql-python As suggested there, I downloaded mysql.connector from https://dev.mysql.com/downloads/connector/python/ and had success after installing this. – johnnzy Sep 06 '19 at 03:22
  • 5
    `pip install mysql-connector-python` worked. – Egidius Oct 24 '21 at 22:49

29 Answers29

417

I had the same problem and passing auth_plugin='mysql_native_password' did not work, because I accidentally installed mysql-connector instead of mysql-connector-python (via pip3). Just leaving this here in case it helps someone.

ozgeneral
  • 6,079
  • 2
  • 30
  • 45
  • 8
    Thanks! uninstalled my-connector & installed mysql-connector-python and it worked! – Razikh Aug 19 '19 at 21:07
  • 4
    It solved it in my case as well. I followed w3schools and installed mysql-connector on pip and pip3 but w3schools is outdated at the time i am writing this. The proper way to do this is install mysql-connector-python on pip and pip3. – w3Develops Sep 05 '19 at 17:22
  • 1
    How did you import the library then? is it like "import mysql-connector-python" or "import mysql.connector". Sould we use dash our dot? – coskukoz May 05 '20 at 17:41
  • I had this a while ago so I dont exactly remember. but taking a look at https://github.com/mysql/mysql-connector-python it is probably "import mysql.connector". note that if you uninstall "mysql-connector", you don't have the risk of importing the wrong thing. – ozgeneral May 05 '20 at 17:46
  • Yup remember to remove the other one first. :D – AturSams Jan 30 '21 at 11:04
  • Hey, I had the same problem and your solution kinda worked accept now it gives this 1045 error (below) when i run the program. Any ideas why? 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) – su yan Mar 14 '21 at 12:21
  • @suyan it could actually be that username password combination is wrong? – ozgeneral Mar 14 '21 at 17:32
  • 1
    pip3 install mysql-connector-python worked for me. Thanks – Ripudaman Singh Jul 09 '21 at 18:00
  • Uninstalling `mysql.connector` and installing `mysql-connector-python` resolved the issue – Kalana Dananjaya Jun 23 '22 at 03:36
138

Per Caching SHA-2 Pluggable Authentication

In MySQL 8.0, caching_sha2_password is the default authentication plugin rather than mysql_native_password.

You're using mysql_native_password, which is no longer the default. Assuming you're using the correct connector for your version you need to specify the auth_plugin argument when instantiating your connection object

cnx = mysql.connector.connect(user='lcherukuri', password='password',
                              host='127.0.0.1', database='test',
                              auth_plugin='mysql_native_password')

From those same docs:

The connect() method supports an auth_plugin argument that can be used to force use of a particular plugin. For example, if the server is configured to use sha256_password by default and you want to connect to an account that authenticates using mysql_native_password, either connect using SSL or specify auth_plugin='mysql_native_password'.

Ben
  • 51,770
  • 36
  • 127
  • 149
82

This question is already answered here and this solution works.

caching sha2 password is not supported mysql

Just try this command :

pip install mysql-connector-python
Dirk Horsten
  • 3,753
  • 4
  • 20
  • 37
Anupriya Jaju
  • 839
  • 6
  • 6
  • 4
    Note that `mysql-connector` is deprecated. You should be using `mysql-connector-python` as shown in above answer. – Braden Holt Jun 11 '19 at 17:15
  • "pip install mysql-connector-python" solve my problem. Thanks a lot. – Peter Chen Mar 20 '20 at 21:17
  • I had the same problem and solution above kinda worked accept now it gives this 1045 error (below) when i run the program. Any ideas why? 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) – su yan Mar 14 '21 at 12:23
61

None of the above solution work for me. I tried and very frustrated until I watched the following video: https://www.youtube.com/watch?v=tGinfzlp0fE

pip uninstall mysql-connector work on some computer and it might not work for other computer.

I did the followings:

The mysql-connector causes problem.

  1. pip uninstall mysql-connector

    The following may not need but I removed both connector completely.

  2. pip uninstall mysql-connector-python

    re-install mysql-conenct-python connector.

  3. pip install mysql-connector-python

w3Develops
  • 358
  • 1
  • 3
  • 15
Peter Chen
  • 811
  • 6
  • 4
  • Perfect, I followed the answers by aupriya and ozgeneral and they both did work. But I also find benefit in following your instructions to uninstall mysql-connector completely and then install mysql-connector-python. – w3Develops Sep 05 '19 at 17:28
  • You solution helped :) since i had upgraded from mysql 5.7 to mysql 8.0 and i had to reinstall the `mysql-connector-python` to fix the problem – Ahtisham Dec 04 '20 at 10:48
  • 1
    The second step (not suggested in the other answers) helped me solve my problem. Thanks! – PJ127 Oct 14 '21 at 12:27
12

I also got a similar error

  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\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

You have probably installed mysql-connector instead of mysql-connector-python. So you need to install it again for python3:

pip3 install mysql-connector-python
double-beep
  • 5,031
  • 17
  • 33
  • 41
WinHtut
  • 121
  • 1
  • 3
10

I had this same issue but my resolution was different because this didn't completely work.

I found this on a GitHub forum - copy and paste this into your terminal. You don't have to change your password; it can be the exact same.

ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '{NewPassword}';

check your settings using this

select Host,User,plugin from mysql.user;
AndiAna
  • 854
  • 6
  • 26
Kori Vernon
  • 101
  • 2
  • 5
  • Above suggestions didnt work for me either. Neither adding the auth_plugin in the connect request, nor switching to the different connector library. what worked for me was switching to native password by setting it through alter user like you suggest. then check in your settings which ones you have set using select Host,User,plugin from mysql.user; after that it will start working. – AndiAna Oct 22 '21 at 13:01
  • Finally, thank you! After a bit of playing around with settings it seems doing this at the same time as changing from mysql-connector to mysql-connector-python did the trick. – Mikael Puusaari Jan 23 '22 at 09:54
6

pip3 install mysql-connector-python did solve my problem as well. Ignore using mysql-connector module.

Samsul Islam
  • 2,581
  • 2
  • 17
  • 23
TF Tan
  • 61
  • 1
  • 1
5

Modify Mysql encryption

ALTER USER 'lcherukuri'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Kaustuv
  • 728
  • 8
  • 13
5

Use pip install mysql-connector-python

Then connect like this:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",               #hostname
  user="Harish",                   # the user who has privilege to the db
  passwd="Harish96",               #password for user
  database="Factdb",               #database name
    auth_plugin = 'mysql_native_password',

)
5

I realized that I install mysql-connector instead of mysql-connector-python so run these commands in the terminal

pip3 uninstall mysql-connector
pip3 install mysql-connector-python
Varun Agarwal
  • 51
  • 1
  • 3
4

For those who couldn't work out because they installed mysql-connector first, I did the following:

1.First on CMD go to the path of 'pip'

2.Use 'pip list' command

3.There would be three packages installed namely six, protobuf and mysql-connector

4.Uninstall each of them separately

5.Now freshly install the mysql-connector-python module

This worked out for me

4

pip install -U mysql-connector-python this worked for me, if you already have installed mysql-connector-python and then follow https://stackoverflow.com/a/50557297/6202853 this answer

Chen Wang
  • 771
  • 6
  • 8
  • This worked for me when no others didn't. This will work if you are using a python venv but i did need to amend my datbase details by adding auth_plugin = 'mysql_native_password' – ThurstonLevi Aug 06 '21 at 14:29
3

You can go to Settings->Project->Project Interpreter and here install latest version of mysql-connector-python package. In my case it was mysql-connector-python 8.0.15.

PacuS
  • 31
  • 3
3

To have a more permanent solution without going through your code and modifying whatever needs to be modified: Per MySQL 8 documentation, easiest way to fix this is to add the following to your MySQL d file -> restart MySQL server.

This worked for me!

If your MySQL installation must serve pre-8.0 clients and you encounter compatibility issues after upgrading to MySQL 8.0 or higher, the simplest way to address those issues and restore pre-8.0 compatibility is to reconfigure the server to revert to the previous default authentication plugin (mysql_native_password). For example, use these lines in the server option file:

[mysqld]
#add the following file to your MySQLd file
    default_authentication_plugin=mysql_native_password
FlyingZebra1
  • 1,285
  • 1
  • 18
  • 28
3

Please install below command using command prompt.

pip install mysql-connector-python

enter image description here

Subhrajyoti Das
  • 2,685
  • 3
  • 21
  • 36
Vishal
  • 31
  • 2
3

I was facing the same error for 2 days, then finally I found a solution. I checked for all the installed connectors using pip list and uninstalled all the connectors. In my case they were:

  1. mysql-connector
  2. mysql-connector-python
  3. mysql-connector-python-rf

Uninstalled them using pip uninstall mysql-connector and finally downloaded and installed the mysql-connector-python from MySQL official website and it works well.

x1n13y84issmd42
  • 890
  • 9
  • 17
Pratika
  • 91
  • 1
  • 6
2
  1. Install mysql connector using the below command.

    pip install mysql-connector-python-rf

  2. Use the command to set the privileges.

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password'; FLUSH PRIVILEGES;

  3. Use the python command to connect to mysql database

    mydb = mysql.connector.connect( host="localhost", user="root", passwd="very_strong_password", auth_plugin='mysql_native_password')

1

If you are looking for the solution of following error

ERROR: Could not install packages due to an EnvironmentError: [WinError 5] Acces s is denied: 'D:\softwares\spider\Lib\site-packages\libmysql.dll' Consider using the --user option or check the permissions.

The solution: You should add --user if you find an access-denied error.

 pip install --user mysql-connector-python

paste this command into cmd and solve your problem

B--rian
  • 5,578
  • 10
  • 38
  • 89
Farrukh
  • 11
  • 1
1

This seems to be a problem with the mysql-connector package. Uninstall and install the mysql-connector-python package instead.

sudo pip uninstall mysql-connector
sudo pip install mysql-connector-python

Alternatively, you could use the mysql-connector package with auth_plugin variable shown the following python code

mysql.connector.connect(host='localhost',port="3306",user='user',password='pass',database='dbname',auth_plugin='myql_native_password')

I think in either case, you also need to setup your SQL database user with the mysql_native_password

alter user 'user'@'localhost' identified with mysql_native_password by 'password';
embulldogs99
  • 840
  • 9
  • 9
0

I ran into the same problem as well. My problem was, that I accidentally installed the wrong connector version. Delete your currently installed version from your file system (my path looks like this: C:\Program Files\Python36\Lib\site-packages) and then execute "pip install mysql-connector-python". This should solve your problem

0

i try to resolve this error and finally install PyMySQL instead of mysql library and it's working properly.

thanks.

0

I had an almost identical error:

Error while connecting to MySQL: Authentication plugin 'caching_sha2_password' is not supported

The solution for me was simple:

My db username/password creds were incorrect. The error was not descriptive of the problem, so I thought I would share this in case someone else runs into this.

0

I uninstalled mysql-connector (I installed following tutorial on w3schools tutorial) and installed mysql-connector-python instead. It worked.

Oleko Dundich
  • 41
  • 1
  • 1
  • 3
0

Using MySql 8 I got the same error when connecting my code to the DB, using the pip install mysql-connector-python did solve this error.

simomedx
  • 1
  • 1
0

This did the trick for me:

pip install cryptography
HeyMan
  • 1,529
  • 18
  • 32
0

It failed with MySQL server version 8.0, but worked with the version 5.7.33
The solution is to use MySQL version 5.7.33

Webdeveloper_Jelle
  • 2,868
  • 4
  • 29
  • 55
Sylvain
  • 679
  • 9
  • 13
0

if you are looking for connection url or connection string with correct plugin ; then following worked for me


url = 'mysql+mysqlconnector://%s:%s@%s:%s/%s?auth_plugin=mysql_native_password' % (settings['user'], settings['password'], settings['host'], settings['port'], settings['database'])

Arsii Rasheed
  • 324
  • 1
  • 5
  • 18
0

pip install mysql-connector-python , i hope it work

0

You can change the authentication string in mysql server.

Check the authentication_string of all user, use below query.

SELECT user,authentication_string,plugin,host FROM mysql.user;

Use alter query to change authentication string of user.

ALTER USER 'username'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
purval_patel
  • 385
  • 2
  • 11