新装的mysql,想登陆时却无法登陆,使用当前root密码或者空密码都不正确。
mysql版本: 8.0.15 MySQL Community Server - GPL
[root@localhost ~]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@localhost ~]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
经网上查询,了解安装成功后,会有一个初始的临时密码 temporary password 存在/var/log/mysqld.log
[root@localhost log]# cat /var/log/mysqld.log |grep temporary
2019-03-30T16:10:25.141218Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 2yi;0u:+g;#S
使用这个密码可以正常登陆,并通过set password或者alter user修改密码:
[root@localhost log]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.15 MySQL Community Server - GPL
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> set password='Test@1234';
Query OK, 0 rows affected (0.00 sec)
//或者
mysql> ALTER user 'root'@'localhost' IDENTIFIED BY 'Test@1234';
其他tips:
1、创建新数据库用户
create user 'test'@'localhost' identified by '123456';
2、创建允许外网 IP 访问的数据库用户
create user 'test'@'%' identified by '123456';
3、刷新授权
flush privileges;