"cryptography is required for sha256_password or caching_sha2_password"
Question:
Good day. Hope your all are well. Can someone help me with fix this?
I’m new to the MySQL environment. I’m trying to connect to MySQL Database remotely. I used the following python code and got this error.
Print(e) = "cryptography is required for sha256_password or
caching_sha2_password"
And have no idea how to solve the error.
import pymysql as db
HOST = "XXXXX.XXX.XX”
PORT = XXXX
USER = "my_username"
PASSWORD = "my_password”
DB = "db_name"
try:
connection = db.Connection(host=HOST, port=PORT,user=USER,
passwd=PASSWORD, db=DB)
dbhandler = connection.cursor()
dbhandler.execute("SELECT * from table_name")
result = dbhandler.fetchall()
for item in result:
print (DB)
except Exception as e:
print(e)
finally:
connection.close()
Answers:
import mysql.connector
def connection():
conn = mysql.connector.connect(host = "XXXXX",
user = 'XXXXX',
password = 'XXXXX',
database = 'login_page',
auth_plugin='mysql_native_password')
c = conn.cursor()
return c , conn
Download mysql connector rather than pymysql and try connecting this way. It worked for me, hope it works for u too.
The error message can be made more comprehensive and helpful. In order to fix this “cryptography” package needs to be installed.
pip install cryptography
To use “sha256_password” or “caching_sha2_password” for authenticate, you need to install additional dependency:
$ python3 -m pip install PyMySQL[rsa]
Source: https://pymysql.readthedocs.io/en/latest/user/installation.html
For what it’s worth, I had this problem today using MySQL via SQLAlchemy in Python. It turned out that I was using the wrong password for this account. In other words, if you have this problem, you might want to start by just confirming that you’re using the correct password.
FWIW, I am not sure why this generated a cryptography message. Something buggy along the way?
Easy on MYSQL Workbench. Create a new MySQL user and fill the “Limit to Host Matching” with your IP. Is % by default.
I met this problem too. When I try to solve the problem use method of @brcmipn in MySQL Worbench, it tells me MySQL is running in Super Safe Mode.So I use
SET GLOBAL READ_ONLY = OFF;
and after that the problem don’t happen again.
I also had this problem today using MySQL via SQLAlchemy in Python. It went away just by logging into the database via mysql -u root -p. Credit to @Ben for the hint. Something buggy may well be happening.
I got the message because my database hadn’t been started.
"This report means that sha256_password with caching_sha2_password These two encryption algorithms need to use cryptography.
Although the meaning is clear, you may not know how to solve it.
Actually, cryptography is a python package, so the solution is simple:"
Try running pip install cryptography
on you cmd or terminal.
Here is the source.
Downgrading then upgrading PyMySQL worked for me. This happened to me twice in the last few days, on Ubuntu 20 & 22. I already had everything installed, and everything had been working the day before. Here’s what happened:
PyMySQL
1.0.2 – was working yesterday, failed today with crypto error
downgrade to
0.9 – works, no crypto error
upgrade back to
1.0.2 – works
I was looking into this today and the answer from @gajam is correct.
You can double check the database you are using and verify that the authentication type you are using (ie. MySQL server) for <user/root> is not set to "sha256_password” or “caching_sha2_password”. You can check via MySQL WorkBench > Users and Privileges.
If this is set and you are using pyMySQL to connect then you will have to install the RSA option (as commented by @gajam)
Note that I am also using SQLAlchemy when I stumbled upon this issue.
If you just want to fix this RuntimeError, pip install cryptography
is enough.
In my case, this error occurs when I use PyMySQL to connect MySQL 8.0 with wrong password. caching_sha2_password
is MySQL 8.0 default authentication plugin. If the password is correct, error will not occur.
I find that after first round authentication failed, PyMySQL tries to do full authentication with RSA. sha2_rsa_encrypt
function relays on cryptography package.
https://github.com/PyMySQL/PyMySQL/blob/v1.0.3/pymysql/_auth.py#:~:text=caching_sha2_password_auth
Even after installing cryptography, it raises (1045, "Access denied for user 'username'@'host' (using password: YES)")
error eventually if your password is still wrong.
Good day. Hope your all are well. Can someone help me with fix this?
I’m new to the MySQL environment. I’m trying to connect to MySQL Database remotely. I used the following python code and got this error.
Print(e) = "cryptography is required for sha256_password or
caching_sha2_password"
And have no idea how to solve the error.
import pymysql as db
HOST = "XXXXX.XXX.XX”
PORT = XXXX
USER = "my_username"
PASSWORD = "my_password”
DB = "db_name"
try:
connection = db.Connection(host=HOST, port=PORT,user=USER,
passwd=PASSWORD, db=DB)
dbhandler = connection.cursor()
dbhandler.execute("SELECT * from table_name")
result = dbhandler.fetchall()
for item in result:
print (DB)
except Exception as e:
print(e)
finally:
connection.close()
import mysql.connector
def connection():
conn = mysql.connector.connect(host = "XXXXX",
user = 'XXXXX',
password = 'XXXXX',
database = 'login_page',
auth_plugin='mysql_native_password')
c = conn.cursor()
return c , conn
Download mysql connector rather than pymysql and try connecting this way. It worked for me, hope it works for u too.
The error message can be made more comprehensive and helpful. In order to fix this “cryptography” package needs to be installed.
pip install cryptography
To use “sha256_password” or “caching_sha2_password” for authenticate, you need to install additional dependency:
$ python3 -m pip install PyMySQL[rsa]
Source: https://pymysql.readthedocs.io/en/latest/user/installation.html
For what it’s worth, I had this problem today using MySQL via SQLAlchemy in Python. It turned out that I was using the wrong password for this account. In other words, if you have this problem, you might want to start by just confirming that you’re using the correct password.
FWIW, I am not sure why this generated a cryptography message. Something buggy along the way?
Easy on MYSQL Workbench. Create a new MySQL user and fill the “Limit to Host Matching” with your IP. Is % by default.
I met this problem too. When I try to solve the problem use method of @brcmipn in MySQL Worbench, it tells me MySQL is running in Super Safe Mode.So I use
SET GLOBAL READ_ONLY = OFF;
and after that the problem don’t happen again.
I also had this problem today using MySQL via SQLAlchemy in Python. It went away just by logging into the database via mysql -u root -p. Credit to @Ben for the hint. Something buggy may well be happening.
I got the message because my database hadn’t been started.
"This report means that sha256_password with caching_sha2_password These two encryption algorithms need to use cryptography.
Although the meaning is clear, you may not know how to solve it.
Actually, cryptography is a python package, so the solution is simple:"
Try running pip install cryptography
on you cmd or terminal.
Here is the source.
Downgrading then upgrading PyMySQL worked for me. This happened to me twice in the last few days, on Ubuntu 20 & 22. I already had everything installed, and everything had been working the day before. Here’s what happened:
PyMySQL
1.0.2 – was working yesterday, failed today with crypto error
downgrade to
0.9 – works, no crypto error
upgrade back to
1.0.2 – works
I was looking into this today and the answer from @gajam is correct.
You can double check the database you are using and verify that the authentication type you are using (ie. MySQL server) for <user/root> is not set to "sha256_password” or “caching_sha2_password”. You can check via MySQL WorkBench > Users and Privileges.
If this is set and you are using pyMySQL to connect then you will have to install the RSA option (as commented by @gajam)
Note that I am also using SQLAlchemy when I stumbled upon this issue.
If you just want to fix this RuntimeError, pip install cryptography
is enough.
In my case, this error occurs when I use PyMySQL to connect MySQL 8.0 with wrong password. caching_sha2_password
is MySQL 8.0 default authentication plugin. If the password is correct, error will not occur.
I find that after first round authentication failed, PyMySQL tries to do full authentication with RSA. sha2_rsa_encrypt
function relays on cryptography package.
https://github.com/PyMySQL/PyMySQL/blob/v1.0.3/pymysql/_auth.py#:~:text=caching_sha2_password_auth
Even after installing cryptography, it raises (1045, "Access denied for user 'username'@'host' (using password: YES)")
error eventually if your password is still wrong.