CentOS 7.4下解决登录MySQL时,ERROR 1045 (28000): Access denied for user root@localhost (using password: YES

时间:2021-11-15 20:04:57
[root@VM_0_11_centos ~]# mysql -u root -p
Enter password:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

启动一下mysqld服务试试

[root@VM_0_11_centos ~]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
mysql -u root -p[root@VM_0_11_centos ~]# mysql -u root -p
Enter password:

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

发现问题还是没有解决

[root@VM_0_11_centos ~]# service mysqld stop

Redirecting to /bin/systemctl stop mysqld.service

先停掉mysqld服务

[root@VM_0_11_centos ~]# mysql -u root mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@VM_0_11_centos ~]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &

[1] 2513

既然连不上socket,把networking跳掉,安全模式再登录

[root@VM_0_11_centos ~]# 180330 21:47:46 mysqld_safe Logging to '/var/log/mysqld.log'.

180330 21:47:46 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
mysql -u root mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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;
Database changed
mysql> UPDATE user SET Password=PASSWORD('???') where USER='???';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

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

mysql> quit

Bye

去修改一下密码,再刷新

[root@VM_0_11_centos ~]# mysql -u root -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

再次登录,可以了。


感谢:ktz666