MySQL补充——忘记密码怎么办
摘要:本文主要记录了在忘记密码时怎么办。
部分内容来自以下博客:
https://www.cnblogs.com/wuotto/p/9682400.html
关闭MySQL数据库
使用命令检查MySQL数据库是否已经关闭:
1 [root@localhost ~]# systemctl status mysql
出现“Active: inactive (dead)”表示数据库已关闭,如果是“Active: active (exited)”表示已开启,需要手动关闭:
1 [root@localhost ~]# systemctl stop mysql
修改MySQL的配置文件
找到MySQL的配置文件“my.cnf”,默认路径是:“/etc/my.cnf”。
打开文件并在“[mysqld]”下添加:
1 skip-grant-tables
这句话的作用是在登录MySQL的时候可以跳过密码直接登录。
保存修改并退出。
启动MySQL
使用命令启动MySQL:
1 [root@localhost ~]# systemctl start mysql
输入“mysql”即可进入数据库。
修改密码
连接“mysql”数据库,修改用户密码:
1 mysql>use mysql;2 Reading table information forcompletion of table and column names3 You can turn off this feature to get a quicker startup with -A4
5 Database changed6 mysql> update mysql.user set password=password('123456') where user='root';7 Query OK, 5 rows affected (0.00sec)8 Rows matched: 5 Changed: 5 Warnings: 0
9
10 mysql>
将root用户的密码设为123456。
刷新使改动生效:
1 mysql>flush privileges;2 Query OK, 0 rows affected (0.00 sec)
回改文件并重启
退出MySQL,重新打开“my.cnf”配置文件,删除“skip-grant-tables”,保存退出。
重启MySQL,需要密码登录,输入设置的密码即可:
1 [root@localhost ~]# vim /etc/my.cnf2 [root@localhost ~]# systemctl restart mysql3 [root@localhost ~]# mysql4 ERROR 1045 (28000): Access denied for user 'root'@'localhost'(using password: NO)5 [root@localhost ~]# mysql -u root -p6 Enter password:7 Welcome to the MySQL monitor. Commands end with ; or \g.8 Your MySQL connection id is 4
9 Server version: 5.6.45MySQL Community Server (GPL)10
11 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.12
13 Oracle is a registered trademark of Oracle Corporation and/or its14 affiliates. Other names may be trademarks of their respective15 owners.16
17 Type 'help;' or '\h' for help. Type '\c' to clearthe current input statement.18
19 mysql>