Navicat 连接MySQL出现1045-Access denied for user 'root'@'localhost'已解决

出现这种情况可能是密码错误或者未授权

步骤概括

1、找到mysql配置文件,/etc/my.cnf ,在配置文件加入skip-grant-tables,然后重启mysql服务即可生效

2、免密登录mysql,修改密码,重新重启mysql

        5.7 之前版本:update user set password = password('root') where user='root';

        5.7及之后版本:update user set authentication_string = password('root') where user='root';

3、授权

        grant all privileges on *.* to 'root'@'%' identified by 'root';

        flush privileges;

详细步骤

1、找到mysql配置文件,/etc/my.cnf ,在配置文件加入skip-grant-tables

2、重启mysql服务即可生效

systemctl restart mysqld.service  //重启mysql服务

systemctl status mysqld.service   //查看mysql状态

systemctl stop mysqld.service     //停止mysql服务

3、进入mysql,修改mysql密码

mysql -uroot -p

不用输入密码,直接回车,即可进入mysql命令界面

首先进入表mysql,如图

修改密码:这里要根据版本来执行不同的SQL语句了,因为版本不同,存储密码的字段可能不相同。5.7以前的版本可以用以下语句更新root密码:

update user set password = password('root') where user='root';

如果是高版本,则会提示错误信息

ERROR 1054 (42S22): Unknown column 'password' in 'field list'

而我本地mysql版本是5.7.21,里边没有password,是因为已经改成authentication_string字段了,于是重新执行修改字段后的更新语句

update user set authentication_string = password('root') where user='root';

修改成功!重启mysql,记得将skip-grant-tables注释。

4、授权远程连接

指定某个ip可以登录,那么我就可以将授权的语句改成:
grant all privileges on *.* to 'root'@'192.168.xx.xx' identified by 'root';
如果你是本地登录的,那么:
grant all privileges on *.* to 'root'@'localhost' identified by 'root';
当然也可以设置所有ip都可登录
grant all privileges on *.* to 'root'@'%' identified by 'root';
如果授权成功,会有Query OK的提示。
然后执行fluse privileges ,使授权立即生效
flush privileges;

也许会提示错误:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

这样只需执行如下命令即可:

mysql -u root -p  #登录
mysql>set global validate_password_policy=0; #密码强度设为最低等级
mysql>set global validate_password_length=4; #密码允许最小长度为4
mysql>flush privileges;                      #更新授权表,生效


===============关于 mysql 密码策略相关参数========================
1、validate_password_length  固定密码的总长度;
2、validate_password_dictionary_file 指定密码验证的文件路径;
3、validate_password_mixed_case_count  整个密码中至少要包含大/小写字母的总个数;
4、validate_password_number_count  整个密码中至少要包含阿拉伯数字的个数;
5、validate_password_policy 指定密码的强度验证等级,默认为 MEDIUM;
validate_password_policy 的取值:
    0/LOW:只验证长度;
    1/MEDIUM:验证长度、数字、大小写、特殊字符;
    2/STRONG:验证长度、数字、大小写、特殊字符、字典文件;
6、validate_password_special_char_count 整个密码中至少要包含特殊字符的个数

可通过sql语句查看密码策略,查相关资料可知,validate_password_length最小值为4,即使你设置为小于4的值,mysql也会默认设置为4。

show variables like 'validate_password%';

欢迎加群交流:700637673

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章