linux下mysql忘记root密码怎么办

Linux下MySQL忘记root密码怎么办?

Linux下MySQL忘记root密码怎么办?

1. 修改MySQL配置文件

默认MySQL的配置文件为/etc/my.cnf,在[mysqld]下面添加一行

1.skip-grant-tables

2. 保存配置文件,重启MySQL服务

1.service mysqld restart

3. 再次进入MySQL

1.[KANO@kelvin mysql]$ mysql -u root -p
2.Enter password: #此处直接回车
3.Welcome to the MySQL monitor. Commands end with ; or \g.
4.Your MySQL connection id is 2
5.Server version: 5.7.11 MySQL Community Server (GPL)
6.
7.Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
8.
9.Oracle is a registered trademark of Oracle Corporation and/or its
10.affiliates. Other names may be trademarks of their respective
11.owners.
12.
13.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
14.
15.mysql>
16.

这样我们就能照常进入MySQL

4. 重新修改root密码

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 user SET password = password('123456') WHERE user = 'root';
7.ERROR 1054 (42S22): Unknown column 'password' in 'field list'
8.

这个时候出现了问题,没有找到password这个字段

原来是mysql数据库下已经没有password这个字段,改成了

1.authentication_string

因此我们要更换语句

1.mysql> update mysql.user set authentication_string=password('123456') where user='root';
2.Query OK, 1 row affected, 1 warning (0.03 sec)
3.Rows matched: 1 Changed: 1 Warnings: 1
4.
5.mysql> exit;
6.Bye

5. 编辑配置文件,把添加的那行删去,再重启MySQL

1.[KANO@kelvin mysql]$ sudo vim /etc/my.cnf
2.[KANO@kelvin mysql]$ service mysqld restart
3.Redirecting to /bin/systemctl restart mysqld.service
4.

然后输入密码就可以登录MySQL了~:-D

上一篇:MySQL 5.7 Command Line Client输入密码后闪退和windows下mysql忘记root密码的解决办法


下一篇:附录:MySQL忘记root密码