flask连接数据库问题
来源:7-1 Flask 结合SQLAlchemy 打通数据库通讯
丫比比鸭
2022-01-03
老师,在连接数据库时会出现2059问题,上网查说是因为mysql版本太高【我的是8.0】导致的,有什么可以解决的方法吗【已解决,附在问题最下方】
MySQLdb._exceptions.OperationalError: (2059, )
from indexController import index_page
from flask_sqlalchemy import SQLAlchemy
app = Flask( __name__ )
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:123456@127.0.0.1/mysql"
db = SQLAlchemy( app )
app.register_blueprint(index_page,url_prefix="/imooc")
if __name__ == "__main__":
app.run( host="0.0.0.0",debug=True)```
> indexController
```@index_page.route("/template")
def template():
context = {}
from sqlalchemy import text
from application import db
sql = text("select * from `user`")
result = db.engine.execute( sql )
context['result'] = result
return render_template("index.html",**context)
感觉不太像是代码的问题,已经卡在这边一天了
-----------------------------------------------------------------------------------
已解决,出现2509报错原因是因为mysql对自己的密码进行的加密处理,进行一番百度就得到了答案。
cmd当中管理员运行
1.启动mysql
2.mysql -u root -p
3.输入密码
此时进入mysql的启动界面,输入指令
4.select host,user,plugin,authentication_string from mysql.user;
【注意这个地方的分号不要丢】
【可以查看到mysql的每一个用户的密码管理方式,在plugin下面】
【发现root对应的密码管理方式是caching_sha2_password】
【所以我们需要对此进行处理,将这种加密的密码管理方式转换成旧的mysql_native_password方式】
5.ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的mysql密码';
【注意此处的分号;仍然不能丢,网上好多教学都没有分号导致我直接卡了一天】
【此时会出现ok这样的选项,再用下述代码刷新一下】
6.FLUSH PRIVILEGES;
【上面的代码和上上面的代码大小写不区分】
【此时再此输入4指令,查看一下我们的密码方式是不是改变了】
7.select host,user,plugin,authentication_string from mysql.user;
【看一下root对应的plugin已经改成了native形式,修改成功~~】
------------------------------------------------------------------------------------
共勉,希望大家不要再出错了
写回答
1回答
-
编程浪子
2022-01-05
00
相似问题