Đổi mật khẩu root mysql
Vào một ngày đẹp trời nào đó khi login vào tài khoản root mysql bằng lệnh:
mysql -u root -p
Bạn nhận được lỗi sau:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Thì bình tĩnh set lại pass cho root mysql bằng các bước sau:
Bước 1: Dừng dịch vụ MySQL server.
Bước 2: Chạy (mysqld) server/daemon process với tham số –skip-grant-tables để không bị hỏi pass root
Bước 3: Kết nối tới MySQL server với root user.
Bước 4: Tạo mật khẩu mới cho root.
Bước 5: Thoát và khởi động lại MySQL server.
Sau đây là các bước cụ thể:
Bước 1:Dừng MySQL service:
systemctl stop mysqld
Bước 2 : Chạy ngầm process mysqld với tham số:
mysqld_safe --skip-grant-tables &
Kết quả:
[1] 11674 [root@localhost ~]# 191010 13:55:52 mysqld_safe Logging to '/var/log/mysqld.log'. 191010 13:55:52 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Bước 3 : Mở một cửa số kết nối mới và kết nối Mysql sever sử dụng mysql client
mysql -u root
Két quả :
Output: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 56299 Server version: 5.6.34-1 (Debian) 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: Tạo mật khẩu mới cho MySQL root user:
MySQL 5.7.5 và cũ hơn
mysql> use mysql; mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root'; mysql> flush privileges; mysql> quit
MySQL 5.7.6 và mới hớn
mysql> use mysql; mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD("newpass"); mysql> flush privileges; mysql> quit
Bước 5:Dừng MySQL server:
for i in `ps auxf|grep mysqld|grep "grep" -v|awk '{print $2}'`; do kill -9 $i; done
Kết quả:
[1]+ Killed mysqld_safe --skip-grant-tables
Khởi động lại mysql server và hưởng thụ thành quả nhé:
systemctl restart mysqld mysql -u root -p