1. 程式人生 > 實用技巧 >mysql忘記root密碼

mysql忘記root密碼

1.centos伺服器忘記root密碼,登入伺服器,開啟/etc/my.cnf,新增skip_grant_tables

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#在my.ini,[mysqld]下新增一行,使其登入時跳過許可權檢查
skip_grant_tables
user=mysql

儲存後退出。

2.重新啟動mysql,如果提示“mysqld: unrecognized service”,則需要安裝mysql-server服務:yum -y install mysql-server,沒有提示直接重啟。

[root@cuishougt bin]#
service mysqld restart Stopping mysqld: [ OK ] Starting mysqld: [ OK ]

3.這時再進入mysql是跳過密碼驗證的,提示輸入密碼時直接回車,然後更新root密碼,重新整理許可權後退出。

[root@cuishougt bin]# 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> 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 mysql> updates Display all 1105 possibilities? (y or n) mysql> UPDATE user SET Password = password ( 'root' ) WHERE User = 'root' ; Query OK, 3 rows affected (0.00 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> quit;

4.再次修改/etc/my.conf檔案,去掉或者註釋skip_grant_tables程式碼,然後重啟mysql服務,再次登入就可以用新密碼登入了。

[root@cuishougt bin]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@cuishougt bin]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@cuishougt bin]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 Source distribution

5.windows下mysql8.0忘記root密碼重置方法如下

1)停止mysql服務

net stop mysql

2)輸入 mysqld --skip-grant-tables --shared-memory (第一個引數是跳過許可權檢查,第二個似乎是 MySQL 8 需要的)

mysqld --skip-grant-tables --shared-memory

3)重新開啟一個命名視窗登入mysql,這時不需要輸入密碼,直接回車進入mysql,選擇mysql庫,然後重置密碼

UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '123456';
FLUSH PRIVILEGES;
exit;

4)重啟mysql服務,這時就可以用新的密碼登入root賬戶了。