您的位置:首页 > 数据库 > MySQL

MySQL补充——忘记密码怎么办

2020-07-02 13:45 519 查看

精选30+云产品,助力企业轻松上云!>>>

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 ~]# sys
1000
temctl start mysql

输入“mysql”即可进入数据库。

修改密码

连接“mysql”数据库,修改用户密码:

1 mysql> use mysql;
2 Reading table information for completion of table and column names
3 You can turn off this feature to get a quicker startup with -A
4
5 Database changed
6 mysql> update mysql.user set password=password('123456') where user='root';
7 Query OK, 5 rows affected (0.00 sec)
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.cnf
2 [root@localhost ~]# systemctl restart mysql
3 [root@localhost ~]# mysql
4 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
5 [root@localhost ~]# mysql -u root -p
6 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.45 MySQL Community Server (GPL)
10
11 Copyright (c) 2000, 2019, Oracle and/or its affi
1ff8
liates. All rights reserved.
12
13 Oracle is a registered trademark of Oracle Corporation and/or its
14 affiliates. Other names may be trademarks of their respective
15 owners.
16
17 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
18
19 mysql>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息