MySQL的root密码找回

1 . 查找my.conf位置

[root@iZ28t57xzbcZ ~]# whereis my.cnf
my: /etc/my.cnf

2 . 修改my.conf文件,新增skip-grant-tables

[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-grant-tables

3 . 重启mysql

systemctl restart mariadb

4 . 登陆修改root密码

[root@localhost log]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.23-log Source distribution

Copyright (c) 2000, 2011, 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
Database changed
mysql> update user set password = password('root') where user = 'root';
Query OK, 4 rows affected (0.91 sec)
Rows matched: 4  Changed: 4  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)

mysql> quit
Bye

5 . 删除之前my.conf文件中新增的skip-grant-tables

6 . 重启DB,登陆正常

7 . 参数skip-grant-tables说明
这个参数看名字就是skip权限用的,通常用来找回root密码用,相对危险,慎用,特别是在生产上

参考:
https://my.oschina.net/Kenyon/blog/87632