mysql5.7忘记密码修改方法

      mysql是开发中最常用的关系数据库之一。一般在安装数据库到时候会自定义root密码,有时候会忘记该密码,这时候需要对数据库进行密码修改。

一、windows下更改mysql数据库密码

     在windows下找到my.ini文件,例如:C:\ProgramData\MySQL\MySQL Server 5.7,打开该文件夹下的my.ini文件,找到[mysqld]然后添加skip-grant-tables,意味着跳过密码验证。打开cmd,输入mysql -u root -p回车会直接进入如下:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 184
Server version: 5.7.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

  使用mysql数据库,输入:use mysql;

  更换数据库进行如下操作:

mysql> update user set authentication_string = password("123456") where user = "root";
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1
mysql> flush privileges;       --刷新

  这样以后退出数据库,重新输入mysql -u root -p,输入密码就可以进入了。

二、centos下更改mysql数据库密码

       centos下更改root密码与windows下的做法类似,不过windows下的设置文件在my.ini下,而centos则更改的是my.cnf文件。输入vim /etc/my.cnf。后面的做法与windows下的更改密码方式相同。

备注:
  如果没有成功,可以在更改my.ini或my.cnf文件的时候停止数据库,更改文件后再重新启动配置。

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄