一、问题描述
执行以下代码:
mysql -uroot
运行结果如下:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
错误的原因是秘密错误。初次启动Mysql时系统自动生成了临时秘密,找到临时密码就可以正常登陆
二、解决方法
(1)查看临时秘密
1、切换文件目录到 /var/log
cd /var/log
2、查看mysqld.log文件
more mysqld.log
返回结果如下:
2020-07-16T17:07:59.769774Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (my sqld 8.0.21) initializing of server in progress as process 9857 2020-07-16T17:07:59.776926Z 1 [System] [MY-013576] [InnoDB] InnoDB initializatio n has started. 2020-07-16T17:08:00.567970Z 1 [System] [MY-013577] [InnoDB] InnoDB initializatio n has ended. 2020-07-16T17:08:02.500261Z 6 [Note] [MY-010454] [Server] A temporary password i s generated for root@localhost:YbitKorHK3<T 2020-07-16T17:08:06.605946Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (my sqld 8.0.21) starting as process 9905 2020-07-16T17:08:06.617260Z 1 [System] [MY-013576] [InnoDB] InnoDB initializatio n has started. 2020-07-16T17:08:06.806288Z 1 [System] [MY-013577] [InnoDB] InnoDB initializatio n has ended. 2020-07-16T17:08:06.919836Z 0 [System] [MY-011323] [Server] X Plugin ready for c onnections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock 2020-07-16T17:08:07.074850Z 0 [Warning] [MY-010068] [Server] CA certificate ca.p em is self signed. 2020-07-16T17:08:07.075506Z 0 [System] [MY-013602] [Server] Channel mysql_main c onfigured to support TLS. Encrypted connections are now supported for this chann el. 2020-07-16T17:08:07.122128Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: re ady for connections. Version: '8.0.21' socket: '/var/lib/mysql/mysql.sock' por --More--(21%)
获得临时秘密为:YbitKorHK3<T
输入如下代码登陆mysql的root用户:
mysql -uroot -p
返回结果如下:
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.21 Copyright (c) 2000, 2020, 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.
在Enter password:后面输入临时秘密即可登陆。
(2)修改初始密码
登陆mysql root用户之后,使用执行以下命令以修改初始密码:
alter user 'root'@'localhost' identified by "mima@123";