Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Đổi mật khẩu user MySQL bằng câu lệnh UPDATE

Cập nhật bảng user trong database mysql để đổi mật khẩu.

Cú pháp

USE mysql;
UPDATE user 
SET authentication_string = PASSWORD('new_password') 
WHERE user = 'user_name' AND host = 'host_name';
FLUSH PRIVILEGES;
  • new_password: Mật khẩu mới
  • user_name: Tên user hiện tại
  • host_name: Host của user (localhost, %, IP...)

Ví dụ: Đổi pass user@localhost thành 'tutorials'

USE mysql;
UPDATE user 
SET authentication_string = PASSWORD('tutorials') 
WHERE user = 'user' AND host = 'localhost';
FLUSH PRIVILEGES;

Lưu ý: MySQL 8.0+ dùng authentication_string thay cho cột password cũ. Luôn chạy FLUSH PRIVILEGES sau UPDATE trực tiếp.