Rotate a MySQL user to a server-generated random password
Build a paste-ready command, then review its compatibility and effects before running it.
mysql -u root -p -e "ALTER USER '<mysql-user>'@'localhost' IDENTIFIED BY RANDOM PASSWORD;"Complete required fields to copy the generated command.
dangerwriterequires privileges Destructive command. Review the target and use a preview or dry-run variant first. Compatibility
MySQL 8.0.18+IDENTIFIED BY RANDOM PASSWORD avoids placing the new password in shell history. Capture the generated password securely and update dependent applications immediately.
Operational knowledgereview due 2027-01-19
RequirementsALTER USER privilegeRequires the privileges needed by ALTER USER for the target account.Dependent applicationsPrepare application credential rotation before changing a production account.
Version supportmysql Current supported releasesVerified for linux using posix, bash, zsh syntax; consult compatibility notes for platform-specific differences.
Expected signalsEnter password:Representative successful output; values vary with the selected target and system state.
Known errorsmysql: command not foundmysql is missing or is not available on PATH.Permission deniedThe current identity does not have the required access.
Verifymysql -u {{targetUser}} -p -e "SELECT CURRENT_USER(), NOW();"The account authenticates with the generated password and CURRENT_USER() identifies the intended account.Rollback noteMySQL cannot reveal the previous password. Rotate the account again or restore access with another administrative account.
Command breakdown
01mysqlCommandRuns the mysql stage of this one-liner.
02-uOptionConfigures mysql with the -u option.
03<administrative-mysql-user>ParameterA value supplied in the Fill parameters section.
04-pOptionSelects the command-specific port, process, or print mode.
05-eOptionConfigures mysql with the -e option.
06"ALTER USER '<mysql-user>'@'<account-host>' IDENTIFIED BY RANDOM PASSWORD;"ParameterA value supplied in the Fill parameters section.
Example input
mysql -u root -p -e "ALTER USER 'appuser'@'localhost' IDENTIFIED BY RANDOM PASSWORD;"
Example output
Enter password:
+---------+-----------+------------------+------------------+
| user | host | generated password | auth_factor |
+---------+-----------+------------------+------------------+
| appuser | localhost | iQ7#nP2!cV9@rT4 | 1 |
+---------+-----------+------------------+------------------+
Illustrative output — exact values vary by system and data.
Official sources
Related commands