一、更改MySQL配置文件my.cnf,跳過密碼驗證。
編輯配置文件/etc/my.cnf文件,在[mysqld]下面添加skip-grant-tables,保存退出。如圖:
vim /etc/my.cnf
保存退出。
重啟mysql服務:service mysqld restart。
二、進入MySQL數據庫,更改root密碼。
重啟MySQL服務后,使用mysql命令進入MySQL命令行,更改root密碼。
更改root密碼:
mysql>UPDATE mysql.user SET password=PASSWORD('你的密碼') where USER='root';
注意:必須使用PASSWORD()函數加密密碼,否則登錄不了。
刷新權限表:
mysql>flush privileges;
退出數據庫:
exit
三、刪除跳過密碼驗證配置信息或者注釋掉,重啟MySQL服務。
還原配置到原來的:刪除配置信息中之前添加的skip-grant-tables或者注釋掉。
vim /etc/my.cnf
重啟MySQL服務:service mysqld restart