Linux 连接mysql报错Access denied for user 'root'@'localhost'

本文指导如何解决忘记MySQL密码的问题,通过停止服务、重启服务、重设密码、重启服务和再次连接数据库的步骤来解决Access denied错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

忘记mysql密码,使用不对的服务器密码,在连接服务器的时候,常常会提示 Access denied 错误,如下图所示:

[root@localhost bin]# ./mysql -uroot -p12345
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

遇到上面情况怎么办,请按如下步骤操作:

1、停止mysql服务

[root@localhost bin]# chkconfig --list | grep -i mysql
mysql           0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭
[root@localhost bin]# service mysql stop
Shutting down MySQL                                        [确定]
2、用mysqld_safe重启服务

[root@localhost bin]# ./mysqld_safe --user=root --skip-grant-tables --skip-networking &
[1] 3818
[root@localhost bin]# 111105 07:30:32 mysqld_safe Logging to '/usr/local/mysql/var/localhost.localdomain.err'.
111105 07:30:32 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var
./mysql -uroot mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.48-log Source distribution




Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license




Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

3、重设密码

mysql> update user set password=password('12345') where user='root' ;
Query OK, 3 rows affected (0.05 sec)
Rows matched: 3  Changed: 3  Warnings: 0




mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)




mysql> quit
Bye

4、重启服务

[root@localhost bin]#service mysql stop


[root@localhost bin]# service mysql start

5、再次连接数据库

[root@localhost bin]# ./mysql -uroot -p12345
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.48-log Source distribution




Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license




Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值