MySQL5.5.38和MySQL5.7版本中忘记root密码,解决办法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL5.5.38和MySQL5.7版本中忘记root密码,解决办法相关的知识,希望对你有一定的参考价值。

1、在centos6.5中安装mysql5.5.38版本,忘记密码如何解决?

(1)、先关闭mysqld服务

         service  mysqld  stop

(2)、使用mysqld_safe安全模式启动mysql,使用两个参数:

--skip-grant-tables:跳过授权表

--skip-networking: 跳过网络,防止其他用户对数据库进行读写操作,待密码恢复后可正常开启

  执行命令:  

mysqld_safe  --skip-grant-tables  --skip-networking  &

(3)、无密码登录:

mysql  -u  root

(4)、修改密码:

mysql>  use  mysql;    ###使用mysql数据库

mysql>  update user set password=password(新密码)  where user=root

mysql>  flush  privileges;

mysql>  quit   //退出数据库

(5)、重新启动mysql服务

service  mysqld  restart

(6)、使用新密码登录mysql

mysql  -uroot  -p新密码

2、在centos7中安装mysql5.7.13版本中忘记root密码,如何解决?

(1)、修改主配置文件my.cnf  

vim  /etc/my.cnf

###在[mysqld]中添加

skip-grant-tables

保存,退出

(2)、重启mysql服务

systemctl  mysql  restart

(3)、使用root用户登录(密码为空,直接回车进入)

mysql -u root -p

(4)、在mysql中执行命令:

mysql>  use  mysql;

mysql>  update user  set  authentication_string=password(新密码)  where  user=root;

注释:在mysql5.7版本中,不存在password字段,使用authentication_string字段

mysql>  flush  privileges;

mysql>  quit  //退出数据库

(5)、将原先my.cnf配置文件中添加的skip-grant-tables参数,删除,重启服务

sed -i s/skip-grant-tables/   /g   /etc/my.cnf

systemctl  restart  mysqld

(6)、使用新密码登录数据库测试:

mysql  -u  root  -p新密码


本文出自 “keep常明” 博客,请务必保留此出处http://keep88.blog.51cto.com/11829099/1909970

以上是关于MySQL5.5.38和MySQL5.7版本中忘记root密码,解决办法的主要内容,如果未能解决你的问题,请参考以下文章

mysql5.7如何修改密码以及密码如何忘记无密码登陆

mysql5.7忘记root密码

mysql5.7数据库忘记密码解决方法

mysql5.7中root密码忘记后修改密码

Mysql5.7忘记root密码及mysql5.7修改root密码的方法

Mysql5.7忘记root密码及mysql5.7修改root密码的方法