“ sha256_password或caching_sha2_password需要加密”

Tan*_*der 11 python mysql cryptography mysql-python

美好的一天。希望你一切都好。有人可以帮我解决这个问题吗?

我是MySQL环境的新手。我正在尝试远程连接到MySQL数据库。我使用以下python代码,并收到此错误。

Print(e) = "cryptography is required for sha256_password or 
             caching_sha2_password"
Run Code Online (Sandbox Code Playgroud)

而且不知道如何解决错误。

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()
Run Code Online (Sandbox Code Playgroud)

小智 37

该错误消息可以变得更加全面和有用。为了修复此“密码”程序包,需要安装。

pip install cryptography
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。那对我有用。我认为没有必要重新发明轮子。 (2认同)

小智 17

要使用“sha256_password”或“caching_sha2_password”进行身份验证,您需要安装额外的依赖项:

$ python3 -m pip install PyMySQL[rsa]
Run Code Online (Sandbox Code Playgroud)

来源:https : //pymysql.readthedocs.io/en/latest/user/installation.html


Ben*_*Ben 9

值得一提的是,我今天在 Python 中通过 SQLAlchemy 使用 MySQL 时遇到了这个问题。事实证明,我为这个帐户使用了错误的密码。换句话说,如果您遇到此问题,您可能希望首先确认您使用的是正确的密码。

FWIW,我不确定为什么这会生成加密消息。沿途有什么马车吗?


小智 9

“这个报告的意思是 sha256_password 和 caching_sha2_password 这两种加密算法需要用到密码学,
虽然意思很清楚,但是你可能不知道如何解决。
其实密码学是一个 python 包,所以解决办法很简单:”

尝试pip install cryptography在 cmd 或终端上运行。
这是来源


kzt*_*ztd 7

我收到该消息是因为我的数据库尚未启动。


Al *_*ins 6

我今天在 Python 中通过 SQLAlchemy 使用 MySQL 时也遇到了这个问题。只需通过 mysql -u root -p 登录数据库即可消失。感谢@Ben 的提示。很可能发生了一些错误的事情。


Nis*_*att 2

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
Run Code Online (Sandbox Code Playgroud)

下载 mysql 连接器而不是 pymysql 并尝试以这种方式连接。它对我有用,希望对你也有用。