$ sudo grep'temporary password' /var/log/mysqld.log 2019-11-22T16:40:10.133730Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: >KG6Ybt3%lgo $ mysql -u root -p Enter password:
重置密码可以使用这条命令:
1
ALTER user 'root'@'localhost' IDENTIFIED BY '你的新密码';
注意这里的'localhost'也有可能是别的参数,具体可以通过下面这条命令来进行查询:
1
select user, host, authentication_string, plugin from mysql.user;
2、遗忘root密码
如果是忘记了root密码,就比较麻烦了。首先我们停止mysqld服务。
1 2
systemctl stop mysqld.service systemctl status mysqld.service
接着我们编辑/etc/my.conf让其跳过登录密码检查。
1
echo skip-grant-tables >> /etc/my.conf
接着我们重启mysqld服务并登录,此时不需要使用密码。
1 2 3
systemctl restart mysqld.service systemctl status mysqld.service mysql -u root
这里我们可以看到用户的账户信息都是存储在mysql这个数据库中的user表里面的。
1 2 3
show databases; use mysql; show tables;
1 2
select * from user\G; -- \G参数表示纵向输出格式
1 2 3 4 5 6 7
select host, user, authentication_string, plugin from user; -- host: 允许用户登录的ip‘位置’%表示可以远程; -- user:当前数据库的用户名; -- authentication_string: 用户密码;在mysql 5.7.9以后废弃了password字段和password()函数; -- plugin: 密码加密方式; update user set authentication_string='' where user='root'; select host, user, authentication_string, plugin from user;
$ systemctl restart mysqld.service $ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.18
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> show tables; ERROR 1046 (3D000): No database selected mysql> ALTER user 'root'@'localhost' IDENTIFIED BY 'centos7'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql> ALTER user 'root'@'localhost' IDENTIFIED BY 'MySQLNB8@2333'; Query OK, 0 rows affected (0.01 sec)