欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql Access denied for user ‘root’@’localhost’ (using password: YES)解决方法

程序员文章站 2024-02-28 15:05:40
今天在启动mysql时出现以下问题: [root@www ~]# mysql -u root -p enter password: error 1045 (...

今天在启动mysql时出现以下问题:

[root@www ~]# mysql -u root -p
enter password: 
error 1045 (28000): access denied for user 'root'@'localhost' (using password: yes)

网上的答案是各种各样的,最终解决问题的方法总结为以下,好多都是没有设置初始密码造成此问题的。

解决方法如下:

[root@www ~]# service mysqld stop   #先关闭mysql服务
stopping mysqld:                      [ ok ]
[root@www ~]# mysql
error 2002 (hy000): can't connect to local mysql server through socket '/var/lib/mysql/mysql.sock' (2)
[root@www ~]# mysql -u root -p      #关闭服务后又出现如下问题
enter password: 
error 2002 (hy000): can't connect to local mysql server through socket '/var/lib/mysql/mysql.sock' (2)
[root@www ~]# mysql -u root mysql    #键入此命令进入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.1.73 source distribution
copyright (c) 2000, 2013, 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> update user set password=password('123') where user='root' and host='localhost';  ---->修改root的密码
query ok, 1 row affected (0.04 sec)
rows matched: 1 changed: 1 warnings: 0
mysql> flush priviledge;
error 1064 (42000): you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'priviledge' at line 1
mysql> flush privileges;
query ok, 0 rows affected (0.00 sec)
mysql> \q
bye
[root@www ~]# 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.1.73 source distribution
copyright (c) 2000, 2013, 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> \q
bye

到此就完了!!