I recently forgot the MySQL password on my local machine and found a way to recover it.

  • Open and edit the configuration file my.cnf:

    sudo vim /opt/homebrew/etc/my.cnf
    
  • Add skip-grant-tables to the configuration file:

    # Default Homebrew MySQL server config
    [mysqld]
    skip-grant-tables
    # Only allow connections from localhost
    bind-address = 127.0.0.1
    mysqlx-bind-address = 127.0.0.1
    
  • Restart the service:

    sudo brew restart mysql
    
  • Connect to MySQL:

    mysql -u root -p
    
  • Flush privileges:

    FLUSH PRIVILEGES;
    
  • Change the account password:

    ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';