It will happen from time to time, that you’re on alien machine and have to brutally update things in db without knowing credentials. Example is for root (quite secure candidate to change because it shouldn’t be used in app 😃 ) but will work for any user.

  • shutdown db
service mysql stop
  • create text file with command like this (update user accordingly) ex. in /tmp/pwchange.txt
SET PASSWORD FOR "root"@"localhost" = PASSWORD("HereYourNewPassword");
  • start mysqld with --init-file param
mysqld_safe --init-file=/tmp/pwchange.txt

sometimes you may require to point configuration file ex. --defaults-file=/etc/mysql/my.cnf

  • wait until it loads and kill mysql (ex. Ctrl+C / kill / etc)
  • start mysql
service mysql start
  • delete file with password
rm -f /tmp/pwchange.txt

You should be able to login with updated password.

Sources link