本人環境爲MySQL 5.7 + CentOS 6.3。
1. 編輯MySQL配置文件/etc/my.cnf
找到[mysqld],在下面添加一行skip-grant-tables
2. 重啓MySQL
service mysqld restart
3. root用戶登錄MySQL,提示密碼直接回車
mysql -uroot -p
4. 修改root密碼,並刷新權限
update mysql.user set authentication_string=password('新密碼') where user='root' and host='localhost';
flush privileges;
5. 退出MySQL,並編輯MySQL配置文件/etc/my.cnf
6. 重啓MySQL
service mysqld restart
7. 使用新密碼登錄,測試修改成果
注意:如果MySQL是5.5,則修改密碼的語句是update mysql.user set password=password('123456') where user='root' and host='localhost';