linux(ubuntu) 1045, "Access denied for user 'root'@'localhost' (using password: YES)"

问题现象:

最近使用 flask 的 sqlalchemy 框架,在链接数据库(mysql)时出现报错

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user 'root'@'localhost' (using password: YES)") (Background on this error at: http://sqlalche.me/e/e3q8)

直接在终端输入 mysql 也会出现报错

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

但是使用 mysql -uroot -p 可以正常进入数据库

解决方法:

我的系统为 ubuntu

修改文件

/etc/mysql/mysql.conf.d/mysqld.cnf

找到 [mysqld]

添加配置:

skip-grant-tables

保存后重启服务

/etc/init.d/mysql restart

此时报错都被解决掉了 直接输入 mysql 即可进入数据库

如果是自己测试或者练手小项目就可以继续了,但是此方式存在安全隐患,会跳过权限验证,如果对安全有要求的需要进入数据库修改密码后将此配置去掉后重启

use mysql;
update user set password=password("你的新密码") where user="root";
原文地址:https://www.cnblogs.com/chestnut-egg/p/10472324.html