忘记密码
2023-06-10 大约 2 分钟
# Mysql 8.x
MySQL在更新到8.0版本之后,重置、修改密码的方法有所改变(事实上是在5.7.6之后user表中的password字段以及password()方法被废弃了,所以需要调整下旧的密码重置步骤);
# 定位配置文件
[root@192 /]# mysqld --verbose --help | grep -A 1 'Default options'
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
1
2
3
2
3
# 修改配置
# 使用vim进行编辑
vi /etc/my.cnf;
[mysqld]
# 添加这行代码 然后保存退出;
skip-grant-tables
1
2
3
4
5
6
2
3
4
5
6
# 重启MySQL
# 停止mysql
systemctl stop mysqld
# 启动mysql
systemctl start mysqld
1
2
3
4
5
2
3
4
5
# 无密码登录
[root@192 /]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 删除旧密码
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set authentication_string = "" where user = 'yahweh';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 去除免密码登录
修改我们的配置文件,此例中为/etc/my.cnf文件。我们将skip-grant-tables一行去除,并使用重启MySQL服务。
# 修改密码
[root@192 /]# mysql -uyahweh -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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.
# 修改用户名 xxxx 的密码为 123456xxx;注意密码强度否则会报错
mysql> ALTER USER 'xxxx'@'localhost' IDENTIFIED BY '123456xxx';
Query OK, 0 rows affected (0.02 sec)
mysql>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MySQL密码就完成了重置,要记得保存好密码哦。