打开终端分别执行下面的两行命令
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
再开一个终端
sudo /usr/local/mysql/bin/mysql -u root
mysql>use mysql
update user set authentication_string=password('newpassword') where user='root';