SQLAlchemy 报错 NotSupportedError: Authentication plugin ‘caching_sha2_password’ is not supported 解决方法
生活随笔
收集整理的這篇文章主要介紹了
SQLAlchemy 报错 NotSupportedError: Authentication plugin ‘caching_sha2_password’ is not supported 解决方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
報錯內容
在使用 python SQLAlchemy 連接數據庫時,遇到以下錯誤:
NotSupportedError: Authentication plugin ‘caching_sha2_password’ is not supported原因
使用 mysql8.0 版本,登錄失敗,提示 Authentication plugin ‘caching_sha2_password’ is not supported。
原因是在 MySQL 8.0 以后,默認的密碼加密方式是 caching_sha2_password 而不是mysql_native_password。
解決方法
登錄mysql數據庫 mysql -u root -p
更新身份認證方式 ALTER USER root@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
py 代碼中添加auth_plugin=mysql_native_password
完整代碼示例:
#!/usr/bin/env python3 # -*- coding: utf-8 -*-from sqlalchemy import Column, String, create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base# 創建對象的基類: Base = declarative_base()# 定義User對象: class User(Base):# 表的名字:__tablename__ = 'user'# 表的結構:id = Column(String(20), primary_key=True)name = Column(String(20))# 初始化數據庫連接: engine = create_engine('mysql+mysqlconnector://root:123456@localhost:3306/crawer?auth_plugin=mysql_native_password')# 創建DBSession類型: DBSession = sessionmaker(bind=engine)# 創建session對象: session = DBSession() # 創建新User對象: new_user = User(id='5', name='Bob') # 添加到session: session.add(new_user) # 提交即保存到數據庫: session.commit() # 關閉session: session.close()# 創建Session: session = DBSession() # 創建Query查詢,filter是where條件,最后調用one()返回唯一行,如果調用all()則返回所有行: user = session.query(User).filter(User.id=='5').one() # 打印類型和對象的name屬性: print('type:', type(user)) print('name:', user.name) # 關閉Session: session.close()總結
以上是生活随笔為你收集整理的SQLAlchemy 报错 NotSupportedError: Authentication plugin ‘caching_sha2_password’ is not supported 解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git pull 卡在 Unpackin
- 下一篇: linux cmake编译源码,linu