2019年2月25日
linux下mysql忘记root密码解决方法
linux:Centos 7 x86_64
mysql:5.7.22
问题:忘记mysql密码
1、首先停止mysql服务进程:
1 |
service mysqld stop |
然后编辑mysql的配置文件etc/my.cnf
1 |
vim /etc/my.cnf |
找到 [mysqld]这个模块:
在最后面添加一段代码
1 |
skip-grant-tables ##忽略mysql权限问题,直接登录 |
然后保存 :wq!退出
启动mysql服务:
1 |
service mysqld start |
启动服务时老是报错:Starting MySQL… ERROR! The server quit without updating PID file (/usr/local/mysql/data/mysql.pid).
最后也不知虾JB搞了哪里就好了
直接进入mysql数据库:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Starting MySQL. SUCCESS! [root@host ~]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.22 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use mysql; |
使用mysql表,然后进行修改mysql的root密码:
1 2 3 4 5 6 7 |
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update user set password=password("*") where user="root"; ERROR 1054 (42S22): Unknown column 'password' in 'field list' |
这里又报错了:ERROR 1054 (42S22): Unknown column ‘password’ in ‘field list’
后来这样才成功:字段password改成authentication_string(好像是mysql5.7都是这样)
1 2 |
mysql> update user set authentication_string=password('*') where user='root'; Query OK, 0 rows affected, 1 warning (0.00 sec) |
刷新权限退出
1 2 3 4 |
mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> quit; |
最后把之前在配置文件etc/my.cnf添加的那一行“skip-grant-tables”删除掉保存,重启服务:
1 2 3 4 |
[root@host ~]# service mysqld stop Shutting down MySQL.. SUCCESS! [root@host ~]# service mysqld start Starting MySQL.. SUCCESS! |
大功告成!