mysql 5.5.38_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新密码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值