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.[[email protected] mysql]$ mysql -u root -p2.Enter password: #此处直接回车3.Welcome to the MySQL monitor. Commands end with ; or \g.4.Your MySQL connection id is 25.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 its10.affiliates. Other names may be trademarks of their respective11.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 mysql2.Reading table information for completion of table and column names3.You can turn off this feature to get a quicker startup with -A4.5.Database changed6.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: 14.5.mysql> exit;6.Bye
5. 编辑配置文件,把添加的那行删去,再重启MySQL
1.[KANO@kelvin mysql]$ sudo vim /etc/my.cnf2.[KANO@kelvin mysql]$ service mysqld restart3.Redirecting to /bin/systemctl restart mysqld.service4.
然后输入密码就可以登录MySQL了~:-D
时间: 2024-10-12 13:49:03