“sha256_password 或caching_sha2_password 需要加密"

2022-01-10 00:00:00 python mysql-python cryptography mysql

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

Good day. Hope your all are well. Can someone help me with fix this?

我是 MySQL 环境的新手.我正在尝试远程连接到 MySQL 数据库.我使用了以下 python 代码并得到了这个错误.

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

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

Download mysql connector rather than pymysql and try connecting this way. It worked for me, hope it works for u too.

相关文章