常常设置了mysql的root密码,就忘记掉了,这种事仍是常常发生的。转自他人(http://www.osyunwei.com/archives/2014.html),权作笔记。linux环境下,解决方法:html
一、编辑MySQL配置文件my.cnfmysql
vi /etc/my.cnf #编辑文件,找到[mysqld],在下面添加一行skip-grant-tableslinux
[mysqld]sql
skip-grant-tableside
:wq! #保存退出spa
service mysqld restart #重启MySQL服务rest
二、进入MySQL控制台htm
mysql -uroot -p #直接按回车,这时不须要输入root密码。ip
三、修改root密码it
update mysql.user set password=password('123456') where User="root" and Host="localhost";
flush privileges; #刷新系统受权表
grant all on *.* to 'root'@'localhost' identified by '123456' with grant option;
四、取消/etc/my.cnf中的skip-grant-tables
vi /etc/my.cnf 编辑文件,找到[mysqld],删除skip-grant-tables这一行
:wq! #保存退出
五、重启mysql
service mysqld restart #重启mysql,这个时候mysql的root密码已经修改成123456
六、进入mysql控制台
mysql -uroot -p #进入mysql控制台
123456 #输入密码
以上。