1. vim /etc/my.cnf
在[mysqld]加入
skip-grant-tables
然後保存退出
然後重啓mysql
2 ./etc/init.d/mysqld restart
登錄
3 mysql -u root -p
4. 5.7版本前的mysql運行: update user set password=password("你的密碼") where user="用戶";
4. 5.7版本後的mysql運行:update mysql.user set authentication_string=password('你的密碼') where user='用戶';
5 exit; #退出
6 編輯my.cnf文件刪掉skip-grant-tables 這一行,然後重啓MySQL,/etc/init.d/mysqld restart,否則MySQL仍能免密碼登錄
7 mysql -u root -p輸入密碼