MySQL是极为流行的一款数据库,在更新到8.0版本之后,重置、修改密码的方法有所改变(事实上是在5.7.6之后user表中的password字段以及password()方法被废弃了,所以需要调整下旧的密码重置步骤),在此记录一下供大家查阅。
欢迎大家关注我的个人博客【数洞】 【备用站】
操作流程
1. 定位配置文件
理论上我们的mysql的目录已经在环境变量里了,所以我们直接输入以下命令:
# dain @ localhost in ~ [17:22:22]
$ 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/local/mysql/etc/my.cnf ~/.my.cnf
可以看到,我们的mysql会按顺序查阅四个位置的配置文件,如果这些文件不存在的话,我们可以创建一个新的,例如,我们在/etc/
下创建/etc/my.cnf
文件:
nvim /etc/my.cnf # 我使用的是基于vim的nvim,此处可以使用自己喜欢的编辑器
2. 修改配置
在我们的配置文件中添加以下内容(若没有则创建新文件)并保存退出:
[mysqld]
skip-grant-tables
3. 重启MySQL
在Mac下:
# dain @ localhost in ~ [17:38:46] C:127
$ sudo /usr/local/mysql/support-files/mysql.server restart
在Mac下,我们也可以在系统偏好设置里通过可视化方式重启。
在Debian/Ubuntu下:
# dain @ localhost in ~ [17:38:46] C:127
$ mysql restart
在CentOS/RetHat/Fedora下:
# dain @ localhost in ~ [17:38:46] C:127
$ mysqld restart
4. 无密码登录MySQL
登录Mysql,密码留空:
# dain @ localhost in ~ [17:54:34]
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.12 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>
5. 删除旧密码
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 = 'root';
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> quit
Bye
6. 去除免密码登录
修改我们的配置文件,此例中为/etc/my.cnf
文件。我们将skip-grant-tables
一行去除,并使用步骤3重启MySQL服务。
7. 修改密码
首先,使用步骤4登录MySQL,由于已经删除了密码,所以密码仍然留空即可:
# dain @ localhost in ~ [18:04:21]
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.12 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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Data-Insights123';
Query OK, 0 rows affected (0.02 sec)
这样,我们的MySQL密码就完成了重置,要记得保存好密码哦。