一、没有忘记密码
1)直接更改密码:mysqladmin -uroot -p’123’ password 'new_password’
[root@localhost ~]# mysqladmin -uroot -p'Jiaofan@123' password 'JiaoFan@1234'
[root@localhost ~]# mysql -uroot -p'JiaoFan@1234'
2)进入 msyql 更改密码:alter user ‘root’@‘localhost’ identified by ‘new_password’;
mysql> alter user 'root'@'localhost' identified by 'Jiao@1234';
mysql> flush privileges;
3)#创建新用户并产生随机密码
mysql> CREATE USER
'u1'@'localhost' IDENTIFIED BY RANDOM PASSWORD,
'u2'@'%.example.com' IDENTIFIED BY RANDOM PASSWORD,
'u3'@'%.org' IDENTIFIED BY RANDOM PASSWORD;
+------+---------------+----------------------+
| user | host | generated password |
+------+---------------+----------------------+
| u1 | localhost | BA;42VpXqQ@i+y{&TDFF |
| u2 | %.example.com | YX5>XRAJRP@>sn9azmD4 |
| u3 | %.org | ;GfD44l,)C}PI/6)4TwZ |
+------+---------------+----------------------+
5)#修改随机密码
mysql> ALTER USER
'u1'@'localhost' IDENTIFIED BY RANDOM PASSWORD,
'u2'@'%.example.com' IDENTIFIED BY RANDOM PASSWORD;
+------+---------------+----------------------+
| user | host | generated password |
+------+---------------+----------------------+
| u1 | localhost | yhXBrBp.;Y6abB)e_UWr |
| u2 | %.example.com | >M-vmjp9DTY6}hkp,RcC |
+------+---------------+----------------------+
6)修改随机密码
mysql> SET PASSWORD FOR 'u3'@'%.org' TO RANDOM;
+------+-------+----------------------+
| user | host | generated password |
+------+-------+----------------------+
| u3 | %.org | o(._oNn)d;FC<vJIDg9M |
+------+-------+----------------------+
7)其他有关密码管理请主动参考文档:
https://dev.mysql.com/doc/refman/8.0/en/password-management.html
二、忘记密码
1)更改配置文件,添加 skip-grant-tables 参数
[root@localhost ~]# vi /etc/my.cnf
[mysqld]
#在最后添加这个参数
skip-grant-tables
2)重启mysql
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# mysql -uroot -p
Enter password: <==直接返回