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 #输入密码