如果提示不存在 /var/log/mysqld.log 文件,请确保您已经通过步骤三,启动过一次 MySQL 服务。这里再提下 MySQL服务的启动命令是:systemctl start mysqld.service
如以下的输出,root 用户的登录密码为 Eea*eoqoI9:I。
1 2 3
[mysqlu@202 software]$ sudo grep "temporary password" /var/log/mysqld.log [sudo] password for mysqlu: 2019-12-27T07:48:29.009116Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Eea*eoqoI9:I
五、登录MySQL,并修改默认密码
知道 MySQL 的 root 用户密码后,即可登录 MySQL 了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ mysql -uroot -p Enter password: # 这里输入密码 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 17 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>
进入 MySQL 后,还不能做其他操作,会一直提示你修改默认密码,比如切换到 mysql 数据库:
1 2
mysql> use mysql; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
修改默认密码的命令如下:
1
ALTER user 'root'@'localhost' IDENTIFIED BY '新密码';
其中 MySQL 8 以上的密码策略限制必须要包含大小写字母、数字和特殊符号。如:
1 2 3
mysql> ALTER user 'root'@'localhost' IDENTIFIED BY '2020Super_Star'; Query OK, 0 rows affected (0.02 sec)