ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'忘记mysql密码

时间:2021-07-06 19:19:56

        今天去连公司的mysql数据库,忽然发现用原来的密码登录不进去,不知道是什么鬼,只能强制修改了!

我的操作系统是Centos6.5 ,

一共有两种方法:

1、修改配置文件

 [root@localhost~]#  service  mysqld  stop                --停掉mysql服务

 [root@localhost~]#  vi  /etc/my.cnf                            --编辑文件,在[mysqld]下添加一行    skip-grant-tables

ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'忘记mysql密码

 [root@localhost~]# service  mysqld  start               --开启mysql服务

 [root@localhost~]# mysql                                       --直接用mysql进入数据库,不需要验证用户名密码

mysql>   use  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

Database changed


mysql>  update user set password =PASSWORD('NEW') where user='root';           --更新mysql的root密码为NEW

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       

[root@localhost~]#  service  mysqld   restart                    --重启mysql服务

Shutting down MySQL..                                      [  OK  ]
Starting MySQL.                                                  [  OK  ]

[root@rooo ~]#   mysql  -u root -pNEW

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.17 Source distribution

Copyright (c) 2000, 2014, 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;

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed


出现如上提示,则修改成功

2、mysqld_safe  -skip-grant-tables  &                --跳过密码验证

[root@localhost~]# mysql                                       --直接用mysql进入数据库,不需要验证用户名密码

mysql>   use  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

Database changed

mysql>  update user set password =PASSWORD('NEW') where user='root';           --更新mysql的root密码为NEW

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       

[root@localhost~]#  service  mysqld   restart                    --重启mysql服务

Shutting down MySQL..                                      [  OK  ]
Starting MySQL.                                            [  OK  ]


[root@rooo ~]#   mysql  -u root -pNEW

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.17 Source distribution

Copyright (c) 2000, 2014, 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;

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

出现如上提示,则修改成功