You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改MySQL密码且不写入日志?能否让MySQL不记录下一条命令?

How to Change MySQL Password Without Logging the New Password

Great question—keeping sensitive credentials out of logs is crucial for security, and there are reliable ways to do this without letting your new password end up in MySQL's query logs. Here are the best approaches:

The mysqladmin utility is built for administrative tasks like password changes, and it doesn’t log the password in MySQL’s general or slow query logs. Here’s how to use it:

  • Run this command in your terminal (replace root with your username if needed):
    mysqladmin -u root -p password
    
  • You’ll first be prompted to enter your old password, then asked to type your new password twice.
  • This method keeps the password out of command history, process lists, and MySQL logs entirely—no sensitive data gets written down.

Alternative: Temporarily Disable Query Logs (For Client-Side Changes)

If you need to change the password directly within the MySQL client (e.g., using ALTER USER), you can temporarily turn off the logs that would record the password, then re-enable them right after. Note: This requires the SUPER privilege, and will stop logging all queries during the window, so use it cautiously:

  1. Log into MySQL without including the password in the command (to avoid history leaks):
    mysql -u root -p
    
  2. Disable the general query log and slow query log (if enabled):
    SET GLOBAL general_log = 'OFF';
    SET GLOBAL slow_query_log = 'OFF';
    
  3. Change your password using a statement like this (replace the username and new password as needed):
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_secure_new_password';
    
  4. Immediately re-enable the logs to resume normal logging:
    SET GLOBAL general_log = 'ON';
    SET GLOBAL slow_query_log = 'ON';
    

Important Security Notes

  • Never include passwords directly in command lines (e.g., mysql -u root -pMyOldPass password MyNewPass). This exposes the password in shell history and process listings.
  • If you’re using MySQL 8.0+, prefer ALTER USER over older syntax like SET PASSWORD—it’s more secure and aligns with modern best practices.

内容的提问来源于stack exchange,提问作者L.Ray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:09:38