ERROR 1045 (28000): Access denied for user 'root'@'localhost'

        我是在linux下安装了mysql,服务都启动完成之后在本机去连mysql数据库,进入到mysql安装目录下的bin目录,输入:./mysql -u root -p ,发现一直提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)。

        原来是mysql默认给数据库的root用户加了密码,该密码不是操作系统的密码,此时需要停掉mysql服务,然后进入安全模式去设置,操作如下:

[root@localhost ~]# /etc/rc.d/init.d/mysql

[root@localhost ~]# ./mysqld_safe --user=mysql --skip-grant-tables --skip-networking &

[root@localhost ~]# ./mysql -u root mysql

mysql> select Host, User, Password,password_expired from user where user='root' and host='root' or host='localhost';
ERROR 1054 (42S22): Unknown column 'Password' in 'field list'


mysql> select * from user where user='root' and host='root' or host='localhost';

 

发现字段不是叫做password,而是authentication_string

mysql> select Host, User, authentication_string,password_expired from user where user='root' and host='root' or host='localhost';
+-----------+-----------+-------------------------------------------+------------------+
| Host      | User      | authentication_string                     | password_expired |
+-----------+-----------+-------------------------------------------+------------------+
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | N                |
| localhost | root      | *9BB84292F4643547E34917D364B0C094C76E7306 | Y                |
+-----------+-----------+-------------------------------------------+------------------+
2 rows in set (0.00 sec)

mysql> UPDATE user SET authentication_string=PASSWORD('myegoo3466') where USER='root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye

[root@localhost bin]# /etc/rc.d/init.d/mysql restart

重启mysql服务之后重新连就ok了

[root@localhost bin]# ./mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.12

Copyright (c) 2000, 2016, 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里面操作任何你想操作的东西啦!

你可能感兴趣的:(ERROR 1045 (28000): Access denied for user 'root'@'localhost')