mysql密码忘了的问题及解决方案

新安装的数据库设置初始密码

(1)离线部署mysql可使用:

mysqladmin -u root password '123456'

(2)如是yum在线安装mysql可查看日志中的初始密码

grep -i password /var/log/mysqld.log

如果设置的密码忘记了可使用以下方法

mysql忘记密码

进入mysql的主配

vim /etc/my.cnf[mysqld]skip-grant-tables  #添加此行,跳过密码验证,忽略权限表身份验证

重启mysql

systemctl restart mysqld

直接输入mysql免密登录

mysql
show databases;describe mysql.user;

用户身份验证相关字段

select host,user,authentication_string from mysql.user;

更改密码为‘123’

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

刷新授权

flush privileges;quit

注释调跳过密码认证

 vim /etc/my.cnf

mysql -uroot -p123

更改完成后在数据库里输入命令,如提示以下错误:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

则执行

mysql> alter user root@localhost identified by '123';

测试:

mysql> show databases;

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持站长网。