How to Change the MySQL root Password in Linux or macOS via Command Line
If you have forgot the MySQL root password, can’t remember or want to force it….. you can reset the mysql database password from the command line in either Linux or macOS as long as you know the root user password of the computer you are on, this is for MySQL 5.7 onwards:
Stop MySQL
sudo /usr/local/mysql/support-files/mysql.server stop
Start it in safe mode:
sudo mysqld_safe --skip-grant-tables
This will be an ongoing command until the process is finished so open another shell/terminal window, and log in without a password as root:
mysql -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
Change the lowercase ‘MyNewPass’ to what you want – and keep the single quotes.
\q
Start MySQL
sudo /usr/local/mysql/support-files/mysql.server start
Thats it, now your root password will be updated.