mysql 错误:使用pyinstaller运行Python模块的可执行文件时,不支持身份验证插件“caching_sha2_password”

unguejic  于 2022-11-28  发布在  Mysql
关注(0)|答案(1)|浏览(109)

我正在尝试运行一个可执行文件“main.exe”,它是使用pyinstaller模块从三个python模块(“main.py“是主脚本)构建的。用于从脚本构建可执行文件的命令是,

pyinstaller --onefile main.py

此脚本调用“tictactoe_office_release.py”脚本中的函数,该脚本建立到MySQL 8.0.31服务器数据库的连接以执行CRUD操作。当从命令行运行可执行文件时,我收到以下错误字符串:

Error: 'Authentication plugin 'caching_sha2_password' is not supported'
Traceback (most recent call last):
  File "main.py", line 24, in <module>
  File "tictactoe_office_release.py", line 42, in __init__
  File "mysql_python_lib.py", line 124, in __init__
  File "mysql_python_lib.py", line 96, in read_query
AttributeError: 'NoneType' object has no attribute 'cursor'
[25743] Failed to execute script 'main' due to unhandled exception!

但是,需要注意的是,我的main.py脚本在可执行文件之外运行时没有错误。
1.卸载“mysql连接器”并安装“mysql连接器-python”
2)在“mysql.connector.connect()”函数调用中将“auth_plugin”参数设置为“mysql_native_password”
3)通过运行以下命令修改mysql加密:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'Panther021698';

但在我重新构建并运行可执行文件后收到了相同的错误。
我的“tictactoe_office_release.py”模块中的相关代码描述了用于在Python解释器和MySQL服务器以及数据库之间实现通信的函数定义,如下所示:

from distutils.util import execute
import mysql.connector
from mysql.connector import Error
from mysql.connector.locales.eng import client_error

class mysql_python_connection:

    ''' Provide class definition for creating connection to MySQL server, 
    initializing database, and executing queries '''
    
    def __init__(self):
        
        self.host_name = "localhost"
        self.user_name = "root"
        self.passwd = "Panther021698"   

    def create_server_connection(self):

        ''' This function establishes a connection between Python 
        interpreter and the MySQL Community Server that we are attempting
        to connect to '''

        self.connection = None # Close any existing connections

        try:
            self.connection = mysql.connector.connect(
                host = self.host_name,
                user = self.user_name, 
                passwd = self.passwd
            )
            print("MySQL connection successful")
        except Error as err:
            print(f"Error: '{err}'")

    def create_database(self, query):

        ''' This function initializes a new database
        on the connected MySQL server'''

        cursor = self.connection.cursor()
        try:
            cursor.execute(query)
            print("Database created successfully")
        except Error as err:
            print(f"Error: '{err}'")

    def create_db_connection(self, db_name):

        ''' This function establishes a connection between Python 
        the MySQL Community Server and a database that we 
        are initializing on the server '''

        self.connection = None # Close any existing connections
        self.db_name = db_name

        try:
            self.connection = mysql.connector.connect(
                host = self.host_name,
                user = self.user_name, 
                passwd = self.passwd,
                database = self.db_name
                
            )
            print("MySQL Database connection successful")
        except Error as err:
            print(f"Error: '{err}'")

    def execute_query(self, query):

        ''' This function takes SQL queries stored
        in Python as strings and passes them
        to the "cursor.execute()" method to 
        execute them on the server'''

        cursor = self.connection.cursor()
        try:
            cursor.execute(query)
            self.connection.commit() # Implements commands detailed in SQL queries
            print(query + "Query successful")
        except Error as err:
            print(f"Error: '{err}'")

    def read_query(self,query):
        
        ''' This function reads and returns data from
        a MySQL database using the specified query '''
        
        cursor = self.connection.cursor()
        print("cursor datatype is ")
        print(type(cursor))
        #result = None
        try: 
            
            cursor.execute(query)
            result = cursor.fetchall()
            return result
        except Error as err:
            print(f"Error: '{err}'")

此外,我的MySQL环境变量在下图中提供。

kqlmhetl

kqlmhetl1#

我在使用MYSQL 8的时候也遇到了同样的错误。从我目前为止读到的内容来看,我还没有读到太多。在版本8中,默认的密码插件被改变了。我已经更新了很多p;我把我的MySQL降到了5.7版,脚本又工作了

相关问题