vi /etc/my.cnf
#編輯文件,找到[mysqld],在下面添加一行skip-grant-tablesmysql
[mysqld]sql
skip-grant-tableside
:wq! #保存退出spa
#重啓MySQL服務rest
service mysqld restart
#直接按回車,這時不須要輸入root密碼。code
mysql -uroot -p
update mysql.user set password=password('123456') where User="root" and Host="localhost";ip
flush privileges; #刷新系統受權表it
grant all on *.* to 'root'@'localhost' identified by '123456' with grant option;io
vi /etc/my.cnf 編輯文件,找到[mysqld],刪除skip-grant-tables這一行table
:wq! #保存退出
service mysqld restart #重啓mysql,這個時候mysql的root密碼已經修改成123456
mysql -uroot -p #進入mysql控制檯
123456 #輸入密碼