1. 程式人生 > >ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'忘記mysql密碼

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

        今天去連公司的mysql資料庫,忽然發現用原來的密碼登入不進去,不知道是什麼鬼,只能強制修改了!

我的作業系統是Centos6.5 ,

一共有兩種方法:

1、修改配置檔案

 [[email protected]~]#  service  mysqld  stop                --停掉mysql服務

 [[email protected]~]#  vi  /etc/my.cnf                            --編輯檔案,在[mysqld]下新增一行    skip-grant-tables

 [[email protected]

~]# service  mysqld  start               --開啟mysql服務

 [[email protected]~]# 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       

[[email protected]

~]#  service  mysqld   restart                    --重啟mysql服務

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

[[email protected] ~]#   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  &                --跳過密碼驗證

[[email protected]~]# 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       

[[email protected]~]#  service  mysqld   restart                    --重啟mysql服務

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


[[email protected] ~]#   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

出現如上提示,則修改成功