linux下mysql忘记root密码怎么办

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux下mysql忘记root密码怎么办相关的知识,希望对你有一定的参考价值。

Linux下<a href="https://it.cha138.com/mysql/" target="_blank"><strong class="keylink">mysql</strong></a>忘记root密码怎么办?

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

1. 修改MySQL配置文件

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

1.skip-grant-tables

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

1.service mysqld restart

3. 再次进入MySQL

1.[[email protected] 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

以上是关于linux下mysql忘记root密码怎么办的主要内容,如果未能解决你的问题,请参考以下文章

linux下mysql的root密码忘记,怎么改root密码

linux系统mysql忘记root密码怎么登录

MySQL忘记密码怎么修改密码

Linux服务器mysql的root密码忘记怎么办?

linux mysql 忘记密码 怎么修改密

linux下mysql root账户忘记密码