Làm thế nào để khôi phục lại password mysql root trên Linux khi bạn lỡ quên mất mật khẩu? Chuyện này sẽ hết sức đơn giản nếu bạn làm theo những bước hướng dẫn trong bài viết này.
Bước 1 : Stop MySQL Server Service
Bạn chạy lệnh sau để dừng dịch vụ MySQL Server trên linux
service mysqld stop
Bước 2 : Khởi động MySQL Server mà không cần password
mysqld_safe --skip-grant-tables &
Chạy lệnh trên xong bạn nhấn tiếp enter để tiếp tục nhập lệnh khác
[root@xtraffic ~]# 160820 19:02:01 mysqld_safe Logging to '/var/log/mysqld.log'. 160820 19:02:01 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql [root@xtraffic ~]#
Bước 3 : Kết nối MySQL Server bằng MySQL client
mysql -u root
[root@xtraffic ~]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.51-log MySQL Community Server (GPL) by Remi Copyright (c) 2000, 2016, 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>
Bước 4 : Thiết lập password MySQL Server mới cho user root
USE mysql; UPDATE user set password=PASSWORD("NEW-ROOT-PASSWORD") WHERE User="root"; flush privileges; quit
mysql> USE mysql; No connection. Trying to reconnect... Connection id: 3 Current database: *** NONE *** 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-ROOT-PASSWORD") WHERE User="root"; Query OK, 3 rows affected (0.01 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye [root@xtraffic ~]#
Bước 5 : Khởi động lại MySQL Server
service mysqld restart
Bước 6 : Kiểm tra lại password mới của user root
mysql -u root -p