mysql登录后重置root密码的步骤

mysql重置root密码。

方法一:

  • 编辑配置文件 /etc/my.cnf ,在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证

  • 接下来我们需要重启MySQL,使用service mysqld restart

  • 进入mysql中重置密码,

    mysql> update user set password="你的新密码" where user="root";
    mysql> flush privileges;
    mysql> quit
  • 退出,注释掉配置文件新增的那行,重启。再登录。

方法二:

或者有用下面的方法更改root的密码(这个是基于centos7.5上的mysql8.0的):


#1.停止mysql数据库,因为在centos7.5 上面init.d目录里没有mysqld服务
/etc/init.d/mysqld stop
或者
service mysqld stop

#2.执行如下命令,
mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
#如果报错没有mysqld_safe命令就使用 mysqld命令。

#3.使用root登录mysql数据库
mysql -u root mysql

#4.更新root密码,如果报密码强度不够而报错,就暂时不用password()函数
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
#最新版MySQL请采用如下SQL,
使用mysql8.0及以上的版本,先使用update将密码置为空,再使用alter修改,并设置强度高的密码
mysql> UPDATE user SET authentication_string=PASSWORD('newpassword') where USER='root';
authentication_string:用户密码;在mysql 5.7.9以后废弃了password字段和password()函数;所以这条不能成功。

mysql>alter user 'root'@'localhost' identified by 'Rfasf1231#%!fa';

#5.刷新权限
mysql> FLUSH PRIVILEGES;

#6.退出mysql
mysql> quit

#7.重启mysql
/etc/init.d/mysqld restart
或者 service mysqld restart

#8.使用root用户重新登录mysql
mysql -uroot -p
Enter password: <输入新设的密码newpassword>

 

 

 

posted @ 2021-08-11 17:09  风风羊  阅读(953)  评论(0编辑  收藏  举报