Mysql8.0.26 version password modification and unable to log in or forget the password to reset the password

1. Password modification

1. Password modification before mysql5.7.9

mysql> USE mysql;
Database changed
mysql> UPDATE user SET authentication_string="密码" WHERE user="root";
Query OK, 1 row affected (0.39 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> FLUSH privileges;  # 刷新保存
Query OK, 0 rows affected (0.13 sec)

2. Mysql8.0.26 modification method
Analysis: Because the password ("password") function is canceled after version 5.7.9, an error will be reported when using authentication_string="password": the
insert image description here
correct way:

//依次输入命令
use mysql;//选择数据库
SELECT user, Host FROM user;
//查询用户以及相关的权限
ALTER USER 'root'@'localhost' IDENTIFIEED WITH mysql_native_password BY "密码";
//注:localhost,对应的是上面查询的用户的主机(Host),我这里已经修改过所以是%,没有修改过的是localhost;
SELECT user, authentication_string FROM user;
//查看是否已经修改
FLUSH privileges;
//刷新权限;
quit;
//退出
//然后冲重新登录验证
mysql -u root =p


insert image description here
insert image description here

2. MySql cannot log in or forgets the password to reset

**Analysis:** If you cannot log in or forget your password, you can choose to log in without a password in mysql security mode.
1. Stop mysql service

net stop mysql
//到安装目录bin目录下打开cmd命令窗口输入上面命令,如果环境变量已经配置好了,可以不需要到目录下打开cmd,直接win+R在运行里面输入cmd打开命令窗口

2. Run the command line mysqld --console --skip-grant-tables --shared-memory with administrator privileges

//win+R在运行里面输入cmd打开命令窗口执行下面的命令
mysqld --console --skip-grant-tables --shared-memory
//启动 MySQL 服务的时候跳过权限表认证

Note: The command window is not static, remember not to close it.
insert image description here
3. Log in without a password to clear the password

//在mysql安装目录下的bin目录下打开cmd 执行下面命令进行无密码登录
mysql -u root
use mysql;//选择数据库
update user set authentication_string='' where user='root';
//先将密码清空,刷新保存
FLUSH privileges;

insert image description here
insert image description here
insert image description here
4. Close the mysqld task and log in again

//关闭mysqld --console --skip-grant-tables --shared-memory执行的任务Ctrl+c或者直接关闭cmd命令窗口
//在mysql安装目录下的bin目录下打开cmd 执行下面命令进行登录
net start mysql//启动数据库
mysql -u root -p
//输入密码(因为密码为空所以直接回车就好)
use mysql;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY "密码";
//修改密码
//注:localhost,对应的是上面查询的用户的主机(Host),我这里已经修改过所以是%,没有修改过的是localhost;
FLUSH privileges;

insert image description here
**

3. Initialize MySql

**
1. Stop mysql

net stop mysql
//到安装目录bin目录下打开cmd命令窗口输入上面命令,如果环境变量已经配置好了,可以不需要到目录下打开cmd,直接win+R在运行里面输入cmd打开命令窗口

2. Transfer the mysql data storage directory
Change the datadir attribute in the configuration file my.ini to the target path (you can copy and transfer the contents of the original /data folder), or delete /data directly.

Open the cmd command line with administrator privileges, enter mysqld --initialize --user=mysql --console, and an initialization password will be generated:
, remember the original password
3. Start mysql

net start mysql

Enter MySQL through the initial password and modify the user password

//打开cmd 到安装目录bin目录下
mysql -u root -p
//原始密码登录
use mysql;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY "密码";
//修改密码登录,刷新
FLUSH privileges;
quit;//退出,使用修改的密码重新登录

Guess you like

Origin blog.csdn.net/A_awen/article/details/127270573