1.停止当前mysql服务
/etc/init.d/mysqld stop 或 service mysqld stop
(把stop换为status可查看当前状态)
2.以--skip-grant-tables 选项启动mysql
mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
(如果运行此语句没有回到上一级shell,另外打开一个终端继续执行以下步骤即可)
3.登录
mysql -u root mysql
4.修改登录密码:修改yourPassword处即可
mysql> UPDATE user SET Password=PASSWORD('yourPassword') where USER='root';
mysql> FLUSH PRIVILEGES;
mysql> exit